Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > PBE

Reply
 
 
jimgardener
Guest
Posts: n/a
 
      06-23-2008
i am learning cryptography using Jason weiss book.i tried out
encryption using PBEWithMD5AndDES ,i could encrypt a string as below

byte[] salt=new byte[] { (byte)0x3a, (byte)0x44, (byte)0x7f,
(byte)0xfl,(byte)0xa2, (byte)0xe5, (byte)0x87, (byte)0x31 };
int iter=1000;
String msg="a plain message";
char[] passwd=new char[]{'m','y','c','o','d','e'};
PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd, salt, iter) ;
SecretKeyFactory factory=null;
SecretKey key=null;
Cipher cipher=null;
try{
factory =SecretKeyFactory.getInstance("PBEWithMD5AndDES");
key = factory.generateSecret(pbeKeySpec) ;
cipher=Cipher.getInstance("PBEWithMD5AndDES");
cipher.init (Cipher. ENCRYPT_MODE, key) ;

byte[] cipherText = cipher.doFinal(msg .getBytes()) ;
FileOutputStream out=new FileOutputStream("pbeencrypted.txt");
out.write(cipherText);
Arrays.fill(passwd, '\u0000');


now how can i decrypt this?can i use the same keyspec as above and
what algorithm should i use to create cipher? cipher.init (Cipher.
DECRYPT_MODE, key) ; will cause 'InvalidKeyException: requires PBE
parameters'

can someone help?
jim
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-23-2008
On Mon, 23 Jun 2008 07:32:57 -0700 (PDT), jimgardener
<> wrote, quoted or indirectly quoted someone who
said :

>now how can i decrypt this?can i use the same keyspec as above and
>what algorithm should i use to create cipher? cipher.init (Cipher.
>DECRYPT_MODE, key) ; will cause 'InvalidKeyException: requires PBE
>parameters'


I have a similar example posted at
http://mindprod.com/jgloss/cipher.html

It uses the simpler AES algorithm, but much the same problems apply.

In the real world, you must somehow get your secret key to the other
end, perhaps by secure courier. In public/private key systems you can
exchange public keys by insecure channels, so long as you make sure
there was no tampering with a followup phone call to confirm the
digest.

JCE has methods to export keys as raw bytes. You can probable also
export them armoured in various packagings too. see
http://mindprod.com/jgloss/armouring.html
if you want experiment with manual armouring.

JCE suffers from a lack of documenation. There is no way you can do
even the simplest thing without something beyond the JavaDoc. You
can try books or googling for algorithm and method names. I learned
most from comments in various snippets of code.

see http://mindprod.com/jgloss/jce.html

It also helps to read up on the mathematical background of the various
algorithms not so that you fully understand them,, but so at least
you have an idea of what sort of extra parms they may be hungry for.


--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-23-2008
On Mon, 23 Jun 2008 07:32:57 -0700 (PDT), jimgardener
<> wrote, quoted or indirectly quoted someone who
said :

>now how can i decrypt this?can i use the same keyspec as above and
>what algorithm should i use to create cipher? cipher.init (Cipher.
>DECRYPT_MODE, key) ; will cause 'InvalidKeyException: requires PBE
>parameters'


try google on [PBE DECRYPT_MODE] that will find you code involving PBE
and decryption. That will likely give you the key. Watch out.
BouncyCastle, Sun etc providers need not all work identically.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
jimgardener
Guest
Posts: n/a
 
      06-24-2008
try google on [PBE DECRYPT_MODE] that will find you code involving PBE
> and decryption. That will likely give you the key.



i modified the code to

PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd) ;
and
PBEParameterSpec ps = new PBEParameterSpec(salt, 1000);
instead of
PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd,salt, 1000) ;

and then used the ps in
cipher.init (Cipher. ENCRYPT_MODE, key,ps) ;
and later
cipher.init(Cipher.DECRYPT_MODE, key,ps);

this gives the correct decryption..

I still couldn't get the case PBEKeySpec pbeKeySpec = new
PBEKeySpec(passwd,salt, 1000) to do decryption ..i wonder if someone
can solve this..
jim
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Encripting with PBE key krzysztof.murkowski@gmail.com Java 3 10-05-2007 07:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57