Suppose the application has it's secret values securely loaded. Below is not a secure way to handle this, but for the purpose of this post we will be overly transparent.
1 2 3 | key = "PasswordP@s$w0rdPa$$w0rdP@ssw0rd" ; salt = "S@lt$@ltSa|tSaltS@lt$a|t$@|tSalT" ; iv = "iv1^!vi^1viV!^iv" ; // length must be 16 bytes |
1 2 | cipherText = clearText.Encrypt(key, salt, iv); clearText2 = cipherText.Decrypt(key, salt, iv); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | /// <summary> /// Encrypts text given default values /// </summary> /// <param name="PlainText">/// <param name="Password">/// <param name="Salt">Password Salt value/// <param name="InitialVector">16 character string/// <returns></returns> public static string Encrypt( this string PlainText, string Password, string Salt, string InitialVector) { return PlainText.Encrypt(Password, Salt, "SHA1" , 72, InitialVector, 256); } /// <summary> /// Encrypts a string given set of parameters /// </summary> /// <param name="PlainText">/// <param name="Password">/// <param name="Salt">/// <param name="HashAlgorithm">/// <param name="PasswordIterations">/// <param name="InitialVector">/// <param name="KeySize">/// <returns></returns> public static string Encrypt( this string PlainText, string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize) { try { byte [] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText); byte [] CipherTextBytes; using (ICryptoTransform Encryptor = CreateRijndael(Password, Salt, HashAlgorithm, PasswordIterations, InitialVector, KeySize, CryptoTransformDirection.Encrypt)) { using (MemoryStream MemStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length); cryptoStream.FlushFinalBlock(); CipherTextBytes = MemStream.ToArray(); } } } return Convert.ToBase64String(CipherTextBytes); } catch (Exception ex) { throw ex; } } /// <summary> /// Decrypts text given default values /// </summary> /// <param name="CipherText">/// <param name="Password">/// <param name="Salt">Password Salt value/// <param name="InitialVector">16 character string/// <returns></returns> public static string Decrypt( this string CipherText, string Password, string Salt, string InitialVector) { return CipherText.Decrypt(Password, Salt, "SHA1" , 72, InitialVector, 256); } /// <summary> /// Decrypts a string given set of parameters /// </summary> /// <param name="CipherText">/// <param name="Password">/// <param name="Salt">/// <param name="HashAlgorithm">/// <param name="PasswordIterations">/// <param name="InitialVector">/// <param name="KeySize">/// <returns></returns> public static string Decrypt( this string CipherText, string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize) { try { byte [] CipherTextBytes = Convert.FromBase64String(CipherText); byte [] PlainTextBytes; int ByteCount; using (ICryptoTransform Decryptor = CreateRijndael(Password, Salt, HashAlgorithm, PasswordIterations, InitialVector, KeySize, CryptoTransformDirection.Decrypt)) { using (MemoryStream MemStream = new MemoryStream(CipherTextBytes)) { using (CryptoStream cryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read)) { PlainTextBytes = new byte [CipherTextBytes.Length]; ByteCount = cryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length); } } } return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount); } catch (Exception ex) { //return String.Empty; throw ex; } } /// <summary> /// Enumeration to define which direction the crypto is going /// </summary> public enum CryptoTransformDirection { Encrypt, Decrypt } /// <summary> /// Gets a transform object which will perform the Encryption/Decryption /// </summary> /// <param name="Password">/// <param name="Salt">/// <param name="HashAlgorithm">/// <param name="PasswordIterations">/// <param name="InitialVector">/// <param name="KeySize">/// <param name="direction">Encrypt/Decrypt/// <returns></returns> public static ICryptoTransform CreateRijndael( string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize, CryptoTransformDirection direction) { byte [] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector); byte [] SaltValueBytes = Encoding.ASCII.GetBytes(Salt); PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations); byte [] KeyBytes = DerivedPassword.GetBytes(KeySize / 8); RijndaelManaged SymmetricKey = new RijndaelManaged(); SymmetricKey.Mode = CipherMode.CBC; if (direction == CryptoTransformDirection.Decrypt) { return SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes); } else { return SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes); } } |
No comments:
Post a Comment