I know, this topic was handled here before, but I just dont get my error. Code in Android:
public String encryptMsg(String input) {
try {
byte[] key_Array = Base64.decode(password, Base64.DEFAULT);
Cipher _Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = { 1, 2, 3, 4, 4, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
IvParameterSpec ivspec = new IvParameterSpec(iv);
Key SecretKey = new SecretKeySpec(key_Array, "AES");
_Cipher.init(Cipher.ENCRYPT_MODE, SecretKey, ivspec);
return Base64.encodeToString(_Cipher.doFinal(input.getBytes()), Base64.DEFAULT);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
public String decryptMsg(String input) {
try {
byte[] key_Array = Base64.decode(password, Base64.DEFAULT);
Cipher _Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = { 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
IvParameterSpec ivspec = new IvParameterSpec(iv);
Key SecretKey = new SecretKeySpec(key_Array, "AES");
_Cipher.init(Cipher.DECRYPT_MODE, SecretKey, ivspec);
byte DecodedMessage[] = Base64.decode(input, Base64.DEFAULT);
return new String(_Cipher.doFinal(DecodedMessage));
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
My code in C#:
private string newEncrypt(string input)
{
RijndaelManaged aes = new RijndaelManaged();
aes.BlockSize = 128;
aes.KeySize = 256;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
byte[] KeyArrBytes32Value = new byte[32];
Array.Copy(Encoding.ASCII.GetBytes(keyString), KeyArrBytes32Value, 32);
byte[] ivArr = { 1, 2, 3, 4, 4, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
byte[] IVBytes16Value = new byte[16];
Array.Copy(ivArr, IVBytes16Value, 16);
aes.Key = KeyArrBytes32Value;
aes.IV = IVBytes16Value;
ICryptoTransform cipher = aes.CreateEncryptor();
byte[] plainTextByte = ASCIIEncoding.UTF8.GetBytes(input);
byte[] CipherText = cipher.TransformFinalBlock(plainTextByte, 0, plainTextByte.Length);
return Convert.ToBase64String(CipherText);
}
private string newDecrypt(string input)
{
RijndaelManaged aes = new RijndaelManaged();
aes.BlockSize = 128;
aes.KeySize = 256;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
byte[] KeyArrBytes32Value = new byte[32];
Array.Copy(Encoding.ASCII.GetBytes(keyString), KeyArrBytes32Value, 32);
byte[] ivArr = { 1, 2, 3, 4, 4, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
byte[] IVBytes16Value = new byte[16];
Array.Copy(ivArr, IVBytes16Value, 16);
aes.Key = KeyArrBytes32Value;
aes.IV = IVBytes16Value;
ICryptoTransform cipher = aes.CreateDecryptor();
byte[] encryptedBytes = Convert.FromBase64CharArray(input.ToCharArray(), 0, input.Length);
byte[] decryptedData = cipher.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
return ASCIIEncoding.UTF8.GetString(decryptedData);
}
I get an error while decrypting text in the android application after i encrypted in the c# application. The error is as follows: "javax.crypto.BadPaddingException: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt" If I'm not totally wrong the padding on both sides is the same ("PKCS").
Does anyone have an idea, what I'm doing wrong?
PS: The key I used to encrypt is 32 numeric characters