public virtual bool CompareHash(byte[] plaintext, byte[] hashedText)
        {
            if (plaintext == null)
            {
                throw new ArgumentNullException("plainText");
            }
            if (hashedText == null)
            {
                throw new ArgumentNullException("hashedText");
            }
            if (hashedText.Length == 0)
            {
                throw new ArgumentException(Properties.Resources.ExceptionByteArrayValueMustBeGreaterThanZeroBytes, "hashedText");
            }

            bool result = false;

            byte[] hashedPlainText = null;
            byte[] salt            = null;
            try
            {
                salt            = this.ExtractSalt(hashedText);
                hashedPlainText = this.CreateHashWithSalt(plaintext, salt);
            }
            finally
            {
                CryptographyUtility.ZeroOutBytes(salt);
            }
            result = CryptographyUtility.CompareBytes(hashedPlainText, hashedText);

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// <para>Decrypts bytes with the initialized algorithm and key.</para>
        /// </summary>
        /// <param name="encryptedText"><para>The text which you wish to decrypt.</para></param>
        /// <returns><para>The resulting plaintext.</para></returns>
        public virtual byte[] Decrypt(byte[] encryptedText)
        {
            byte[] output = null;
            byte[] data   = this.ExtractIV(encryptedText);

            this.algorithm.Key = Key;

            using (ICryptoTransform transform = this.algorithm.CreateDecryptor())
            {
                //output = this.Transform(transform, data);
                output = CryptographyUtility.Transform(transform, data);
            }

            CryptographyUtility.ZeroOutBytes(this.algorithm.Key);

            return(output);
        }
Exemplo n.º 3
0
        /// <summary>
        /// <para>Encrypts bytes with the initialized algorithm and key.</para>
        /// </summary>
        /// <param name="plaintext"><para>The plaintext in which you wish to encrypt.</para></param>
        /// <returns><para>The resulting ciphertext.</para></returns>
        public virtual byte[] Encrypt(byte[] plaintext)
        {
            byte[] output     = null;
            byte[] cipherText = null;

            this.algorithm.Key = Key;

            using (ICryptoTransform transform = this.algorithm.CreateEncryptor())
            {
                //cipherText = this.Transform(transform, plaintext);
                cipherText = CryptographyUtility.Transform(transform, plaintext);
            }

            output = new byte[IVLength + cipherText.Length];
            Buffer.BlockCopy(this.algorithm.IV, 0, output, 0, IVLength);
            Buffer.BlockCopy(cipherText, 0, output, IVLength, cipherText.Length);

            CryptographyUtility.ZeroOutBytes(this.algorithm.Key);

            return(output);
        }