简体   繁体   中英

C# RSA public Modulus/Exponent? (Bad Data)

I have tried and tried but I continue to get "Bad Data". How do you decrypt data using the RSACryptoServiceProvider with the public key's Exponent/Modulus?

public static byte[] Encrypt(byte[] b, byte[] mod, byte[] exp)
{
    CspParameters csp = new CspParameters();
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
    RSACryptoServiceProvider.UseMachineKeyStore = false;

    RSAParameters par = new RSAParameters();
    par.Exponent = exp;
    par.Modulus = mod;
    rsa.ImportParameters(par);

    return rsa.Encrypt(b, false);
}
public static byte[] Decrypt(byte[] b, byte[] pubexp, byte[] mod, byte[] priexp)
{
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    RSACryptoServiceProvider.UseMachineKeyStore = false;
    RSAParameters rp = new RSAParameters();

    rp.Exponent = pubexp;
    rp.D = priexp;

    rp.Modulus = mod;
    rsa.ImportParameters(rp);
    return rsa.Decrypt(b, false);
}

static List<byte[]> Base2Array(string str)
{
    byte[] b = Convert.FromBase64String(str);

    List<byte[]> Bytes = new List<byte[]>();

    int i = 0;
    while (i < b.Length)
    {
        int size = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(b, i));
        i += 4;
        byte[] b2 = new byte[size];
        Array.Copy(b, i, b2, 0, size);
        Bytes.Add(b2);
        i += size;
    }

    return Bytes;
}


static void Main(string[] args)
{
    List<byte[]> pub = Base2Array("AAAAB3NzaC1yc2EAAAABJQAAAIBMW4HxU1glv+CcZpJnvUKEyeNfFoKkyLOVLOOb/vNXQkrkGsNdpYAZkKKizij8fD3u3/iYT8UI+xkFoyonRYVipgCslirJB1VdvLivXs69Ht4vf7VAv2yJSUni3XsIHauMlfOkjJ7DpUW75ZkrxsGieICFWlXvRnAyDdqQrkZRZQ==");
    List<byte[]> pri = Base2Array("AAAAgBSjHDNiojO3UXZg6Ux4VyrOx9SCn9mCWgykWTEUeR6Anp6DxhlPUw3UEEetVy97hlw8iGCEQxcvG4T7qocni9UtUTLdpuQzvr6718y2CP0ouKt/1hVKD9QssT08XUvJEBQnnl2yVZAbIqT/DGnUH36L0BnQE/2ombPakwHscfFFAAAAQQCSfQy2cP8Oa0IR0u0whxqGmuuXY/3tCD8NaaSCYm31ly0QBdxFdf2WkC51DNVaf5/1ErHceMapFN9Z3j+/6lA7AAAAQQCFcMoSA32f240nFBmMv/nn/mL1uSdAXOxjUHViJc6M8ntZvW2ZuP2qTvfA3mh1AK5K69piX/4T72xxqTA2tmrfAAAAQFxX1JunFI+fpobdienVCZcjibwbpDPf1MVTbwQhAXHqVBL3XXgkysS/67X/aWnv/+SdBDaXa1SnDpphSWOkxAQ=");

    //pub[0] 7
    //pub[1] 1
    //pub[2] 128

    //pri[0] 128
    //pri[1] 65
    //pri[2] 65
    //pri[3] 64

    byte[] pubmod = null;
    byte[] primod = null;
    byte[] pubexp = null;
    byte[] priexp = null;

    pubexp = pub[0];
    pubmod = pub[2];

    priexp = pri[0];
    primod = pri[2];


    byte[] bstr = Encoding.ASCII.GetBytes("Test");

    bstr = Encrypt(bstr, pubmod, pubexp);
    bstr = Decrypt(bstr, pubexp, pubmod, null);


    string str = Encoding.ASCII.GetString(bstr);
}

i do something like that:

public byte[] PublicDecryption(byte[] encryptedData)
{
        var encData = new BigInteger(encryptedData);
        BigInteger bnData = encData.modPow(_exponent, _modulus);
        return bnData.getBytes();
}

public byte[] PrivateDecryption(byte[] encryptedData)
{
    var encData = new BigInteger(encryptedData);
    d = new BigInteger(rsaParams.D);
    BigInteger bnData = encData.modPow(d, _modulus);
    return bnData.getBytes();
}

where BigInteger is this:

http://www.codeproject.com/KB/cs/biginteger.aspx

because microsoft implementation is partial and buggy.

I've never had a problem with this.

Hope this help

Few month ago I have tried to implement scenario with private key encryption and public key decryption. I spent a week trying doing this with RSACryptoServiceProvider and.. nothing. They support only two use cases:

  1. Public key encryption , private (full) key decryption.

  2. Signing with private key, verifying with public.

And they have done everything to not allow you do something else with their API. Please, check out msdn forums: I have found many answers like that one msdn , including answers from support development team. All they said: that this is prohibit by design. So my suggestion is don't even try to do that with RSACryptoServiceProvider , better use another implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM