PBE

J

jimgardener

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
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 
J

jimgardener

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top