Ces deux fonctions sont pratique quand on a deux Guid sous la main :-)

 

public static string EncryptPassword(string sPASSWORD, Guid gKEY, Guid gIV)
{
    UTF8Encoding utf8 = new UTF8Encoding(false);
 
 string sResult = null;
 byte[] byPassword = utf8.GetBytes(sPASSWORD);
 using ( MemoryStream stm = new MemoryStream() )
    {
        Rijndael rij = Rijndael.Create();
        rij.Key = gKEY.ToByteArray();
        rij.IV  = gIV.ToByteArray();
 using ( CryptoStream cs = new CryptoStream(stm, rij.CreateEncryptor(), CryptoStreamMode.Write) )
        {
            cs.Write(byPassword, 0, byPassword.Length);
            cs.FlushFinalBlock();
            cs.Close();
        }
        sResult = Convert.ToBase64String(stm.ToArray());
    }
 return sResult;
}
 
public static string DecryptPassword(string sPASSWORD, Guid gKEY, Guid gIV)
{
    UTF8Encoding utf8 = new UTF8Encoding(false);
 
 string sResult = null;
 byte[] byPassword = Convert.FromBase64String(sPASSWORD);
 using ( MemoryStream stm = new MemoryStream() )
    {
        Rijndael rij = Rijndael.Create();
        rij.Key = gKEY.ToByteArray();
        rij.IV  = gIV.ToByteArray();
 using ( CryptoStream cs = new CryptoStream(stm, rij.CreateDecryptor(), CryptoStreamMode.Write) )
        {
            cs.Write(byPassword, 0, byPassword.Length);
            cs.Flush();
            cs.Close();
        }
 byte[] byResult = stm.ToArray();
        sResult = utf8.GetString(byResult, 0, byResult.Length);
    }
 return sResult;
}
View User Profile for Norbert Saint Georges
Envoié par Norbert Saint Georges dimanche 20 janvier 2013 22:10:42 Categories: C#
Tetrasys Oy
Évaluer ce contenu Voix 0

Commentaires

Les commentaires sont fermés pour ce message.