RSA Key generation logic:
const int RSA_FULL = 1;
const string strSaltKey = "ABCDEFGHIJKLMNOP"; //16 Digit Key means 16*8*8 = 1024 bit
CspParameters cspParams;
cspParams = new CspParameters(RSA_FULL);
cspParams.KeyContainerName = strSaltKey ;
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
var rsa = new RSACryptoServiceProvider(cspParams);
string PublicPrivateKey = rsa.ToXmlString(true);
string PublicKey = rsa.ToXmlString(false);
NameSpace : using System.Security.Cryptography;
Encryption Methods:
private static bool _optimalAsymmetricEncryptionPadding = false;
public static string EncryptText(string text, int keySize, string publicKeyXml)
{
var encrypted = Encrypt(Encoding.UTF8.GetBytes(text), publicKeyXml, keySize);
return Convert.ToBase64String(encrypted);
}
private static byte[] Encrypt(byte[] data, string publicKeyXml, int keySize)
{
if (data == null || data.Length == 0)
{
throw new ArgumentException("Data are empty", "data");
}
int maxLength = GetMaxDataLength(keySize);
if (data.Length > maxLength)
{
throw new ArgumentException(String.Format("Maximum data length is {0}", maxLength), "data");
}
if (!IsKeySizeValid(keySize))
{
throw new ArgumentException("Key size is not valid", "keySize");
}
if (String.IsNullOrEmpty(publicKeyXml))
{
throw new ArgumentException("Key is null or empty", "publicKeyXml");
}
using (var provider = new RSACryptoServiceProvider(keySize))
{
provider.FromXmlString(publicKeyXml);
return provider.Encrypt(data, _optimalAsymmetricEncryptionPadding);
}
}
public static int GetMaxDataLength(int keySize)
{
if (_optimalAsymmetricEncryptionPadding)
{
return ((keySize - 384) / 8) + 7;
}
return ((keySize - 384) / 8) + 37;
}
public static bool IsKeySizeValid(int keySize)
{
return keySize >= 384 &&
keySize <= 16384 &&
keySize % 8 == 0;
}
Decryption Methods:
private static bool _optimalAsymmetricEncryptionPadding = false;
public static string DecryptText(string text, int keySize, string privateXml)
{
byte[] decrypted = Decrypt(Convert.FromBase64String(text), keySize, privateXml);
return Encoding.UTF8.GetString(decrypted);
}
public static byte[] Decrypt(byte[] data, int keySize, string publicAndPrivateKeyXml)
{
if (data == null || data.Length == 0) throw new ArgumentException("Data are empty", "data");
if (!IsKeySizeValid(keySize)) throw new ArgumentException("Key size is not valid", "keySize");
if (String.IsNullOrEmpty(publicAndPrivateKeyXml)) throw new ArgumentException("Key is null or empty", "publicAndPr
using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider(keySize))
{
provider.FromXmlString(publicAndPrivateKeyXml);
return provider.Decrypt(data, _optimalAsymmetricEncryptionPadding);
}
}
public static string EncryptText(string text, int keySize, string publicKeyXml)
{
var encrypted = Encrypt(Encoding.UTF8.GetBytes(text), publicKeyXml, keySize);
return Convert.ToBase64String(encrypted);
}
private static byte[] Encrypt(byte[] data, string publicKeyXml, int keySize)
{
if (data == null || data.Length == 0)
{
throw new ArgumentException("Data are empty", "data");
}
int maxLength = GetMaxDataLength(keySize);
if (data.Length > maxLength)
{
throw new ArgumentException(String.Format("Maximum data length is {0}", maxLength), "data");
}
if (!IsKeySizeValid(keySize))
{
throw new ArgumentException("Key size is not valid", "keySize");
}
if (String.IsNullOrEmpty(publicKeyXml))
{
throw new ArgumentException("Key is null or empty", "publicKeyXml");
}
using (var provider = new RSACryptoServiceProvider(keySize))
{
provider.FromXmlString(publicKeyXml);
return provider.Encrypt(data, _optimalAsymmetricEncryptionPadding);
}
}
public static int GetMaxDataLength(int keySize)
{
if (_optimalAsymmetricEncryptionPadding)
{
return ((keySize - 384) / 8) + 7;
}
return ((keySize - 384) / 8) + 37;
}
public static bool IsKeySizeValid(int keySize)
{
return keySize >= 384 &&
keySize <= 16384 &&
keySize % 8 == 0;
}
Sample Code to call Encrypt and Decrypt methods:
To Encrypt :
string strEncrypt = EncryptText("TEST", 1024, strPublicKey);
To Decrypt:
string strDecrypt = DecryptText(strEncrypt , 1024, strPrivateKey);