AES在groovy中加密,在c#中解密

agyaoht7  于 2024-01-06  发布在  C#
关注(0)|答案(1)|浏览(215)

我需要使用Groovy和AES使用预共享密钥加密一小串文本。
另一端的系统需要使用C#解密,并且不能更改(根本不能)。它使用具有默认设置的AesManaged类,并使用ICryptoTransform CreateDecryptor()进行解密。它使用BlockSize 128,KeySize 256,CBC模式和PKCS 7填充。
问题是,假设两边的密钥和初始化向量是相同的,有人能看出为什么这在c#端不能被解密吗?两边只是从Base64解码密钥,并且有一个公共的共享密钥存储位置。
加密成功。

  1. def cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
  2. def key = new SecretKeySpec(secret.decodeBase64(), 0, 32, "AES")
  3. cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv))
  4. vars.put('encryptedMessage', cipher.doFinal(message.getBytes("UTF-8")).encodeBase64() as String)
  5. vars.put('iv', iv.encodeBase64() as String)

字符串
解密端看起来像这样:

  1. AesManaged aes = new AesManaged();
  2. aes.Key = (code i'm not willing to share)
  3. aes.IV = (code i'm not willing to share)
  4. using (ICryptoTransform decryptor = aes.CreateDecryptor())
  5. {
  6. using (MemoryStream memoryStream = new MemoryStream(payload))
  7. {
  8. using (CryptoStream cryptoStream = new CryptoStream((Stream) memoryStream, decryptor, CryptoStreamMode.Read))
  9. {
  10. using (StreamReader streamReader = new StreamReader((Stream) cryptoStream))
  11. return streamReader.ReadToEnd();
  12. }
  13. }
  14. }

wooyq4lh

wooyq4lh1#

问题不在于加密,加密是完全兼容的。不幸的是,我没有意识到服务器和客户端之间的消息格式是不同的。

相关问题