Java 编码和 .NET 解码

发布于 2024-08-18 20:31:55 字数 1939 浏览 8 评论 0原文

加密是在 java 中:


String salt = "DC14DBE5F917C7D03C02CD5ADB88FA41";
String password = "25623F17-0027-3B82-BB4B-B7DD60DCDC9B";

char[] passwordChars = new char[password.length()];
password.getChars(0,password.length(), passwordChars, 0);

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(passwordChars, salt.getBytes(), 2, 256);
SecretKey sKey = factory.generateSecret(spec);
byte[] raw = _sKey.getEncoded();

String toEncrypt = "The text to be encrypted.";

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, skey);

AlgorithmParameters params = cipher.getParameters();
byte[] initVector = params.getParameterSpec(IvParameterSpec.class).getIV();

byte[] encryptedBytes = cipher.doFinal(toEncrypt.getBytes());

而解密是在 c# 中:


string hashAlgorithm = "SHA1";
int passwordIterations = 2;
int keySize = 256;

byte[] saltValueBytes = Encoding.ASCII.GetBytes( salt );
byte[] cipherTextBytes = Convert.FromBase64String( cipherText );

PasswordDeriveBytes passwordDB = new PasswordDeriveBytes(password, saltValueBytes, hashAlgorithm  passwordIterations );

byte[] keyBytes = passwordDB.GetBytes( keySize / 8 );

RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor( keyBytes, initVector );

MemoryStream memoryStream = new MemoryStream( cipherTextBytes );

CryptoStream cryptoStream = new CryptoStream( memoryStream, decryptor, CryptoStreamMode.Read );

byte[] plainTextBytes = new byte[ cipherTextBytes.Length ];

int decryptedByteCount = cryptoStream.Read( plainTextBytes, 0, plainTextBytes.Length );

memoryStream.Close();
cryptoStream.Close();

string plainText = Encoding.UTF8.GetString( plainTextBytes, 0, decryptedByteCount );

解密失败,出现异常“填充无效且无法删除”。

知道可能是什么问题吗?

The encryption is in java:


String salt = "DC14DBE5F917C7D03C02CD5ADB88FA41";
String password = "25623F17-0027-3B82-BB4B-B7DD60DCDC9B";

char[] passwordChars = new char[password.length()];
password.getChars(0,password.length(), passwordChars, 0);

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(passwordChars, salt.getBytes(), 2, 256);
SecretKey sKey = factory.generateSecret(spec);
byte[] raw = _sKey.getEncoded();

String toEncrypt = "The text to be encrypted.";

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, skey);

AlgorithmParameters params = cipher.getParameters();
byte[] initVector = params.getParameterSpec(IvParameterSpec.class).getIV();

byte[] encryptedBytes = cipher.doFinal(toEncrypt.getBytes());

While the decryption is in c#:


string hashAlgorithm = "SHA1";
int passwordIterations = 2;
int keySize = 256;

byte[] saltValueBytes = Encoding.ASCII.GetBytes( salt );
byte[] cipherTextBytes = Convert.FromBase64String( cipherText );

PasswordDeriveBytes passwordDB = new PasswordDeriveBytes(password, saltValueBytes, hashAlgorithm  passwordIterations );

byte[] keyBytes = passwordDB.GetBytes( keySize / 8 );

RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor( keyBytes, initVector );

MemoryStream memoryStream = new MemoryStream( cipherTextBytes );

CryptoStream cryptoStream = new CryptoStream( memoryStream, decryptor, CryptoStreamMode.Read );

byte[] plainTextBytes = new byte[ cipherTextBytes.Length ];

int decryptedByteCount = cryptoStream.Read( plainTextBytes, 0, plainTextBytes.Length );

memoryStream.Close();
cryptoStream.Close();

string plainText = Encoding.UTF8.GetString( plainTextBytes, 0, decryptedByteCount );

The decryption failed with exception "Padding is invalid and cannot be removed."

Any idea what might be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春风十里 2024-08-25 20:31:55

这通常表明解密失败。我建议您检查密钥生成函数的输出,看看您是否实际上使用相同的密钥。例如,我注意到 Java 代码意味着您正在使用基于 SHA1 的 HMAC,而 .NET 代码意味着您正在使用未加密的 SHA1 哈希来生成密钥。

或者,也可能是填充不匹配。我没有看到您在 .NET 代码中将 PaddingMode 显式设置为 PKCS7。

This generally indicates that decryption has failed. I suggest you check the output of the key generation functions, to see if you are actually using the same key. I notice, for instance, that the Java code implies you are using a SHA1-based HMAC, whereas the .NET code implies you are using an unkeyed SHA1 hash to generate the key.

Alternatively, it could be a mismatch in the padding. I don't see where you are explicitly setting the PaddingMode to PKCS7 in the .NET code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文