Voici un rapide Crypter/Décrypter des données de chaîne en utilisant AES (Rijndael):
private static readonly byte[] rgbKey = Encoding.UTF8.GetBytes("[email protected]"); // change to your own secure key
private static readonly byte[] rgbIv = Encoding.UTF8.GetBytes("to$eO_e!maI*o3ut"); // change to your own secure initialization vector
public static string Encrypt(string originalString)
{
if (string.IsNullOrEmpty(originalString))
{
throw new ArgumentNullException(
"originalString",
"The string which needs to be encrypted can not be null.");
}
using (var cryptoProvider = new RijndaelManaged())
using (var memoryStream = new MemoryStream())
using (var encryptor = cryptoProvider.CreateEncryptor(rgbKey, rgbIv))
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(originalString);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
}
public static string Decrypt(string encryptedString)
{
if (string.IsNullOrEmpty(encryptedString))
{
throw new ArgumentNullException(
"encryptedString",
"The string which needs to be decrypted can not be null.");
}
using (var cryptoProvider = new RijndaelManaged())
using (var memoryStream = new MemoryStream(Convert.FromBase64String(encryptedString)))
using (var decryptor = cryptoProvider.CreateDecryptor(rgbKey, rgbIv))
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
using (var reader = new StreamReader(cryptoStream))
{
return reader.ReadToEnd();
}
}
Il me semble qu'il utilise PKI pour l'interaction client-serveur et doit stocker les clés publiques et privées associées. Bien que n'étant pas exactement la pratique standard dans cette situation, AES est un moyen raisonnable de protéger les clés PKI. –