gpt4 book ai didi

java - 在 Java Android 中加密并在 C# Unity 中解密

转载 作者:行者123 更新时间:2023-11-30 06:14:56 28 4
gpt4 key购买 nike

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

目前,我的java类可以加密和解密他自己的数据。我的 C# 也可以做同样的事情。问题是,我的 C# 代码无法解密 Java 之前加密的内容。我 100% 确定他们有相同的 key (打印日志以便比较,结果是相同的)。我在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==

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

最佳答案

找到解决办法了!一切正常。

遵循一些提示后,修复如下:

  1. 确保一切都在欧洲央行。在 Java 中安装了这个:

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

我只是将其放入加密和解密函数中:

Cipher cipher = Cipher.getInstance("AES");
  • 在C#中, key 大小和IV大小需要取决于实际的原始Key和IV数组(不是32或16等任意数字,需要是 key 的实际大小)

  • 不再将字符串写入文件,现在我直接写入加密后的字节。这使得 C# 端可以更轻松地直接获取所有内容并对其进行解密。

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

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

    关于java - 在 Java Android 中加密并在 C# Unity 中解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49435812/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com