Wraps the EVP_CIPHER object.
Inheritance: OpenSSL.Core.Base
Example #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);
			}
		}
Example #2
0
		/// <summary>
		/// Calls OPENSSL_malloc() and initializes the buffer using EVP_CIPHER_CTX_init()
		/// </summary>
		/// <param name="cipher"></param>
		public CipherContext(Cipher cipher)
		{
		    this.ptr = Native.EVP_CIPHER_CTX_new();
		    this.owner = true;
            this.OnNewHandle(this.ptr);
			this.cipher = cipher;
		}
Example #3
0
        public DecryptCipherContext(Cipher cipher, byte[] key, byte[] iv, int padding)
        {
            this.ptr = Native.EVP_CIPHER_CTX_new();
            this.owner = true;
            this.OnNewHandle(this.ptr);
            this.cipher = cipher;

            byte[] real_key = SetupKey(key);
            byte[] real_iv = SetupIV(iv);

            Native.ExpectSuccess(Native.EVP_DecryptInit_ex(
                    this.ptr, this.cipher.Handle, IntPtr.Zero, null, null));

            Native.ExpectSuccess(Native.EVP_CIPHER_CTX_set_key_length(this.ptr, real_key.Length));

            if( padding >= 0 )
                Native.ExpectSuccess(Native.EVP_CIPHER_CTX_set_padding(this.ptr, padding));

            if ((this.cipher.Flags & Native.EVP_CIPH_MODE) == Native.EVP_CIPH_STREAM_CIPHER)
            {
                for (int i = 0; i < Math.Min(real_key.Length, iv.Length); i++)
                {
                    real_key[i] ^= iv[i];
                }

                Native.ExpectSuccess(Native.EVP_DecryptInit_ex(
                    this.ptr, this.cipher.Handle, IntPtr.Zero, real_key, null));
            }
            else
            {
                Native.ExpectSuccess(Native.EVP_DecryptInit_ex(
                    this.ptr, this.cipher.Handle, IntPtr.Zero, real_key, real_iv));
            }
        }
 private void initKey(string password, string method)
 {
     string k = method + ":" + password;
     if (cachedKeys.ContainsKey(k))
     {
         key = cachedKeys[k];
         cipher = cachedCiphers[k];
         return;
     }
     cipher = Cipher.CreateByName(method);
     if (cipher == null)
     {
         throw new NullReferenceException();
     }
     byte[] passbuf = System.Text.Encoding.UTF8.GetBytes(password); ;
     key =  new byte[cipher.KeyLength];
     byte[] iv = new byte[cipher.IVLength];
     Native.EVP_BytesToKey(cipher.Handle, MessageDigest.MD5.Handle, null, passbuf, passbuf.Length, 1, key, iv);
     cachedKeys[k] = key;
     cachedCiphers[k] = cipher;
 }
Example #5
0
		public void TestEncryptDecryptWithSalt(Cipher cipher)
		{
			if (cipher == Cipher.Null)
				Assert.Ignore();

			var inputMsg = "This is a message";
			var input = Encoding.ASCII.GetBytes(inputMsg);
			var salt = Encoding.ASCII.GetBytes("salt");
			var secret = Encoding.ASCII.GetBytes("Password!");

			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;
				var 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);
			}
		}
Example #6
0
		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);
				}
			}
		}
Example #7
0
 /// <summary>
 /// Calls OPENSSL_malloc() and initializes the buffer using EVP_CIPHER_CTX_init()
 /// </summary>
 /// <param name="cipher"></param>
 public CipherContext(Cipher cipher)
     : base(Native.OPENSSL_malloc(Marshal.SizeOf(typeof(EVP_CIPHER_CTX))), true)
 {
     Native.EVP_CIPHER_CTX_init(this.ptr);
     this.cipher = cipher;
 }
Example #8
0
		/// <summary>
		/// Calls PEM_write_bio_RSAPrivateKey()
		/// </summary>
		/// <param name="bio"></param>
		/// <param name="enc"></param>
		/// <param name="passwd"></param>
		/// <param name="arg"></param>
		public void WritePrivateKey(BIO bio, Cipher enc, PasswordHandler passwd, object arg)
		{
			PasswordThunk thunk = new PasswordThunk(passwd, arg);
			Native.ExpectSuccess(Native.PEM_write_bio_RSAPrivateKey(
				bio.Handle,
				this.ptr,
				enc == null ? IntPtr.Zero : enc.Handle,
				null,
				0,
				thunk.Callback,
				IntPtr.Zero));
		}
Example #9
0
		/// <summary>
		/// Calls PEM_write_bio_PKCS8PrivateKey
		/// </summary>
		/// <param name="bp"></param>
		/// <param name="cipher"></param>
		/// <param name="handler"></param>
		/// <param name="arg"></param>
		public void WritePrivateKey(BIO bp, Cipher cipher, PasswordHandler handler, object arg)
		{
			var thunk = new PasswordThunk(handler, null);
			Native.ExpectSuccess(Native.PEM_write_bio_PKCS8PrivateKey(bp.Handle, ptr, cipher.Handle, IntPtr.Zero, 0, thunk.Callback, IntPtr.Zero));
		}
Example #10
0
		/// <summary>
		/// Calls PEM_write_bio_PKCS8PrivateKey
		/// </summary>
		/// <param name="bp"></param>
		/// <param name="cipher"></param>
		/// <param name="password"></param>
		public void WritePrivateKey(BIO bp, Cipher cipher, string password)
		{
			PasswordCallback callback = new PasswordCallback(password);
			WritePrivateKey(bp, cipher, callback.OnPassword, null);
		}
Example #11
0
 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();
 }
Example #12
0
        /// <summary>
        /// Calls PEM_write_bio_PKCS8PrivateKey
        /// </summary>
        /// <param name="bp"></param>
        /// <param name="cipher"></param>
        /// <param name="handler"></param>
        /// <param name="arg"></param>
        public void WritePrivateKey(BIO bp, Cipher cipher, PasswordHandler handler, object arg)
        {
            PasswordThunk thunk = new PasswordThunk(handler, null);

            Native.ExpectSuccess(Native.PEM_write_bio_PKCS8PrivateKey(bp.Handle, this.ptr, cipher.Handle, IntPtr.Zero, 0, thunk.Callback, IntPtr.Zero));
        }
Example #13
0
        /// <summary>
        /// Calls PEM_write_bio_PKCS8PrivateKey
        /// </summary>
        /// <param name="bp"></param>
        /// <param name="cipher"></param>
        /// <param name="password"></param>
        public void WritePrivateKey(BIO bp, Cipher cipher, string password)
        {
            PasswordCallback callback = new PasswordCallback(password);

            WritePrivateKey(bp, cipher, callback.OnPassword, null);
        }