RSA problem I can't seem to decrypt

G

Guest

I get this error when I try to decrypt the string that is encrypted:
I would like to know what the solution is because this should work :)

{"The data to be decrypted exceeds the maximum for this modulus of 128
bytes."}


code:

// Read public key into a string
public const string PUBLIC_KEY =

"<RSAKeyValue><Modulus>zlOXtyPGngT3Q+bRgMfFxloxmf7D872ytE+ecqJWzTasBuCG9RlUfKr3IzglP4dByH+QHSkpAoKV9bM5IJz0/UR88NQp6s10+5w6OkEo/zcQ//Zrqv8AxgtZDx/0cYZmBEqqIJPu9ecZZIxKYGJFCQ4v+b5gPnIDKwe4WA0Seyk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

public static string RSAEncryption(string text, bool DoOAEPPadding)
{
string result = String.Empty;

if (String.IsNullOrEmpty(text))
{
return result;
}

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
try
{
RSA.FromXmlString(PUBLIC_KEY);
result = Convert.ToBase64String(
RSA.Encrypt(
Encoding.Default.GetBytes(text),
DoOAEPPadding
)
);
}
catch (Exception ex)
{
string m = ex.Message;
}
finally
{
RSA = null;
}

return result;
}

public static string RSADecryption(string text, bool DoOAEPPadding)
{
string result = String.Empty;

if (String.IsNullOrEmpty(text))
{
return result;
}

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
try
{
RSA.FromXmlString(PUBLIC_KEY);
result = Encoding.Default.GetString(
RSA.Decrypt(
Encoding.Default.GetBytes(text),
DoOAEPPadding
)
);
}
catch (Exception ex)
{
string m = ex.Message;
}
finally
{
RSA = null;
}

return result;
}
 
W

William Stacey [MVP]

Using RSA, you can only encrypt/decrypt a data block which is less then the
key size. That is why typically only Session keys are encrypted with RSA,
then an algo like AES is used for the actual data encryption (and it is
faster.)

--
William Stacey [MVP]

|I get this error when I try to decrypt the string that is encrypted:
| I would like to know what the solution is because this should work :)
|
| {"The data to be decrypted exceeds the maximum for this modulus of 128
| bytes."}
|
|
| code:
|
| // Read public key into a string
| public const string PUBLIC_KEY =
|
|
"<RSAKeyValue><Modulus>zlOXtyPGngT3Q+bRgMfFxloxmf7D872ytE+ecqJWzTasBuCG9RlUfKr3IzglP4dByH+QHSkpAoKV9bM5IJz0/UR88NQp6s10+5w6OkEo/zcQ//Zrqv8AxgtZDx/0cYZmBEqqIJPu9ecZZIxKYGJFCQ4v+b5gPnIDKwe4WA0Seyk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
|
| public static string RSAEncryption(string text, bool DoOAEPPadding)
| {
| string result = String.Empty;
|
| if (String.IsNullOrEmpty(text))
| {
| return result;
| }
|
| RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
| try
| {
| RSA.FromXmlString(PUBLIC_KEY);
| result = Convert.ToBase64String(
| RSA.Encrypt(
| Encoding.Default.GetBytes(text),
| DoOAEPPadding
| )
| );
| }
| catch (Exception ex)
| {
| string m = ex.Message;
| }
| finally
| {
| RSA = null;
| }
|
| return result;
| }
|
| public static string RSADecryption(string text, bool DoOAEPPadding)
| {
| string result = String.Empty;
|
| if (String.IsNullOrEmpty(text))
| {
| return result;
| }
|
| RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
| try
| {
| RSA.FromXmlString(PUBLIC_KEY);
| result = Encoding.Default.GetString(
| RSA.Decrypt(
| Encoding.Default.GetBytes(text),
| DoOAEPPadding
| )
| );
| }
| catch (Exception ex)
| {
| string m = ex.Message;
| }
| finally
| {
| RSA = null;
| }
|
| return result;
| }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top