'错误:数据或密钥错误'同时使用Node-RSA库解密密文

时间:2017-11-15 05:24:39

标签: c# node.js encryption cryptography rsa

我使用Node-RSA库来解密以下的密文。

AD2FrZBeg6gqxJu3KPZtyvBhYcgb3Gk/D1hVG6/1bheD+E3sRv07a4mV/9WDiB1N1om8mHRgikNEoRlecv5UsAU=

但发生以下错误。

Error: Error during decryption (probably incorrect key). Original error: Error: Incorrect data or key
at NodeRSA.module.exports.NodeRSA.$$decryptKey 

我的代码正在关注。

    var NodeRSA = require('node-rsa');
    var key = new NodeRSA();

    key.importKey({
        n: new Buffer('0086fa9ba066685845fc03833a9699c8baefb53cfbf19052a7f10f1eaa30488cec1ceb752bdff2df9fad6c64b3498956e7dbab4035b4823c99a44cc57088a23783', 'hex'),
        e: 65537,
        d: new Buffer('5d2f0dd982596ef781affb1cab73a77c46985c6da2aafc252cea3f4546e80f40c0e247d7d9467750ea1321cc5aa638871b3ed96d19dcc124916b0bcb296f35e1', 'hex'),
        p: new Buffer('00c59419db615e56b9805cc45673a32d278917534804171edcf925ab1df203927f', 'hex'),
        q: new Buffer('00aee3f86b66087abc069b8b1736e38ad6af624f7ea80e70b95f4ff2bf77cd90fd', 'hex'),
        dmp1: new Buffer('008112f5a969fcb56f4e3a4c51a60dcdebec157ee4a7376b843487b53844e8ac85', 'hex'),
        dmq1: new Buffer('1a7370470e0f8a4095df40922a430fe498720e03e1f70d257c3ce34202249d21', 'hex'),
        coeff: new Buffer('00b399675e5e81506b729a777cc03026f0b2119853dfc5eb124610c0ab82999e45', 'hex')
    }, 'components');

    var publicComponents = key.exportKey('components-public');
    console.log(publicComponents);
    var encrypted = 'AD2FrZBeg6gqxJu3KPZtyvBhYcgb3Gk/D1hVG6/1bheD+E3sRv07a4mV/9WDiB1N1om8mHRgikNEoRlecv5UsAU=';
    console.log('encrypted: ', encrypted);
    var decrypted = key.decrypt(encrypted,'utf8');
    console.log('decrypted: ', decrypted);

我预测var encrypted字符串与解密函数不兼容。

更新11.16:
我从c#代码中获得了var encrypted c#代码正在关注

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            string publicModulus = "0086fa9ba066685845fc03833a9699c8baefb53cfbf19052a7f10f1eaa30488cec1ceb752bdff2df9fad6c64b3498956e7dbab4035b4823c99a44cc57088a23783";
            byte[] publicExponent = BitConverter.GetBytes(65537);

            // public key generate
            RSAParameters publicKey = new RSAParameters();
            publicKey.Modulus = StringToByteArray(publicModulus);
            publicKey.Exponent = publicExponent;
            rsa.ImportParameters(publicKey);
            publicKeyText = rsa.ToXmlString(false);

            string test = RSAEncrypt("Hello RSA!", publicKeyText);

加密功能正在关注

public string RSAEncrypt(string getValue, string pubKey)
        {
            System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 
            rsa.FromXmlString(pubKey);

            //encode into UFT8
            byte[] inbuf = (new UTF8Encoding()).GetBytes(getValue);

            //encrypt
            byte[] encbuf = rsa.Encrypt(inbuf, false);

            //encode into Base64
            return Convert.ToBase64String(encbuf);

        }

0 个答案:

没有答案