Skip to content

Instantly share code, notes, and snippets.

@waldyrfelix
Created October 17, 2012 21:03
Show Gist options
  • Save waldyrfelix/3908170 to your computer and use it in GitHub Desktop.
Save waldyrfelix/3908170 to your computer and use it in GitHub Desktop.
Criptografia no AES no Windows 8
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