Start by reading the class documentation Rsacryptoproviderservice and AES.
Here examples, taken from MSDN itself, of how to use them:
RSA encryption:
static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAKeyInfo);
encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
return encryptedData;
}
RSA decryption:
static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
byte[] decryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAKeyInfo);
decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
return decryptedData;
}
And to use:
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
byte[] dataToEncrypt = ByteConverter.GetBytes("MEU DADO PARA CRIPTOGRAFAR");
byte[] encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false); // Criptografa
byte[] decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false); // Descriptografa
}
AES encryption:
static byte[] AesEncrypt(string plainText, byte[] key, byte[] IV)
{
byte[] encrypted;
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = IV;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
return encrypted;
}
AES decryption:
static string AesDecrypt(byte[] cipherText, byte[] key, byte[] IV)
{
string plaintext = null;
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = IV;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
return plaintext;
}
And to use:
using (Aes myAes = Aes.Create())
{
byte[] encrypted = AesEncrypt("MEU DADO PARA CRIPTOGRAFAR", myAes.Key, myAes.IV);
string decrypted = AesDecrypt(encrypted, myAes.Key, myAes.IV);
}
I don’t know ASP.NET, but maybe the documentation of. NET encryption functions help you. If you need more help in any particular application (protocol), please [Edit] the question with the relevant details.
– mgibsonbr