-2

My Public Key

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

Javascript encrypt function

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

}

My C# code:

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();

See html to view my Modulus and Exponent http://103.203.49.220:1234/TestRSA.html

This example return encrypted string by Javascript

I also write it in C#, but It don't return the same value as JS

Link to download c# code. http://www.mediafire.com/file/xzqtxbibqk83h48/RSAEncryptionTestApplication.zip

Please help me to fix it. Ensure C# return the same data as js

bartonjs
  • 30,352
  • 2
  • 71
  • 111
Le Tung Anh
  • 811
  • 6
  • 7
  • First, don't roll your own crypto (unless it's homework). Second, from what I see, your RSA encryption code isn't padding the input. Microsoft RSA cryptographic service provider can use PKCS #1 or OAEP padding. – Anton Tykhyy Apr 30 '17 at 18:44
  • If you have a problem with your code, then you should provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that demonstrates your problem. Help us help you without wasting time on guessing. Also, don't provide a link to your code. Instead, [edit] your question to include the code. Links break all the time and if it does, this question will lose all value. – Artjom B. May 01 '17 at 00:14

1 Answers1

1
RSAParameters rsaParams = new RSAParameters();

var hexValue = FromHex("010001");

This was a good start, things went pretty downhill from there.

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

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

If you insist on using BigInteger to do your own raw RSA (which no one will be able to read, since no one does raw RSA due to its bad security... you need to do padding) remember that

  1. BigInteger takes in byte[] in Little-Endian ordering
  2. BigInteger says that a number is negative if bytes.Last() >= 0x80.

The following isn't GC-optimal, but it'll do.

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

Now, please don't do custom implementations of RSA outside of homework/self-discovery. I'll feel bad if you use this knowledge to write an implementation so full of timing and lack-of-padding holes that your key and data are recovered in less than a day.

bartonjs
  • 30,352
  • 2
  • 71
  • 111