public BytesToKey ( OpenSSL.Crypto.MessageDigest md, byte salt, byte data, int count, byte &iv ) : byte[] | ||
md | OpenSSL.Crypto.MessageDigest | |
salt | byte | |
data | byte | |
count | int | |
iv | byte | |
return | byte[] |
public void TestEncryptDecryptWithSalt() { string inputMsg = "This is a message"; byte[] input = Encoding.ASCII.GetBytes(inputMsg); byte[] salt = Encoding.ASCII.GetBytes("salt"); byte[] secret = Encoding.ASCII.GetBytes("Password!"); foreach (var cipher in Ciphers(true)) { Console.Write("Using cipher {0}: ", cipher.LongName); using (var cc = new CipherContext(cipher)) { Console.Write(" KeyLength: {0}, IVLength: {1}, BlockSize: {2}, Stream: {3} ", cipher.KeyLength, cipher.IVLength, cipher.BlockSize, cc.IsStream); byte[] iv; byte[] key = cc.BytesToKey(MessageDigest.SHA1, salt, secret, 1, out iv); var pt = cc.Encrypt(input, key, iv); Assert.AreNotEqual(input, pt); var ct = cc.Decrypt(pt, key, iv); var msg = Encoding.ASCII.GetString(ct); Console.WriteLine("\"{0}\"", msg); Assert.AreEqual(inputMsg, msg); } } }
public void TestCase() { string magic = "Salted__"; const int PKCS5_SALT_LEN = 8; string base64 = "U2FsdGVkX1/moDHvAjok9X4prr8TXQtv9LRAIHk1IE8="; byte[] input = Convert.FromBase64String(base64); byte[] salt = new byte[PKCS5_SALT_LEN]; byte[] msg = new byte[input.Length - magic.Length - PKCS5_SALT_LEN]; Buffer.BlockCopy(input, magic.Length, salt, 0, salt.Length); Buffer.BlockCopy(input, magic.Length + PKCS5_SALT_LEN, msg, 0, msg.Length); using (CipherContext cc = new CipherContext(Cipher.AES_256_CBC)) { byte[] iv; byte[] password = Encoding.ASCII.GetBytes("example"); byte[] key = cc.BytesToKey(MessageDigest.MD5, salt, password, 1, out iv); byte[] output = cc.Decrypt(msg, key, iv); string text = Encoding.ASCII.GetString(output); Console.WriteLine(text); } }
/// <summary> /// Encrypt the specified data using the salt, data and passphraseBytes. /// </summary> /// <param name="salt">The salt.</param> /// <param name="data">The data to encrypt.</param> /// <param name="passphraseBytes">The passphrase bytes.</param> /// <returns>The resulting ciphertext from the encryption process.</returns> private string Encrypt(string salt, byte[] data, byte[] passphraseBytes) { using (CipherContext cc = new CipherContext(Cipher.AES_256_CBC)) { byte[] iv; byte[] encryptionKey = cc.BytesToKey( MessageDigest.SHA512, this.encoding.GetBytes(salt), passphraseBytes, Iterations, out iv); byte[] ciphertextBytes = cc.Encrypt( data, encryptionKey, iv); return string.Concat( salt, SaltDelimiter, Convert.ToBase64String(ciphertextBytes)); } }
public void Execute(string[] args) { try { options.ParseArguments(args); } catch (Exception) { Usage(); return; } MessageDigest md = null; if (options.IsSet("md")) { md = MessageDigest.CreateByName(options.GetString("md")); if (md == null) { Console.Error.WriteLine("{0} is an unsupported message digest type", options.GetString("md")); return; } } if (md == null) md = MessageDigest.MD5; if (options.IsSet("bufsize")) { } BIO bin = Program.GetInFile(options.GetString("infile")); string password = null; if (options.IsSet("password")) password = options.GetString("password"); else if (options.IsSet("kfile")) { string filename = options.GetString("kfile"); string[] lines = File.ReadAllLines(filename); if (lines.Length < 1 || lines[0].Length < 1) { Console.Error.WriteLine("zero length password"); return; } password = lines[0]; } if (password == null) { password = Program.OnPassword(true, options["passarg"]); if (password == null) { Console.Error.WriteLine("error getting password"); return; } } if (options.IsSet("base64")) { } string cipherName = options["cipher"] as string; if (!string.IsNullOrEmpty(cipherName)) { Cipher cipher = Cipher.CreateByName(cipherName); if (cipher == null) { Console.Error.WriteLine("{0} is an unknown cipher", cipherName); return; } byte[] salt = null; if (!options.IsSet("nosalt")) { if (options.IsSet("enc")) { } } byte[] iv; CipherContext cc = new CipherContext(cipher); if (password != null) { byte[] bytes = Encoding.ASCII.GetBytes(password); byte[] key = cc.BytesToKey(MessageDigest.MD5, salt, bytes, 1, out iv); } } //string outfile = this.options["outfile"] as string; //if (string.IsNullOrEmpty(outfile)) // Console.WriteLine(bio.ReadString()); //else // File.WriteAllText(outfile, bio.ReadString()); }
/// <summary> /// Decrypt the specified data using salt, saltBytes, and passphraseBytes. /// </summary> /// <param name="salt">The salt.</param> /// <param name="data">The data to decrypt.</param> /// <param name="passphraseBytes">The passphrase bytes.</param> /// <returns>The plain text after decryption.</returns> private string Decrypt(string salt, byte[] data, byte[] passphraseBytes) { using (CipherContext cc = new CipherContext(Cipher.AES_256_CBC)) { byte[] iv; byte[] encryptionKey = cc.BytesToKey( MessageDigest.SHA512, this.encoding.GetBytes(salt), passphraseBytes, Iterations, out iv); byte[] decryptedBytes = cc.Decrypt(data, encryptionKey, iv); string decryptedText = this.encoding.GetString(decryptedBytes); return decryptedText.Replace(salt, string.Empty); } }