using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Aes_Example { class AesExample { // Mensagem que sera criptografada. const string mensagem = "Mensagem que sera criptografada."; // Senha definida da operadora. const String chave = "uexDPnPr"; public static void Main() { try { byte[] pwdBytes = BuildKey(chave); byte[] encrypted = EncryptStringToBytes_Aes(mensagem, pwdBytes); string roundtrip = DecryptStringFromBytes_Aes(encrypted, pwdBytes); Console.WriteLine("Original: {0}", mensagem); Console.WriteLine("Criptografado: {0}", Convert.ToBase64String(encrypted)); Console.WriteLine("Descriptografado: {0}", roundtrip); Console.ReadKey(); } catch (Exception e) { Console.WriteLine("Error: {0}", e.Message); } } static byte[] BuildKey(string chave) { SHA256Managed sha256algorithm = new SHA256Managed(); byte[] originalKeyBytes = Encoding.UTF8.GetBytes(chave); byte[] keyBytes = sha256algorithm.ComputeHash(originalKeyBytes); // primeiros 16 bytes do SHA-256 Array.Resize(ref keyBytes, 16); return keyBytes; } static SymmetricAlgorithm GetCipher(byte[] key) { Aes aesAlg = Aes.Create("AES"); aesAlg.BlockSize = 128; aesAlg.KeySize = 128; aesAlg.Mode = CipherMode.ECB; aesAlg.Padding = PaddingMode.PKCS7; aesAlg.Key = key; aesAlg.IV = new byte[16]; return aesAlg; } static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key) { // Check arguments. if (plainText == null || plainText.Length <= 0) throw new ArgumentNullException("plainText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); byte[] encrypted; // Create an Aes object // with the specified key and IV. using (SymmetricAlgorithm aesAlg = GetCipher(Key)) { // Create a decrytor to perform the stream transform. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for encryption. using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { //Write all data to the stream. swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } // Return the encrypted bytes from the memory stream. return encrypted; } static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key) { // Check arguments. if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("cipherText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); // Declare the string used to hold // the decrypted text. string plaintext = null; // Create an Aes object // with the specified key and IV. using (SymmetricAlgorithm aesAlg = GetCipher(Key)) { // Create a decrytor to perform the stream transform. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for decryption. using (MemoryStream msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } } return plaintext; } } }