First off, I want to be able to encrypt some data in one of my Java Class and write it in a text file on the phone. Then my Unity class in c# read it, decrypt it and the data can be used.
At the moment, my java class can encrypt and decrypt his own data. My C# can do the same as well. Problem is, my c# code can't decrypt what java previously encrypted. I'm 100% sure they have the same key (printed a log so compare and it's the same). There seems to be something different between my encryption in java and c#.
Here is the error I'm getting when trying to decrypt in c# what was previously crypted by java:
03-22 13:32:57.034 14264 14351 E Unity : CryptographicException: Bad PKCS7 padding. Invalid length 197.
03-22 13:32:57.034 14264 14351 E Unity : at Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (PaddingMode padding, Int32 length, Int32 position) [0x00000] in <filename unknown>:0
03-22 13:32:57.034 14264 14351 E Unity : at Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x00000] in <filename unknown>:0
03-22 13:32:57.034 14264 14351 E Unity : at Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x00000] in <filename unknown>:0
03-22 13:32:57.034 14264 14351 E Unity : at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x00000] in <filename unknown>:0
03-22 13:32:57.034 14264 14351 E Unity : at GoogleReader.Decrypt (System.String text) [0x00000] in <filename unknown>:0
JAVA CODE :
public static String key;
public static String Crypt(String text)
{
try
{
// Get the Key
if(com.UQAC.OceanEmpire.UnityPlayerActivity.myInstance != null){
key = Base64.encodeToString(com.UQAC.OceanEmpire.UnityPlayerActivity.myInstance.key.getEncoded(),Base64.DEFAULT);
com.UQAC.OceanEmpire.UnityPlayerActivity.myInstance.SendMessageToUnity(key);
} else {
return "";
}
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING");
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(Base64.decode(key,Base64.DEFAULT), "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
return Base64.encodeToString(cipher.doFinal(text.getBytes()),Base64.DEFAULT);
}
catch (Exception e)
{
System.out.println("[Exception]:"+e.getMessage());
}
return null;
}
public static String Decrypt(String text)
{
try
{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING");
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(Base64.decode(key,Base64.DEFAULT), "AES");
cipher.init(Cipher.DECRYPT_MODE, SecretKey, ivspec);
byte DecodedMessage[] = Base64.decode(text, Base64.DEFAULT);
return new String(cipher.doFinal(DecodedMessage));
}
catch (Exception e)
{
System.out.println("[Exception]:"+e.getMessage());
}
return null;
}
C# CODE :
public static string keyStr;
public static string Decrypt(string text)
{
RijndaelManaged aes = new RijndaelManaged();
aes.BlockSize = 128;
aes.KeySize = 256;
aes.Mode = CipherMode.ECB;
//aes.Padding = PaddingMode.None;
byte[] keyArr = Convert.FromBase64String(keyStr);
byte[] KeyArrBytes32Value = new byte[32];
Array.Copy(keyArr, KeyArrBytes32Value, Math.Min(KeyArrBytes32Value.Length, keyArr.Length));
byte[] ivArr = { 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
byte[] IVBytes16Value = new byte[16];
Array.Copy(ivArr, IVBytes16Value, Math.Min(ivArr.Length, IVBytes16Value.Length));
aes.Key = KeyArrBytes32Value;
aes.IV = IVBytes16Value;
ICryptoTransform decrypto = aes.CreateDecryptor();
byte[] encryptedBytes = Convert.FromBase64CharArray(text.ToCharArray(), 0, text.Length);
byte[] decryptedData = decrypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
return Encoding.UTF8.GetString(decryptedData);
}
public static string Encrypt(string text)
{
RijndaelManaged aes = new RijndaelManaged();
aes.BlockSize = 128;
aes.KeySize = 256;
aes.Mode = CipherMode.ECB;
byte[] keyArr = Convert.FromBase64String(keyStr);
byte[] KeyArrBytes32Value = new byte[32];
Array.Copy(keyArr, KeyArrBytes32Value, Math.Min(KeyArrBytes32Value.Length, keyArr.Length));
byte[] ivArr = { 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };
byte[] IVBytes16Value = new byte[16];
Array.Copy(ivArr, IVBytes16Value, Math.Min(ivArr.Length, IVBytes16Value.Length));
aes.Key = KeyArrBytes32Value;
aes.IV = IVBytes16Value;
ICryptoTransform encrypto = aes.CreateEncryptor();
byte[] plainTextByte = Encoding.UTF8.GetBytes(text);
byte[] CipherText = encrypto.TransformFinalBlock(plainTextByte, 0, plainTextByte.Length);
return Convert.ToBase64String(CipherText);
}
Test to encrypt and decrypt a string in C#
Original String : Hello World
Crypted : 7Zmd4yvxgR6Mg0nUDQumBA==
Decrypted : Hello World
Test to encrypt and decrypt a string in Java
Original String : Hello World
Crypted : zQjSpJqU8YkHhMDHw8wuTQ==
Decrypted : Hello World
The Key during those test :
FuVf/CNYkHdyBqejq3eoHQ==
As you can see, they have the same key but encrypted the data differently. I can't seem to figure out what I'm doing wrong.