【发布时间】:2011-04-16 23:43:09
【问题描述】:
我正在尝试使用 Cipher1 3.0 解密在 Delphi 中加密的 C# 中的一个字符串,来自 Delphi Encryption Compendium 的第一部分。 我使用 TCipher_Rijndael。
我加密的字符串是:this-is-a-test-example
密码:通过
加密值为:iKBC8kX4ZEk4A1pCj6jwEegqjpxhqw==
当我尝试在 c# 中解密时,我收到错误:要解密的数据长度无效。
有没有人遇到同样的问题,有什么解决办法?
这是c#中的解密方法:
public static byte[] Decrypt(byte[] cipherData,
byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms,
alg.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.Close();
byte[] decryptedData = ms.ToArray();
return decryptedData;
}
这里是 Delphi 中的加密代码:
with TCipher_Rijndael.Create('pass', nil) do
begin
memo2.lines.add ( CodeString( 'this-is-a-test-example' , paEncode, fmtDEFAULT));
Free;
end;
谢谢。
【问题讨论】:
-
这有编码问题的味道 - 你如何在两个应用程序中转换为字符串?
-
您是否在尝试解密之前将字符串解码为字节数组?您输入的加密字符串使用 Base64 编码进行编码——末尾的两个等号是赠品。因此,首先您必须使用 Convert.FromBase64String 将它们转换为字节数组。
-
当然,我将字符串转换为字节数组。
-
C#加密的话,加密后的结果是什么? C# 可以解密它的加密字符串吗?
-
@buda:这里的问题是,c#加密结果和Delphi结果一样吗?有什么不同?回答这个问题将帮助您,也许我们会找到问题所在。
标签: c# delphi aes encryption