本文介绍了RC2CryptoServiceProvider .NET算法与节点js rc2算法的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET的RC2CryptoServiceProvider是否符合OpenSSL.我在CBC中使用RC2CryptoServiceProvider,但是相同文本的加密值(使用相同的密钥和初始化向量)与nodejs加密库的rc2-cbc产生的值不同.Node js加密库符合OpenSSL.

Does .NET's RC2CryptoServiceProvider conform to OpenSSL. I'm using RC2CryptoServiceProvider with CBC but the encrypted value for the same text (using the same key and init vector) is different from what nodejs crypto library's rc2-cbc produces. Node js crypto library conforms to OpenSSL.

有人已经问过这个差异,但还没有答案-

Someone had already asked about this discrepancy but no answers yet - Node.JS RC2-CBC Encryption and Decryption ciphers are not matching with C#

有人可以指出我的完整源代码RC2CryptoServiceProvider吗?加密/解密代码是C#中完全托管的代码,还是在下面使用C ++?

Can someone point me to the complete source code RC2CryptoServiceProvider? Is the encrypt/decrypt code a completely managed one available in C# or does it use C++ underneath?

我正在寻找一种差异,因为我正在寻找一种方法来解密节点js中的.NET应用程序加密的字符串.

I'm interested in finding the differences as I'm looking for a way to decrypt a .NET application encrypted string in node js.

下面是C#代码和相应的节点js代码.对于相同的数据(HelloWorld),密钥和iv,所产生的加密值是不同的.

Below is the C# code and the corresponding node js code. For the same data (HelloWorld), key and iv, the encrypted values produced are different.

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;
    }
}

下面是节点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

推荐答案

我遇到了类似的情况.现有的.NET(核心)代码使用 RC2CryptoServiceProvider 来解密字符串.我想在节点中复制它.

I hit a similar situation. There is existing .NET (core) code using RC2CryptoServiceProvider to decrypt a string. I wanted to replicate this in node.

.NET代码使用密钥大小128(这似乎也是默认值),因此我假设节点(openssl)中的可比算法为 rc2-128 .但这总是在解密时失败.

The .NET code uses keysize 128 (which also appears to be the default) so I assumed the comparable algorithm in node (openssl) would be rc2-128. But this always failed when decrypting.

经过反复试验,我发现在节点中使用 rc2-64 算法的行为与使用keysize 128的.NET代码的行为相同.只是不要问我为什么!

After some trial and error I discovered that using using the rc2-64 algorithm in node behaves the same as the .NET code using keysize 128. Just don't ask me why!

这篇关于RC2CryptoServiceProvider .NET算法与节点js rc2算法的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!