So..
In Java:
Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKey secretKey = new SecretKeySpec(passwordKey, "AES");
Note passwordKey must be of 16 bytes in length for 128 bits encryption.
IvParameterSpec ivParameterSpec = new IvParameterSpec(rawSecretKey);
Then I decided to use Base 64 encoding for sharing data between C# and Java.
The cipher method is:
public String encryptAsBase64(byte[] clearData) throws Exception {
BASE64Encoder _64e = new BASE64Encoder();
byte[] encryptedData = encrypt(clearData);
return _64e.encode(encryptedData);
}
public byte[] encrypt(byte[] clearData) throws Exception {
aesCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
byte[] encryptedData = aesCipher.doFinal(clearData);
return encryptedData;
}
Now, let's see how do decrypt in C#:
const string password = ...;
RijndaelManaged rijndael = new RijndaelManaged();
ICryptoTransform rijndaelDecryptor =
rijndael.CreateDecryptor(passwordKey, passwordKey);
Then
byte[] newClearData =
rijndaelDecryptor.TransformFinalBlock(cryptedData, 0, cryptedData.Length);
You can use the class Convert for Base 64 encoding.
2 comments:
Hi oogifu,
I've been trying to solve my Encryption/Decryption problem for days already.
Basically i need to encrypt my data using C# and decrypt using Java and vice-versa.
I finally chanced upon your blog and i thought i'll be saved!
But after trying out your code.. i am still getting problem in encryption in c#, decryption in java.
Is it possible for you to give me more information on your Interoperable Encryption code?
I really need help here.. pls help..
Hi,
Can you post the full code to decrypt using C# code.
It would be great help
Post a Comment