Esempio n. 1
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);
        }
Esempio n. 5
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);
        }