fork(7) download
  1. using System;
  2. using System.Text;
  3. using System.Security.Cryptography;
  4. using System.Linq;
  5.  
  6.  
  7. namespace Deckard.Jared
  8. {
  9. /// <summary>
  10. /// AES256 class uses the AES algorithm with a provided 256 bit key and a random 128 bit IV to meet PCI standards
  11. /// The IV is randomly generated before each encryption and encoded with the final encrypted string
  12. /// </summary>
  13. public class AES256
  14. {
  15. // Symmetric algorithm interface is used to store the AES service provider
  16. private SymmetricAlgorithm AESProvider;
  17.  
  18. /// <summary>
  19. /// Constructor for AES class that takes a byte array for the key
  20. /// </summary>
  21. /// <param name="key">256 bit key (32 bytes)</param>
  22. public AES256(byte[] key)
  23. {
  24. // Throw error if key is not 256 bits
  25. if (key.Length != 32) throw new CryptographicException("Key must be 256 bits (32 bytes)");
  26.  
  27. // Initialize AESProvider with AES algorithm service
  28. AESProvider = new AesCryptoServiceProvider();
  29. AESProvider.KeySize = 256;
  30.  
  31. // Set the key for AESProvider
  32. AESProvider.Key = key;
  33. }
  34.  
  35. /// <summary>
  36. /// Constructor for AES class that generates the key from a hashed, salted password
  37. /// </summary>
  38. /// <param name="password">Password used to generate the key (Minimum of 8 characters)</param>
  39. /// <param name="salt">Salt used to secure hash from rainbow table attacks (Minimum of 8 characters)</param>
  40. public AES256(string password, string salt)
  41. {
  42. // Throw error if the password or salt are too short
  43. if (password.Length < 8) throw new CryptographicException("Password must be at least 8 characters long");
  44. if (salt.Length < 8) throw new CryptographicException("Salt must be at least 8 characters long");
  45.  
  46. // Initialize AESProvider with AES algorithm service
  47. AESProvider = new AesCryptoServiceProvider();
  48. AESProvider.KeySize = 256;
  49.  
  50. // Initialize a hasher with the 256 bit SHA algorithm
  51. SHA256 sha256 = System.Security.Cryptography.SHA256.Create();
  52.  
  53. // Hash salted password
  54. byte[] key = sha256.ComputeHash(UnicodeEncoding.Unicode.GetBytes(password + salt));
  55.  
  56. // Set the key for AESProvider
  57. AESProvider.Key = key;
  58.  
  59. }
  60.  
  61. /// <summary>
  62. /// Encrypts a string with AES algorithm
  63. /// </summary>
  64. /// <param name="plainText">String to encrypt</param>
  65. /// <returns>Encrypted string with IV prefix</returns>
  66. public string Encrypt(string plainText)
  67. {
  68. // Create new random IV
  69. AESProvider.GenerateIV();
  70.  
  71. // Initialize encryptor now that the IV is set
  72. ICryptoTransform encryptor = AESProvider.CreateEncryptor();
  73.  
  74. // Convert string to bytes
  75. byte[] plainBytes = UnicodeEncoding.Unicode.GetBytes(plainText);
  76.  
  77. // Encrypt plain bytes
  78. byte[] secureBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
  79.  
  80. // Add IV to the beginning of the encrypted bytes
  81. secureBytes = AESProvider.IV.Concat(secureBytes).ToArray();
  82.  
  83. // Return encrypted bytes as a string
  84. return Convert.ToBase64String(secureBytes);
  85. }
  86.  
  87. /// <summary>
  88. /// Decrypts a string with AES algorithm
  89. /// </summary>
  90. /// <param name="secureText">Encrypted string with IV prefix</param>
  91. /// <returns>Decrypted string</returns>
  92. public string Decrypt(string secureText)
  93. {
  94. // Convert encrypted string to bytes
  95. byte[] secureBytes = Convert.FromBase64String(secureText);
  96.  
  97. // Take IV from beginning of secureBytes
  98. AESProvider.IV = secureBytes.Take(16).ToArray();
  99.  
  100. // Initialize decryptor now that the IV is set
  101. ICryptoTransform decryptor = AESProvider.CreateDecryptor();
  102.  
  103. // Decrypt bytes after the IV
  104. byte[] plainBytes = decryptor.TransformFinalBlock(secureBytes, 16, secureBytes.Length - 16);
  105.  
  106. // Return decrypted bytes as a string
  107. return UnicodeEncoding.Unicode.GetString(plainBytes);
  108. }
  109.  
  110. }
  111. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty