public Encrypt ( byte input, byte key, byte iv ) : byte[] | ||
input | byte | |
key | byte | |
iv | byte | |
return | byte[] |
public void TestEncryptDecrypt(Cipher cipher) { var inputMsg = "This is a message"; var input = Encoding.ASCII.GetBytes(inputMsg); var iv = Encoding.ASCII.GetBytes("12345678"); var key = Encoding.ASCII.GetBytes("This is the key"); 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); var pt = cc.Encrypt(input, key, iv); if (cipher == Cipher.Null) Assert.AreEqual(input, pt); else 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 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 Bug3066497() { CipherContext cc = new CipherContext(Cipher.Blowfish_CBC); byte[] inputData = Encoding.UTF8.GetBytes("1234567"); byte[] key = Encoding.UTF8.GetBytes("secret!!"); byte[] iv = Encoding.UTF8.GetBytes("secret!!"); byte[] outputData = cc.Encrypt(inputData, key, iv); }
/// <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)); } }