gpt4 book ai didi

c# - 从 C# 解密 AES,从 Java 加密

转载 作者:行者123 更新时间:2023-12-01 23:58:06 24 4
gpt4 key购买 nike

我们使用下面的代码在 Java 中进行加密

public encrypt(String text) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(passPhrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH); //256 bit
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
this.ecipher = Cipher.getInstance("AES");
this.ecipher.init(Cipher.ENCRYPT_MODE, secret);
byte[] bytes = encrypt.getBytes("UTF-8");
byte[] encrypted = this.ecipher.doFinal(bytes);
return Base64.encodeBase64String(encrypted);
}

我们的供应商正在使用 C# 来解密数据他的代码

string Decrypt(string textToDecrypt, string key)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.ECB;
rijndaelCipher.KeySize = 0x80;
rijndaelCipher.BlockSize = 0x80;
byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[0x10];
int len = pwdBytes.Length;
if (len > keyBytes.Length) {
len = keyBytes.Length;
}
Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Encoding.UTF8.GetString(plainText);
}

但他无法解密数据。他得到了一些垃圾数据。知道如何使用 C# 进行 Java 加密部分解密。

最佳答案

首先,不要对 Java 代码有任何安全性暗示。 ECB模式不是一个好的选择。

其次,C# 代码的问题在于它使用 key 的密码阶段的原始字节,而不是 Java 代码使用的 PBKDF2WithHmacSHA1。 C# 中用于生成 key 的类是 Rfc2898DeriveBytes

关于c# - 从 C# 解密 AES,从 Java 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15296650/

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