
String cipherText = (plainText, key, ivsalt) String plainText = "It's Howdy Doody Time!" #pragma warning restore IDE0063 // Use simple 'using' statementĮxample Usage string key = "Hello, World!" #pragma warning restore IDE0090 // Use 'new(.)' Return array.Where((item, index) => index < lastZeroIndex).ToArray() Private static byte TrimZeroPadding(byte array)įor (int i = array.Length - 1 i >= 0 i-) Using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))Ĭs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length) ĭecryptedBytes = TrimZeroPadding(ms.ToArray()) Public static byte Decrypt(byte bytesToBeDecrypted, byte key, byte iv) Using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))Ĭs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length) Using (MemoryStream ms = new MemoryStream()) Using (var aes = CreateAesInstance(key, iv)) Public static byte Encrypt(byte bytesToBeEncrypted, byte key, byte iv) Var derived = new Rfc2898DeriveBytes(key, iv, Iterations) Īes.Key = derived.GetBytes(aes.KeySize / 8) Īes.IV = derived.GetBytes(aes.BlockSize / 8) Private static Aes CreateAesInstance(byte key, byte iv) SecureString result = new SecureString() įoreach (var character in value.ToArray()) Public static SecureString ToSecureString(string value) Return ToSecureString(DecryptToString(cipherText, key, ivsalt)) Public static SecureString DecryptToSecureString(string cipherText, string key, string ivsalt) / Decrypts the cipher text back to a SecureString using the same secret pass phrase and salt that was ValidateString(cipherText, nameof(cipherText), minLength: 1) īyte cipherBytes = Convert.FromBase64String(cipherText) īyte bytesDecrypted = AesHelper.Decrypt(cipherBytes, keyBytes, ivBytes) Public static string DecryptToString(string cipherText, string key, string ivSalt) Minimum length = 8, Maximum Length = 128. / A string encryped earlier using the same secret Key phrase and IV Salt. / Decrypts the cipher text back to plain text using the same secret Key phrase and IV Salt that was Return Convert.ToBase64String(bytesEncrypted) KeyBytes = SHA256.Create().ComputeHash(keyBytes) īyte bytesEncrypted = AesHelper.Encrypt(plainBytes, keyBytes, ivBytes) Get the bytes of the respective stringsīyte plainBytes = (plainText) īyte keyBytes = (key) īyte ivBytes = (ivSalt) ValidateString(ivSalt, nameof(ivSalt), minLength: 8, maxLength: 128) ValidateString(key, nameof(key), minLength: 8, maxLength: 128) ValidateString(plainText, nameof(plainText), minLength: 1) Public static string Encrypt(string plainText, string key, string ivSalt) / An encrypted cipher text string suitable for passwords to be stored safely into XML or JSON files. / A secret salt for the initialization vector. / The plain text you wish to encrypt to cipher text. / The same Key phrase and IV Salt must be used later when you decrypt the cipher text back to plain text. / Simple AES encryption of a plain text string using the specified secret Key phrase and IV Salt.
#SIMPLE CYPHER PASSWORD CREATOR CODE#
NET Framework application using the same code base contained here. NET 6 application can be decrypted back to plainText The suffix 6 was used since this was written and tested specifically for. The private inner class "AesHelper" works with byte arrays. The public outer or wrapper class "SimpleAes6" works with string inputs and outputs. #pragma warning disable IDE0090 // Use 'new(.)' #pragma warning disable IDE0063 // Use simple 'using' statement storing plain text passwords in Config files, since that would immediately raise an audit flag. At the very least, it is far better using this than The intent is not for military or banking grade cryptography, but something strong enough

Also: the code shown works perfectly well for me in both. Thus my need to make something simple for me to use. That said, I do want to emphasize that I am very much a novice regarding cryptography. Extra: rather than decrypt to just a string, I can also decrypt to a SecureString, which can be passed to a NetworkCredential constructor.I do NOT desire too many choices regarding CipherMode, PaddingMode, etc.I do NOT need military or banking grade encryption.NET Framework 4.8 app using the same methods. I wrote some AES encryption/decryption methods with the following requirements:Īpp using these methods should be able to be decrypted in a.
