Decrypt() public method

Decrypts the specified ciphertext
public Decrypt ( byte input, byte key, byte iv ) : byte[]
input byte
key byte
iv byte
return byte[]
Esempio n. 1
0
		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);
			}
		}
Esempio n. 2
0
        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);
                }
            }
        }
Esempio n. 3
0
		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>
        /// 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);
            }
        }