.NET的RC2CryptoServiceProvider是否符合OpenSSL。我使用的是带CBC的RC2CryptoServiceProvider,但是相同文本的加密值(使用相同的密钥和初始化向量)与nodejs密码库的rc2-cbc产生的值不同。Node js加密库符合OpenSSL。
已经有人问过这个差异,但还没有答案-- Node.JS RC2-CBC Encryption and Decryption ciphers are not matching with C#
有人能告诉我完整的源代码RC2CryptoServiceProvider吗?加密/解密代码是C#中可用的完全托管代码,还是其底层使用的是C++?
我感兴趣的是找出不同之处,因为我正在寻找一种方法来解密节点js中的.NET应用程序加密字符串。
下面是C#代码和对应的node js代码。对于相同的数据(HelloWorld)、密钥和iv,产生的加密值是不同的。
public static string Encrypt(string data, string key, string iv)
{
try
{
byte[] ivBytes = Encoding.ASCII.GetBytes(iv);
byte[] keyBytes = Encoding.ASCII.GetBytes(key);
byte[] dataBytes = Encoding.ASCII.GetBytes(data);
RC2 rc = new RC2CryptoServiceProvider();
rc.Mode = CipherMode.CBC;
rc.Key = keyBytes;
rc.IV = ivBytes;
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, rc.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(dataBytes, 0, dataBytes.Length);
stream2.Close();
return Convert.ToBase64String(stream.ToArray());
}
catch
{
return string.Empty;
}
}下面是node的js代码。
algo = 'rc2-cbc'
key = '1234567890'
iv = 'someInit'
keyBuffer = new Buffer(key)
ivBuffer = new Buffer(iv)
cipher = crypto.createCipheriv(algo, keyBuffer, ivBuffer)
textBuffer = new Buffer('HelloWorld')
encrypted = cipher.update(textBuffer)
encryptedFinal = cipher.final()
encryptedText = encrypted.toString('base64') + encryptedFinal.toString('base64')
console.log encryptedText发布于 2021-02-03 01:20:53
我遇到了类似的情况。现有的.NET (核心)代码使用RC2CryptoServiceProvider来解密字符串。我想把它复制到node中。
.NET代码使用密钥大小128 (这似乎也是默认值),所以我假设节点(openssl)中的类似算法应该是rc2-128。但这在解密时总是失败。
经过反复试验,我发现在node中使用rc2-64算法与使用KeySize128的.NET代码的行为相同。别问我为什么!
https://stackoverflow.com/questions/21420749
复制相似问题