Chiffrer et dechiffrer une chaine en C# ?
Source : Stack Overflow [.net]
EDIT 2013-Oct : Bien que nous ayons modifie cette reponse au fil du temps pour corriger les lacunes, veuillez consulter la reponse de jbtule pour une solution plus robuste et mieux informee.
https://stackoverflow.com/a/10366194/188474
Reponse originale :
Voici un exemple fonctionnel derive de la documentation de la classe “RijndaelManaged” et du kit de formation MCTS.
EDIT 2012-Avril : Cette reponse a ete modifiee pour prefixer l’IV selon la suggestion de jbtule et comme illustre ici :
http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged%28v=vs.95%29.aspx
Bonne chance !
public class Crypto
{
//While an app specific salt is not the best practice for
//password based encryption, it's probably safe enough as long as
//it is truly uncommon. Also too much work to alter this answer otherwise.
private static byte[] _salt = __To_Do__("Add a app specific salt here");
/// <summary>
/// Encrypt the given string using AES. The string can be decrypted using
/// DecryptStringAES(). The sharedSecret parameters must match.
/// </summary>
/// <param name="plainText">The text to encrypt.</param>
/// <param name="sharedSecret">A password used to generate a key for encryption.</param>
public static string EncryptStringAES(string plainText, string sharedSecret)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException("plainText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");
string outStr = null; // Encrypted string to return
RijndaelManaged aesAlg = null;
(Reponse tronquee)