I am trying to decrypt string which is encrypted in C#(.Net) using the same key but I am getting Excpetion as below.
C# code
//Decrypting a string
String key = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//Encrypting a string
public static string passwordEncrypt(string inText, string key)
{
byte[] bytesBuff = Encoding.Unicode.GetBytes(inText);
using (Aes aes = Aes.Create())
{
Rfc2898DeriveBytes crypto = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
aes.Key = crypto.GetBytes(32);
aes.IV = crypto.GetBytes(16);
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cStream.Write(bytesBuff, 0, bytesBuff.Length);
cStream.Close();
}
inText = Convert.ToBase64String(mStream.ToArray());
}
}
return inText;
}
Java Code
final static String key = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
final static byte[] ivBytes = new byte[] { 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00 };
public static String decryptData(String base64Text) {
String plainText = "";
try {
byte[] keyBytes = key.getBytes("UTF-8");
try {
// byte[] cipherData = decrypt(ivBytes, keyBytes,
// Base64.decode(base64Text.getBytes("UTF-8"), Base64.DEFAULT));
byte[] cipherData = decrypt(ivBytes, keyBytes,
Base64.getDecoder().decode(base64Text.getBytes("UTF-8")));
plainText = new String(cipherData, "UTF-8");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return plainText;
}
Exception I am getting:
java.security.InvalidKeyException: Invalid AES key length: 36 bytes
at com.sun.crypto.provider.AESCrypt.init(AESCrypt.java:87)
at com.sun.crypto.provider.CipherBlockChaining.init(CipherBlockChaining.java:95)
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:591)
at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:346)
at javax.crypto.Cipher.implInit(Cipher.java:809)
at javax.crypto.Cipher.chooseProvider(Cipher.java:867)
at javax.crypto.Cipher.init(Cipher.java:1399)
at javax.crypto.Cipher.init(Cipher.java:1330)
at com.trinity.report.loginAuth.AES256Cipher.encrypt(AES256Cipher.java:27)
at com.trinity.report.loginAuth.AES256Cipher.encryptData(AES256Cipher.java:52)
at com.trinity.report.loginAuth.AES256Cipher.main(AES256Cipher.java:107)
The same key is used for Encrpyting.
Sample Key: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted String: zSdnqGp9B1BjMsvzvAeYvEidAgUrI%2ByrOGf%2BGVn9tzo%3D
Plain Text: testthree
Please Help. Thanks in Advance.