Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

rsa

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);
}
like image 647
user230821 Avatar asked May 26 '10 03:05

user230821


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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

like image 178
giammin Avatar answered Sep 28 '22 16:09

giammin


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.

like image 37
Wisdom's Wind Avatar answered Sep 28 '22 16:09

Wisdom's Wind