在Java Android中加密,在C# Unity中解密



首先,我希望能够加密我的一个Java类中的一些数据,并将其写入手机上的文本文件中。然后,我在 c# 中的 Unity 类读取它,解密它,就可以使用数据了。

目前,我的Java类可以加密和解密他自己的数据。我的 C# 也可以做同样的事情。问题是,我的 c# 代码无法解密 java 以前加密的内容。我 100% 确定它们具有相同的密钥(打印日志以便比较并且是相同的)。我在 java 和 c# 中的加密之间似乎有些不同。

这是我尝试在 c# 中解密以前由 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代码:

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# 代码:

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);
}

在 C# 中测试加密和解密字符串

Original String : Hello World
Crypted : 7Zmd4yvxgR6Mg0nUDQumBA==
Decrypted : Hello World

在 Java 中加密和解密字符串的测试

Original String : Hello World
Crypted : zQjSpJqU8YkHhMDHw8wuTQ==
Decrypted : Hello World

这些测试期间的关键:

FuVf/CNYkHdyBqejq3eoHQ==

如您所见,它们具有相同的密钥,但对数据的加密方式不同。我似乎无法弄清楚我做错了什么。

找到了解决方案!一切正常。

遵循一些提示后,这是修复:

  1. 确保一切都在欧洲央行。在 Java 中,而不是有这个:

    Ciphercipher = Cipher.getInstance("AES/CBC/PKCS7PADDING");

我只是把它放在加密和解密函数中:

Cipher cipher = Cipher.getInstance("AES");
    在 C# 中,密钥大小和 IV 大小需要
  1. 取决于实际的原始密钥和 IV 数组(不是像 32 或 16 这样的任意数字,需要是密钥的实际大小)

  2. 为了
  3. 将字符串写入文件,我现在直接写入字节,因为它们在我加密后直接写入它们。这使得 C# 端更容易直接获取所有内容并对其进行解密。

所以主要问题是在文件中写入字符串而不是字节。我不知道为什么这会导致错误(错误:无效的块大小)。

警告:使用 ECB 模式不安全,在某些情况下,检索明文是微不足道的。 不要在生产环境中或任何需要安全性的方案中使用此代码。

您的 C# 代码处于 ECB 模式,但您的 Java 代码处于 CBC 模式。

您是实际编写了您拥有的代码还是只是复制并粘贴它? 看起来像后者。 不要复制和粘贴安全代码。 你应该带着意图和理解来写它。 而不是"哦,看这里,我会把它放在一起,希望它有效"。

此外,您处理静脉输液的方式非常不安全。 切勿使用固定静脉注射。 在适当的情况下,它可以完全显示您的明文。 对每个加密操作使用随机生成的 IV。

最新更新