I want to decrypt an encrypted string using RSA 2048 Algorithm. I am able to encrypt the same successfully. But while decrypting I am getting the exception as below:
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
What I have tried:
RSA Encryption:
#region Encrypt_AES_With_CBPublicKey
public string Encrypt_AES_With_CBPublicKey(CBAPIDetails objReqAPI)
{
try
{
txtLog.Text += "Client:Encryption of AES Key with CB Public Key started under : " + MethodInfo.GetCurrentMethod().Name + Environment.NewLine;
var publicKey = "<RSAKeyValue><Modulus>21wEnTU+mcD2w0Lfo1Gv4rtcSWsQJQTNa6gio05AOkV/Er9w3Y13Ddo5wGtjJ19402S71HUeN0vbKILLJdRSES5MHSdJPSVrOqdrll/vLXxDxWs/U0UT1c8u6k/Ogx9hTtZxYwoeYqdhDblof3E75d9n2F0Zvf6iTb4cI7j6fMs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
var AESkeyBytes = Encoding.UTF8.GetBytes(objReqAPI.AES_KEY);
using (var rsa = new RSACryptoServiceProvider(2048))
{
try
{
rsa.FromXmlString(publicKey.ToString());
var encryptedData = rsa.Encrypt(AESkeyBytes, true);
var base64Encrypted = Convert.ToBase64String(encryptedData);
txtLog.Text += "Client:Encryption of AES Key with CB Public Key Finished under : " + MethodInfo.GetCurrentMethod().Name + Environment.NewLine;
return base64Encrypted;
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#region Modulus
public static string Modulus(string pem)
{
byte[] x509der = null;
x509der = Convert.FromBase64String(pem.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", ""));
byte[] seqOID = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01 };
MemoryStream ms = new MemoryStream(x509der);
BinaryReader reader = new BinaryReader(ms);
if (reader.ReadByte() == 0x30) ReadASNLength(reader);
else return null;
int identifierSize = 0;
if (reader.ReadByte() == 0x30) identifierSize = ReadASNLength(reader);
else return null;
if (reader.ReadByte() == 0x06)
{
int oidLength = ReadASNLength(reader);
byte[] oidBytes = new byte[oidLength];
reader.Read(oidBytes, 0, oidBytes.Length);
if (oidBytes.SequenceEqual(seqOID) == false) return null;
int remainingBytes = identifierSize - 2 - oidBytes.Length;
reader.ReadBytes(remainingBytes);
}
if (reader.ReadByte() == 0x03)
{
ReadASNLength(reader);
reader.ReadByte();
if (reader.ReadByte() == 0x30)
{
ReadASNLength(reader);
if (reader.ReadByte() == 0x02)
{
int modulusSize = ReadASNLength(reader);
byte[] modulus = new byte[modulusSize];
reader.Read(modulus, 0, modulus.Length);
if (modulus[0] == 0x00)
{
byte[] tempModulus = new byte[modulus.Length - 1];
Array.Copy(modulus, 1, tempModulus, 0, modulus.Length - 1);
modulus = tempModulus;
}
if (reader.ReadByte() == 0x02)
{
int exponentSize = ReadASNLength(reader);
byte[] exponent = new byte[exponentSize];
reader.Read(exponent, 0, exponent.Length);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
RSAParameters RSAKeyInfo = new RSAParameters();
RSAKeyInfo.Modulus = modulus;
RSAKeyInfo.Exponent = exponent;
rsa.ImportParameters(RSAKeyInfo);
return rsa.ToXmlString(false);
}
}
}
}
return null;
}
#endregion
#region ReadASNLength
public static int ReadASNLength(BinaryReader reader)
{
int length = reader.ReadByte();
if ((length & 0x00000080) == 0x00000080)
{
int count = length & 0x0000000f;
byte[] lengthBytes = new byte[4];
reader.Read(lengthBytes, 4 - count, count);
Array.Reverse(lengthBytes);
length = BitConverter.ToInt32(lengthBytes, 0);
}
return length;
}
#endregion
#endregion
RSA Decryption:
#region AES_Decrypt_CB_Private_Key
public string AES_Decrypt_CB_Private_Key(CBAPIDetails objReqAPI)
{
try
{
string CB_Private_Key = Common.ReadTextFile(Common.CB_Private_KeyFile);
var privateKey = Modulus(CB_Private_Key);
var testData = Encoding.UTF8.GetBytes(objReqAPI.encrypyedRequestKeyData);
using (var rsa = new RSACryptoServiceProvider(2048))
{
try
{
var base64Encrypted = objReqAPI.encrypyedRequestKeyData;
rsa.FromXmlString(privateKey);
var resultBytes = Convert.FromBase64String(base64Encrypted);
var decryptedBytes = rsa.Decrypt(resultBytes, true);
var decryptedData = Encoding.UTF8.GetString(decryptedBytes);
return decryptedData.ToString();
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion