Reputation: 755
I want to encrypt and decrypt data in c# with rsa. I wrote this code and it works fine for encrypting but i have problem with decrypting. What is my problem?!
Here is my code:
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace Cry
{
public class CryptoUtils
{
public CryptoUtils ()
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
byte[] encryptedData;
byte[] decryptedData;
string decrptedStr = "";
var pub = Convert.FromBase64String("MCgCIQCfkl4xV5T/v3r1bifOc1mVHa9yak5pGjUfAv0r+s6+AwIDAQAB");
var prv = Convert.FromBase64String("MIGsAgEAAiEAn5JeMVeU/7969W4nznNZlR2vcmpOaRo1HwL9K/rOvgMCAwEAAQIg\nMce6pM/6xpIYrMoxluE7JBkVe9Sme9d6NPPJJX3NyBECEgCmwIarl1hSBnTqZNwJ\n8hZhqwIQAPT6CO/l/ma1sDi7eM7tCQISAKH90lYLlr9IinfSN3hp95g1AhAAlyNf\nuioqX1G+y/GVogyJAhEmQQB52juSQ574HnampzXUpQ==");
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
RSAParameters myRSAParameters = RSA.ExportParameters(false);
myRSAParameters.Modulus = pub;
myRSAParameters.Exponent = ByteConverter.GetBytes("65537");
myRSAParameters.D = prv;
encryptedData = RSAEncrypt(dataToEncrypt, myRSAParameters, false);
decryptedData = RSADecrypt(encryptedData, myRSAParameters, false);
decrptedStr = ByteConverter.GetString(decryptedData);
}
}
static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) {
try {
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
RSA.ImportParameters(RSAKeyInfo);
encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
return encryptedData;
} catch (CryptographicException e) {
Console.WriteLine(e.Message);
return null;
}
}
static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) {
try {
byte[] decryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
RSA.ImportParameters(RSAKeyInfo);
decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
return decryptedData;
} catch (CryptographicException e) {
Console.WriteLine(e.ToString());
return null;
}
}
}
}
In line encryptedData = RSAEncrypt(dataToEncrypt, myRSAParameters, false);
data encrypts but in the next line i have this error:
System.Security.Cryptography.CryptographicException: PKCS1 decoding error.
at System.Security.Cryptography.RSAPKCS1KeyExchangeDeformatter.DecryptKeyExchange (System.Byte[] rgbIn) [0x00000] in <filename unknown>:0
at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt (System.Byte[] rgb, Boolean fOAEP) [0x00000] in <filename unknown>:0
I think i did'nt fill myRSAParameters
attributes correctly but i don't know how can fill it!
Upvotes: 4
Views: 9688
Reputation: 33088
In .NET if you specify RSAParameters.D you must also specify (correct) values for P, Q, DP, DQ, and InverseQ.
While Modulus, Exponent, and D are all that are required to do RSA technically, very few implementations actually use D, because it's much more efficient to do RSA via the Chinese Remainder Theorem.
While OpenSSL, for example, will fall back to working with just D; .NET will not.
Upvotes: 3