我有一个RSA公钥指数和模数。如何使用C#加密字符串?

时间:2017-04-30 18:03:29

标签: c# cryptography rsa modulus exponent

我的公钥

setMaxDigits(131);
        var publicKey = new RSAKeyPair('010001', '', '009e57c825fd80fec265fab0aab5ef2af47fb7b82b7b1344f5fd439363e78eff968ec4bfd3d5881b2871c4508c35db0bb307d3115170b5ab555d1d4ac194804673b21b86576ab17dd6d38f8629d1219adfbc71b4663c76369c4802854a13e24946dcb001eabf8d2c2a114acae7ca3fa49d04335983552c74cc1042bd15b3ec1945');

Javascript加密功能

function encryptedString(l, o) {
var h = new Array();
var b = o.length;
var f = 0;
while (f < b) {
    h[f] = o.charCodeAt(f);
    f++;
}
while (h.length % l.chunkSize != 0) {
    h[f++] = 0;
}
var g = h.length;
var p = "";
var e, d, c;
for (f = 0; f < g; f += l.chunkSize) {
    c = new BigInt();
    e = 0;
    for (d = f; d < f + l.chunkSize; ++e) {
        c.digits[e] = h[d++];
        c.digits[e] += h[d++] << 8;
    }
    var n = l.barrett.powMod(c, l.e);
    var m = l.radix == 16 ? biToHex(n) : biToString(n, l.radix);
    p += m + " ";
}
return p.substring(0, p.length - 1);

}

我的C#代码:

RSAParameters rsaParams = new RSAParameters();

var hexValue = FromHex("010001");
var plainTextBytes = System.Text.Encoding.Default.GetBytes(hexValue);
var exp = System.Convert.ToBase64String(plainTextBytes);
rsaParams.Exponent = plainTextBytes;
var hexValue1 = FromHex("009e57c825fd80fec265fab0aab5ef2af47fb7b82b7b1344f5fd439363e78eff968ec4bfd3d5881b2871c4508c35db0bb307d3115170b5ab555d1d4ac194804673b21b86576ab17dd6d38f8629d1219adfbc71b4663c76369c4802854a13e24946dcb001eabf8d2c2a114acae7ca3fa49d04335983552c74cc1042bd15b3ec1945");
var modulus = System.Text.Encoding.Default.GetBytes(hexValue1);
var exp11 = System.Convert.ToBase64String(modulus);
rsaParams.Modulus = modulus;
Modulus = new BigInteger(rsaParams.Modulus);
Exponent = new BigInteger(rsaParams.Exponent);
BigInteger bnData = new BigInteger(newData);
// (bnData ^ Exponent) % Modulus - This Encrypt the data using the public Exponent
BigInteger encData = bnData.modPow(Exponent, Modulus);
var hexEncryptedData = encData.ToHexString();

请参阅html以查看我的模数和指数 http://103.203.49.220:1234/TestRSA.html

此示例通过Javascript返回加密字符串

我也用C#编写它,但它没有返回与JS相同的值

下载c#代码的链接。 http://www.mediafire.com/file/xzqtxbibqk83h48/RSAEncryptionTestApplication.zip

请帮我修理一下。确保C#返回与js相同的数据

1 个答案:

答案 0 :(得分:1)

RSAParameters rsaParams = new RSAParameters();

var hexValue = FromHex("010001");

这是一个良好的开端,事情从那里开始走下坡路。

RSAParameters rsaParams = new RSAParameters
{
    Modulus = FromHex(yourBigLongModulusHex),
    Exponent = FromHex("010001"),
}

using (RSA rsa = RSA.Create())
{
    rsa.ImportParameters(rsaParams);
    return rsa.EncryptData(data, RSAEncryptionPadding.OaepSHA1);
}

如果你坚持使用BigInteger做你自己的原始RSA(没有人能够阅读,因为没有人因为安全性差而进行原始RSA ......你需要做填充)记住

  1. BigInteger以Little-Endian顺序接收byte[]
  2. BigInteger说如果bytes.Last()&gt; = 0x80,则数字为负数。
  3. 以下不是GC优化,但它会做到。

    private static BigInteger BigIntegerFromBigEndian(byte[] data)
    {
        byte[] tmp = new byte[data + 1];
        Buffer.BlockCopy(data, 0, tmp, 1, data.Length);
        Array.Reverse(tmp);
        return new BigInteger(tmp);
    }
    

    现在,请不要在家庭作业/自我发现之外做RSA的自定义实施。如果你使用这些知识来编写一个实现充满时间和缺少填充漏洞,你的密钥和数据可以在不到一天的时间内被恢复,我会感到很难过。