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);
        }
        protected void AddSaltToHash(byte[] salt, ref byte[] hash)
        {
            if (!this.saltEnabled)
            {
                return;
            }

            hash = CryptographyUtility.CombineBytes(salt, hash);
        }
Ejemplo n.º 3
0
        public virtual string Encrypt(string plaintext)
        {
            byte[] plainTextBytes = UnicodeEncoding.Unicode.GetBytes(plaintext);

            byte[] cipherTextBytes = this.Encrypt(plainTextBytes);

            CryptographyUtility.GetRandomBytes(plainTextBytes);

            return(Convert.ToBase64String(cipherTextBytes));
        }
        public virtual string CreateHash(string plaintext)
        {
            byte[] plainTextBytes = UnicodeEncoding.Unicode.GetBytes(plaintext);
            byte[] resultBytes    = this.CreateHash(plainTextBytes);

            // 清空该字节数组.
            CryptographyUtility.GetRandomBytes(plainTextBytes);

            return(Convert.ToBase64String(resultBytes));
        }
        public virtual bool CompareHash(string plaintext, string hashedText)
        {
            byte[] plainTextBytes  = UnicodeEncoding.Unicode.GetBytes(plaintext);
            byte[] hashedTextBytes = Convert.FromBase64String(hashedText);

            bool result = this.CompareHash(plainTextBytes, hashedTextBytes);

            CryptographyUtility.GetRandomBytes(plainTextBytes);

            return(result);
        }
        protected void AddSaltToPlainText(ref byte[] salt, ref byte[] plaintext)
        {
            if (!this.saltEnabled)
            {
                return;
            }

            if (salt == null)
            {
                salt = CryptographyUtility.GetRandomBytes(this.saltLength);
            }

            plaintext = CryptographyUtility.CombineBytes(salt, plaintext);
        }
Ejemplo n.º 7
0
        public virtual string Decrypt(string ciphertextBase64)
        {
            if (string.IsNullOrEmpty(ciphertextBase64))
            {
                throw new ArgumentException(Properties.Resources.ExceptionNullOrEmptyString, "ciphertextBase64");
            }

            byte[] cipherTextBytes = Convert.FromBase64String(ciphertextBase64);
            byte[] decryptedBytes  = this.Decrypt(cipherTextBytes);

            string decryptedString = UnicodeEncoding.Unicode.GetString(decryptedBytes);

            CryptographyUtility.GetRandomBytes(decryptedBytes);

            return(decryptedString);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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);
        }