Created
October 17, 2012 21:03
-
-
Save waldyrfelix/3908170 to your computer and use it in GitHub Desktop.
Criptografia no AES no Windows 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static readonly byte[] iv = // array of 16 bytes | |
public static string Encrypt(string message, string password) | |
{ | |
var algorithm = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7); | |
var key = algorithm.CreateSymmetricKey(Encoding.UTF8.GetBytes(password).AsBuffer()); | |
var encrypted = CryptographicEngine.Encrypt(key, Encoding.UTF8.GetBytes(message).AsBuffer(), iv.AsBuffer()); | |
return CryptographicBuffer.EncodeToBase64String(encrypted); | |
} | |
public static string Decrypt(string message, string password) | |
{ | |
var algorithm = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7); | |
var key = algorithm.CreateSymmetricKey(Encoding.UTF8.GetBytes(password).AsBuffer()); | |
var encrypted = CryptographicEngine.Decrypt(key, Convert.FromBase64String(message).AsBuffer(), iv.AsBuffer()); | |
return CryptographicBuffer.ConvertBinaryToString( BinaryStringEncoding.Utf8, encrypted); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment