AES encryption in java / decrypt in C#

278 Views Asked by At

I'm using the below code to encrypt an email address using my own key in java:

byte[] key = "testKeyMaster123".getBytes("UTF-8");
SecretKeySpec _secretKey = new SecretKeySpec(key, "AES");
String input = "[email protected]";
Cipher cipher = Cipher.getInstance("AES");
byte[] bytes = input.getBytes("UTF-8");
cipher.init(Cipher.ENCRYPT_MODE, _secretKey);
byte[] output = cipher.doFinal(bytes);
result = Base64.encodeToString(output, Base64.DEFAULT);

and the result is:

9�Ѹ��m�G���M�`7<z��je�G�L

and the encoded result is:

i8PWV9EV5N5L0F5w/pZarRc7G8hJIaC8v/lefv0VaeQ=

and in c# I'm using the below code to decrypt it:

var text = "i8PWV9EV5N5L0F5w/pZarRc7G8hJIaC8v/lefv0VaeQ=";
text = Util.decodeBase64(text);
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.ECB;
rijndaelCipher.Padding = PaddingMode.None;

rijndaelCipher.KeySize = 128;
rijndaelCipher.BlockSize = 128;
byte[] encryptedData = HexToBytes(text);

byte[] pwdBytes = key;
byte[] keyBytes = new byte[0x10];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
{
    len = keyBytes.Length;
}
Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
var str = Encoding.UTF8.GetString(plainText);

but the decoded result is:

���W���K�^p��Z�;�I!����^~�i�

which looks different than the java and decrypted result is not the email, it's become this:

,���M d�������

I'm a newbie in a field of encryption/decryption, and most of the above code is gathered from here and there. I understood some of the code as I went through it but I don't know what's wrong here. any help would be appreciated.

0

There are 0 best solutions below