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?