fork(8) download
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5.  
  6. namespace Aes_Example
  7. {
  8. class AesExample
  9. {
  10. // Mensagem que sera criptografada.
  11. const string mensagem = "Mensagem que sera criptografada.";
  12. // Senha definida da operadora.
  13. const String chave = "uexDPnPr";
  14.  
  15. public static void Main()
  16. {
  17. try
  18. {
  19. byte[] pwdBytes = BuildKey(chave);
  20.  
  21. byte[] encrypted = EncryptStringToBytes_Aes(mensagem, pwdBytes);
  22. string roundtrip = DecryptStringFromBytes_Aes(encrypted, pwdBytes);
  23.  
  24. Console.WriteLine("Original: {0}", mensagem);
  25. Console.WriteLine("Criptografado: {0}", Convert.ToBase64String(encrypted));
  26. Console.WriteLine("Descriptografado: {0}", roundtrip);
  27. Console.ReadKey();
  28.  
  29. }
  30. catch (Exception e)
  31. {
  32. Console.WriteLine("Error: {0}", e.Message);
  33. }
  34. }
  35.  
  36. static byte[] BuildKey(string chave)
  37. {
  38. SHA256Managed sha256algorithm = new SHA256Managed();
  39. byte[] originalKeyBytes = Encoding.UTF8.GetBytes(chave);
  40. byte[] keyBytes = sha256algorithm.ComputeHash(originalKeyBytes);
  41. // primeiros 16 bytes do SHA-256
  42. Array.Resize(ref keyBytes, 16);
  43.  
  44. return keyBytes;
  45. }
  46.  
  47. static SymmetricAlgorithm GetCipher(byte[] key)
  48. {
  49. Aes aesAlg = Aes.Create("AES");
  50. aesAlg.BlockSize = 128;
  51. aesAlg.KeySize = 128;
  52. aesAlg.Mode = CipherMode.ECB;
  53. aesAlg.Padding = PaddingMode.PKCS7;
  54. aesAlg.Key = key;
  55. aesAlg.IV = new byte[16];
  56.  
  57.  
  58. return aesAlg;
  59. }
  60.  
  61. static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key)
  62. {
  63. // Check arguments.
  64. if (plainText == null || plainText.Length <= 0)
  65. throw new ArgumentNullException("plainText");
  66. if (Key == null || Key.Length <= 0)
  67. throw new ArgumentNullException("Key");
  68. byte[] encrypted;
  69. // Create an Aes object
  70. // with the specified key and IV.
  71. using (SymmetricAlgorithm aesAlg = GetCipher(Key))
  72. {
  73. // Create a decrytor to perform the stream transform.
  74. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  75.  
  76. // Create the streams used for encryption.
  77. using (MemoryStream msEncrypt = new MemoryStream())
  78. {
  79. using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
  80. {
  81. using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
  82. {
  83.  
  84. //Write all data to the stream.
  85. swEncrypt.Write(plainText);
  86. }
  87. encrypted = msEncrypt.ToArray();
  88. }
  89. }
  90. }
  91.  
  92. // Return the encrypted bytes from the memory stream.
  93. return encrypted;
  94. }
  95.  
  96. static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key)
  97. {
  98. // Check arguments.
  99. if (cipherText == null || cipherText.Length <= 0)
  100. throw new ArgumentNullException("cipherText");
  101. if (Key == null || Key.Length <= 0)
  102. throw new ArgumentNullException("Key");
  103.  
  104. // Declare the string used to hold
  105. // the decrypted text.
  106. string plaintext = null;
  107.  
  108. // Create an Aes object
  109. // with the specified key and IV.
  110. using (SymmetricAlgorithm aesAlg = GetCipher(Key))
  111. {
  112.  
  113. // Create a decrytor to perform the stream transform.
  114. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  115.  
  116. // Create the streams used for decryption.
  117. using (MemoryStream msDecrypt = new MemoryStream(cipherText))
  118. {
  119. using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  120. {
  121. using (StreamReader srDecrypt = new StreamReader(csDecrypt))
  122. {
  123.  
  124. // Read the decrypted bytes from the decrypting stream
  125. // and place them in a string.
  126. plaintext = srDecrypt.ReadToEnd();
  127. }
  128. }
  129. }
  130. }
  131. return plaintext;
  132. }
  133. }
  134. }
Success #stdin #stdout 0.02s 30184KB
stdin
Standard input is empty
stdout
Original:   Mensagem que sera criptografada.
Criptografado: JGTfV+CntuSutHK0LLeZix9Teu87ynjpJN8d3OaQdWge6yN0stn7/1I5KmMJEFYk
Descriptografado: Mensagem que sera criptografada.