Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Recreate key+iv from shared passphrase (.net to java)

843811Feb 22 2010 — edited May 1 2010
I have to decrypt some customer data (passwords) which are encrypted with a .NET program.
I have very little influence over this process, I just have to decrypt the data properly.

The key and IV must be derived from a shared passphrase string ("SHARED PASSPRASE" here).
However, I'm having a very hard time setting up the key and IV properly. The rest is doable, but I could use some help with that. Any suggestions?

.NET code for decryption:
byte[] rawData = Convert.FromBase64String(<encrypted base64 input>);

byte[] salt = new byte[8];
for (int i = 0; i < salt.Length; i++)
  salt[i] = rawData; 

// setup the encryption algorithm
Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes("SHARED PASSPRASE", salt);
Rijndael aes = Rijndael.Create();

aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.ECB;
aes.BlockSize = 256;
aes.KeySize = 256;
aes.IV = keyGenerator.GetBytes(aes.BlockSize / 8);
aes.Key = keyGenerator.GetBytes(aes.KeySize / 8);
Java decryption code:
Security.addProvider(new BouncyCastleProvider());
BASE64Decoder decoder = new BASE64Decoder();
byte[] encryptedPasswordBytes = decoder.decodeBuffer(<encrypted base64 input>);

byte[] salt = new byte[8];
for (int i = 0; i < salt.length; i++)
salt[i] = encryptedPasswordBytes[i];

// -------------
// Here I am stuck, how to get the proper keyspec?
byte[] keyBytes = PKCS5S2ParametersGenerator.PKCS5PasswordToBytes("SHARED PASSPRASE".toCharArray());
SecretKeySpec keyspec = new SecretKeySpec(keyBytes, "AES");

// -------------

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, keyspec);
byte[] decryptedPasswordBytes = cipher.doFinal(encryptedPasswordBytes);
Any help and/or suggestions?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 29 2010
Added on Feb 22 2010
5 comments
932 views