![]() |
PBE
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 |
Re: PBE
On Mon, 23 Jun 2008 07:32:57 -0700 (PDT), jimgardener
<jimgardener@gmail.com> 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 |
Re: PBE
On Mon, 23 Jun 2008 07:32:57 -0700 (PDT), jimgardener
<jimgardener@gmail.com> 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 |
Re: PBE
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 |
| All times are GMT. The time now is 03:15 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.