0

I'm trying to learn about cryptography in C#. The assignment I'm working states that I should encrypt a public key using the following parameters:

Exponent: 11362144024378542149995530613390009998716430950387619937190061108597 56991949056217104428764418891953024519224435553542666457374543274095 09639333989384262385729949578624044207610948821627355876693570108394 89980856934670387451355246115777158531243784255520787524178833140187 0311503661882350734256428011446552231

Modulus: 99656440840574176563305385521896948249485597887868788305755844436736 81373571688938415608140410885678541170145805757280770160982137713823 89714825959368173513133776394580030346373515296029247746151060318750 65736828376549082962569871367654360928995574432638495308492887000005 021125506027838956077501182295786099

I have created a class called RsaEncryption and defined a constructor and initialized values for the exponent and modulus, converting them both to an array of bytes as seen below:

RSAParameters rsaparam = new RSAParameters();
    
    public string modulus = "11362144024378542149995530613390009998716430950387619937190061108597" +
                            "56991949056217104428764418891953024519224435553542666457374543274095" +
                            "09639333989384262385729949578624044207610948821627355876693570108394" +
                            "89980856934670387451355246115777158531243784255520787524178833140187" +
                            "0311503661882350734256428011446552231";
    
    public string exponent = "99656440840574176563305385521896948249485597887868788305755844436736" +
                            "81373571688938415608140410885678541170145805757280770160982137713823" +
                            "89714825959368173513133776394580030346373515296029247746151060318750" +
                            "65736828376549082962569871367654360928995574432638495308492887000005" +
                            "021125506027838956077501182295786099";

    public RsaEncryption()
    {
        rsaparam.Modulus = Encoding.ASCII.GetBytes(modulus);
        rsaparam.Exponent = Encoding.ASCII.GetBytes(exponent);
    }

public string Encrypt(string plainText)
    {
        csp = new RSACryptoServiceProvider();
        csp.ImportParameters(rsaparam);
        var data = Encoding.Unicode.GetBytes(plainText);
        var cypher = csp.Encrypt(data, false);
        return Convert.ToBase64String(cypher);
    }

The issue now is that when I run the encrypt method, I get a bad data exception. On further experimentation, I found that reducing the exponent to 4 characters led to the data being encrypted without issue. Is there a way for me to encrypt the text with the provided exponent?

  • As far as I understand RSA, modulus and exponent are very closely related to each other - maybe your values are not correct? Where did you get modulus and exponent from? – mu88 Jun 24 '21 at 13:28
  • @mu88 I am attempting to consume an API and the vendor provided the exponent and modulus as part of their requirements. – Ozone Developer Jun 24 '21 at 13:33
  • And in which line do you get the exception? – mu88 Jun 24 '21 at 13:44
  • @mu88 on the line where I attempt to import parameters: csp.ImportParameters(rsaparam); – Ozone Developer Jun 24 '21 at 13:54
  • [See this](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacryptoserviceprovider?view=net-5.0#remarks): maybe your key sizes are too big? – mu88 Jun 24 '21 at 13:55
  • 1
    Provided these are consistent values: `RSACryptoServiceProvider()` can only handle a max 4 bytes large public exponent (as you already figured out), [here](https://stackoverflow.com/a/39135984). Even if it could process larger exponents, you need to parse the data (which are presumably decimal values) into a `BigInteger` (rather than UTF8 encode it) and convert it into a `byte` array. Maybe the task is more academic and you should just do a modular exponentiation (without padding)? – Topaco Jun 24 '21 at 18:14

0 Answers0