Super secure! That’s all I can say about this. If you want to encrypt a string using a passphrase, that can be decrypted easily. A bit more difficult to decrypt a string, if the passphrase was also encrypted, and the result was derived 1000 times with random salt. You won’t get the same encrypted string each time you send the original for encrypting. Try it with a simple form application.
static class _EncryptDecrypt { private const int Keysize = 256; private const int DerivationIterations = 1000; public static string ENCRYPT(string _plainText, string _passPhrase) { _passPhrase = EncryptingKey(_passPhrase);//we're gonna encrypt the passPhrase too! Super secure! :) byte[] saltStringBytes = Generate256BitsOfRandomEntropy(); byte[] ivStringBytes = Generate256BitsOfRandomEntropy(); byte[] plainTextBytes = Encoding.UTF8.GetBytes(_plainText); using (Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(_passPhrase, saltStringBytes, DerivationIterations)) { byte[] keyBytes = password.GetBytes(Keysize / 8); using (RijndaelManaged symmetricKey = new RijndaelManaged()) { symmetricKey.BlockSize = 256; symmetricKey.Mode = CipherMode.CBC; symmetricKey.Padding = PaddingMode.PKCS7; using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes)) { using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); byte[] cipherTextBytes = saltStringBytes; cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray(); cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray(); memoryStream.Close(); cryptoStream.Close(); return Convert.ToBase64String(cipherTextBytes); } } } } } } public static string DECRYPT(string cipherText, string passPhrase) { passPhrase = EncryptingKey(passPhrase);//we're gonna encrypt the passPhrase too! Super secure! :) byte[] cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText); byte[] saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray(); byte[] ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray(); byte[] cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray(); using (Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations)) { byte[] keyBytes = password.GetBytes(Keysize / 8); using (RijndaelManaged symmetricKey = new RijndaelManaged()) { symmetricKey.BlockSize = 256; symmetricKey.Mode = CipherMode.CBC; symmetricKey.Padding = PaddingMode.PKCS7; using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes)) { using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes)) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) { byte[] plainTextBytes = new byte[cipherTextBytes.Length]; int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); memoryStream.Close(); cryptoStream.Close(); return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); } } } } } } private static byte[] Generate256BitsOfRandomEntropy() { byte[] randomBytes = new byte[32]; // 32 Bytes => 256 bits. using (RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider()) { rngCsp.GetBytes(randomBytes); } return randomBytes; } private static string EncryptingKey(string value) { StringBuilder Sb = new StringBuilder(); using (SHA256 hash = SHA256Managed.Create()) { Encoding enc = Encoding.UTF8; Byte[] result = hash.ComputeHash(enc.GetBytes(value)); foreach (Byte b in result) Sb.Append(b.ToString("x2")); } return Sb.ToString(); } }
Nice?