/// <summary> /// Method to encrypt with the RFC2898 technique, with a supplied RFC2898 info /// </summary> /// <param name="input">the string to encrypt</param> /// <param name="cryptInfo">the RFC2898 crypt info</param> /// <returns>a byte array of the encrypted data</returns> public static byte[] EncryptRfc2898(string input, RFC2898CryptInfo cryptInfo) { LogManager.Instance.Log().Debug("Calling Encrypt RFC2989"); byte[] toEncode = new ASCIIEncoding().GetBytes(input); byte[] key = new Rfc2898DeriveBytes(cryptInfo.password, new ASCIIEncoding().GetBytes(cryptInfo.salt)).GetBytes(256 / 8); RijndaelManaged symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros }; var encryptor = symmetricKey.CreateEncryptor(key, new ASCIIEncoding().GetBytes(cryptInfo.VIKey)); byte[] encodedBytes; MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, encryptor, CryptoStreamMode.Write); cStream.Write(toEncode, 0, toEncode.Length); cStream.FlushFinalBlock(); encodedBytes = mStream.ToArray(); cStream.Close(); mStream.Close(); return(encodedBytes); }
/// <summary> /// Method to encrypt with the RFC2898 technique, with the static RFC2898 info /// </summary> /// <param name="input">the string to encrypt</param> /// <returns>a byte array of the encrypted data</returns> public static byte[] EncryptRfc2898(string input) { LogManager.Instance.Log().Debug("Calling Encrypt RFC2989 with default crypt info"); if (RFC2898_cryptInfo == null) { LogManager.Instance.Log().Debug("Creating a new static instance of RFC2898 Crypt info"); RFC2898_cryptInfo = new RFC2898CryptInfo(); } return(EncryptRfc2898(input, RFC2898_cryptInfo)); }
/// <summary> /// Method to decrypt with the RFC2898 technique, with a supplied RFC2898 info /// </summary> /// <param name="input">the byte array to decrypt</param> /// <param name="cryptInfo">the RFC2898 crypt info</param> /// <returns>decrypted string data</returns> public static string DecryptRfc2898(byte[] input, RFC2898CryptInfo cryptInfo) { LogManager.Instance.Log().Debug("Calling Decrypt RFC2989"); byte[] key = new Rfc2898DeriveBytes(cryptInfo.password, new ASCIIEncoding().GetBytes(cryptInfo.salt)).GetBytes(256 / 8); RijndaelManaged symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None }; var decryptor = symmetricKey.CreateDecryptor(key, new ASCIIEncoding().GetBytes(cryptInfo.VIKey)); MemoryStream mStream = new MemoryStream(input); CryptoStream cStream = new CryptoStream(mStream, decryptor, CryptoStreamMode.Read); byte[] fromEncode = new byte[input.Length]; cStream.Read(fromEncode, 0, fromEncode.Length); mStream.Close(); cStream.Close(); return(new ASCIIEncoding().GetString(fromEncode).TrimEnd('\0')); }