Austin Muts

Austin Muts

  • 1.4k
  • 330
  • 125.9k

AES 128 bit Encryption-Decryption in C#

Apr 16 2019 7:36 AM
My code does not produce the expected encrypted result of ad7a7a25828cd46c513369798a95de31 after encrypting 788359
 
Any help?
  1. public static class EncryptionService  
  2. {  
  3. private static byte[] key;  
  4. static EncryptionService()  
  5. {  
  6. // key here  
  7. key = UTF8Encoding.UTF8.GetBytes("f22704b8bc0dcc606303b5eb97332630");  
  8. }  
  9. public static string ByteArrayToHexString(byte[] ba)  
  10. {  
  11. return BitConverter.ToString(ba).Replace("-""").ToLower();  
  12. }  
  13. public static string EncryptAndEncode(string plaintext)  
  14. {  
  15. plaintext = "788359";  
  16. return ByteArrayToHexString(AesEncrypt(plaintext));  
  17. }  
  18. public static byte[] AesEncrypt(string inputText)  
  19. {  
  20. byte[] inputBytes = UTF8Encoding.UTF8.GetBytes(inputText);  
  21. byte[] result = null;  
  22. using (MemoryStream memoryStream = new MemoryStream())  
  23. {  
  24. using (CryptoStream cryptoStream = new CryptoStream(memoryStream, GetCryptoAlgorithm().CreateEncryptor(key, key), CryptoStreamMode.Write))  
  25. {  
  26. cryptoStream.Write(inputBytes, 0, inputBytes.Length);  
  27. cryptoStream.FlushFinalBlock();  
  28. result = memoryStream.ToArray();  
  29. }  
  30. }  
  31. return result;  
  32. }  
  33. public static byte[] StringToByteArray(string hex)  
  34. {  
  35. return Enumerable.Range(0, hex.Length)  
  36. .Where(x => x % 2 == 0)  
  37. .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))  
  38. .ToArray();  
  39. }  
  40. public static string DecodeAndDecrypt(string cipherText)  
  41. {  
  42. string DecodeAndDecrypt = AesDecrypt(StringToByteArray(cipherText));  
  43. return (DecodeAndDecrypt);  
  44. }  
  45. public static string AesDecrypt(Byte[] inputBytes)  
  46. {  
  47. Byte[] outputBytes = inputBytes;  
  48. string plaintext = string.Empty;  
  49. using (MemoryStream memoryStream = new MemoryStream(outputBytes))  
  50. {  
  51. using (CryptoStream cryptoStream = new CryptoStream(memoryStream, GetCryptoAlgorithm().CreateDecryptor(key,key), CryptoStreamMode.Read))  
  52. {  
  53. using (StreamReader srDecrypt = new StreamReader(cryptoStream))  
  54. {  
  55. plaintext = srDecrypt.ReadToEnd();  
  56. }  
  57. }  
  58. }  
  59. return plaintext;  
  60. }  
  61. private static RijndaelManaged GetCryptoAlgorithm()  
  62. {  
  63. RijndaelManaged algorithm = new RijndaelManaged();  
  64. //set the mode, padding and block size  
  65. // algorithm.Padding = PaddingMode.ISO10126;  
  66. algorithm.Padding = PaddingMode.Zeros;  
  67. //algorithm.Mode = CipherMode.CBC;OFB  
  68. algorithm.Mode = CipherMode.ECB;  
  69. algorithm.KeySize = 256;  
  70. algorithm.BlockSize =128 ;  
  71. // IV = Encoding.UTF8.GetBytes(algorithm.BlockSize / 8);  
  72. // algorithm.Key = keyAndIvBytes  
  73. //algorithm.IV = Encoding.UTF8.GetBytes(128 / 8);  
  74. return algorithm;  
  75. }  
  76. }  

Answers (3)