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 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); } }
public void TestSealOpen(Cipher cipher) { if (cipher == Cipher.Null) Assert.Ignore(); var inputMsg = "This is a message"; var input = Encoding.ASCII.GetBytes(inputMsg); using (var cc = new CipherContext(cipher)) { var env = cc.Seal(Keys, input); Assert.AreNotEqual(input, env.Data); for (int i = 0; i < Keys.Length; i++) { var result = cc.Open(env.Data, env.Keys[i], env.IV, Keys[i]); Assert.AreEqual(input, result); } } }
public void TestSealOpen() { string inputMsg = "This is a message"; byte[] input = Encoding.ASCII.GetBytes(inputMsg); const int numKeys = 10; var rsas = new RSA[numKeys]; var pkeys = new CryptoKey[numKeys]; for (int i = 0; i < numKeys; i++) { rsas[i] = new RSA(); rsas[i].GenerateKeys(1024, BigNumber.One, null, null); pkeys[i] = new CryptoKey(rsas[i]); } try { foreach (var cipher in Ciphers(true)) { using (var cc = new CipherContext(cipher)) { var env = cc.Seal(pkeys, input); Assert.AreNotEqual(input, env.Data); for (int i = 0; i < numKeys; i++) { var result = cc.Open(env.Data, env.Keys[i], env.IV, pkeys[i]); Assert.AreEqual(input, result); } } } } finally { for (int i = 0; i < numKeys; i++) { pkeys[i].Dispose(); rsas[i].Dispose(); } } }
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); }
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()); }
public void EnableEncryption(byte[] pSharedSecretKey) { mDecryptCipher = Cipher.AES_128_OFB; mDecryptCipherContext = new CipherContext(mDecryptCipher); mDecryptKey = new byte[16]; mDecryptIV = new byte[16]; Buffer.BlockCopy(pSharedSecretKey, 0, mDecryptKey, 0, 16); Buffer.BlockCopy(pSharedSecretKey, 16, mDecryptIV, 0, 16); mProtocolState = EProtocolState.EncryptedAndCompressed; AdvanceDecryptIV(); }
/// <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)); } }
/// <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); } }