using System; using System.IO; using System.Security.Cryptography; namespace FSI.Lib.DeEncryptString { public static class DeEncrypt { /// /// Encrypts the string. /// /// The clear text. /// The key. /// The IV. /// private static byte[] CryptString(byte[] clearText, byte[] Key, byte[] IV) { MemoryStream ms = new MemoryStream(); Rijndael alg = Rijndael.Create(); alg.Key = Key; alg.IV = IV; CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(clearText, 0, clearText.Length); cs.Close(); byte[] encryptedData = ms.ToArray(); return encryptedData; } /// /// Encrypts the string. /// /// The clear text. /// The password. /// public static string CryptString(string clearText, string Password) { byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText); PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); byte[] encryptedData = CryptString(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16)); return Convert.ToBase64String(encryptedData); } /// /// Decrypts the string. /// /// The cipher data. /// The key. /// The IV. /// private static byte[] DecryptString(byte[] cipherData, byte[] Key, byte[] IV) { MemoryStream ms = new MemoryStream(); var alg = Rijndael.Create(); alg.Key = Key; alg.IV = IV; CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(cipherData, 0, cipherData.Length); cs.Close(); byte[] decryptedData = ms.ToArray(); return decryptedData; } /// /// Decrypts the string. /// /// The cipher text. /// The password. /// public static string DecryptString(string cipherText, string Password) { byte[] cipherBytes = Convert.FromBase64String(cipherText); PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); byte[] decryptedData = DecryptString(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16)); return System.Text.Encoding.Unicode.GetString(decryptedData); } } }