Exemplo n.º 1
0
        /// <summary>
        /// 对称加密后的字符串,返回解密后的结果
        /// </summary>
        /// <param name="input">要解密的内容</param>
        /// <param name="key">加密用密钥</param>
        /// <param name="iv">加密用的初始化向量</param>
        /// <param name="encryptSymmetryType">加密算法</param>
        /// <returns>解密后的结果</returns>
        public static string DecryptString(this string input, byte[] key, byte[] iv, EncryptSymmetryType encryptSymmetryType)
        {
            if (input == null || input.Length <= 0)
            {
                throw new ArgumentNullException("input");
            }
            if (key == null || key.Length <= 0)
            {
                throw new ArgumentNullException("key");
            }
            if (iv == null || iv.Length <= 0)
            {
                throw new ArgumentNullException("IV");
            }

            byte[]             cipherBytes        = Convert.FromBase64String(input);
            string             plaintext          = string.Empty;
            SymmetricAlgorithm symmetricAlgorithm = null;

            switch (encryptSymmetryType)
            {
            case EncryptSymmetryType.DES:
            case EncryptSymmetryType.Rijandel:
            case EncryptSymmetryType.TripleDES:
            case EncryptSymmetryType.RC2:
            {
                symmetricAlgorithm     = Encrypt.GetSymmetricAlgorithmBySmartEncryptSymmetryType(encryptSymmetryType);
                symmetricAlgorithm.Key = key;
                symmetricAlgorithm.IV  = iv;
                ICryptoTransform decryptor = symmetricAlgorithm.CreateDecryptor(symmetricAlgorithm.Key, symmetricAlgorithm.IV);
                using (MemoryStream memoryStream = new MemoryStream(cipherBytes))
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader streamReader = new StreamReader(cryptoStream))
                            plaintext = streamReader.ReadToEnd();
                    }
                }

                if (symmetricAlgorithm != null)
                {
                    symmetricAlgorithm.Clear();
                }
            }

            break;
            }

            return(plaintext);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 输入要对称加密的原文,返回加密后的结果,输出密钥和初初始化变量
        /// </summary>
        /// <param name="input">要加密的内容</param>
        /// <param name="key">加密的密钥</param>
        /// <param name="iv">加密的初始化变量</param>
        /// <param name="bitLength">密钥的字节长度</param>
        /// <param name="encryptSymmetryType">要采用的对称加密算法</param>
        /// <returns>加密后的结果</returns>
        private static byte[] EncryptString(this string input, out byte[] key, out byte[] iv, int bitLength, EncryptSymmetryType encryptSymmetryType)
        {
            if (input == null || input.Length <= 0)
            {
                throw new ArgumentNullException("input");
            }
            SymmetricAlgorithm symmetricAlgorithm = null;

            byte[] plainBytes = null;
            key = null;
            iv  = null;
            MemoryStream memoryStream = new MemoryStream();

            switch (encryptSymmetryType)
            {
            case EncryptSymmetryType.DES:
            case EncryptSymmetryType.Rijandel:
            case EncryptSymmetryType.TripleDES:
            case EncryptSymmetryType.RC2:
            {
                symmetricAlgorithm = Encrypt.GetSymmetricAlgorithmBySmartEncryptSymmetryType(encryptSymmetryType);
                if (bitLength != int.MinValue && !symmetricAlgorithm.ValidKeySize(bitLength))
                {
                    throw new Exception("密钥大小对当前算法无效");
                }
                if (bitLength != int.MinValue && symmetricAlgorithm.ValidKeySize(bitLength))
                {
                    symmetricAlgorithm.KeySize = bitLength;
                }
                symmetricAlgorithm.GenerateKey();
                symmetricAlgorithm.GenerateIV();
                key = symmetricAlgorithm.Key;
                iv  = symmetricAlgorithm.IV;
                ICryptoTransform decryptor = symmetricAlgorithm.CreateEncryptor(symmetricAlgorithm.Key, symmetricAlgorithm.IV);
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
                    {
                        streamWriter.Write(input);
                    }
                }

                if (symmetricAlgorithm != null)
                {
                    symmetricAlgorithm.Clear();
                }
            }

            break;
            }

            plainBytes = memoryStream.ToArray();
            return(plainBytes);
        }
Exemplo n.º 3
0
 /// <summary>
 /// 输入要对称加密的原文,返回加密后的结果,输出密钥和初初始化变量
 /// </summary>
 /// <param name="input">要加密的内容</param>
 /// <param name="key">加密的密钥</param>
 /// <param name="iv">加密的初始化变量</param>
 /// <param name="bitLength">密钥的字节长度</param>
 /// <param name="encryptSymmetryType">要采用的对称加密算法</param>
 /// <returns>加密后的结果</returns>
 private static string EncryptString(this string input, out string key, out string iv, int bitLength, EncryptSymmetryType encryptSymmetryType)
 {
     byte[] resultBuffer;
     byte[] keyBytes;
     byte[] ivandBytes;
     resultBuffer = EncryptString(input, out keyBytes, out ivandBytes, bitLength, encryptSymmetryType);
     key          = Convert.ToBase64String(keyBytes);
     iv           = Convert.ToBase64String(ivandBytes);
     return(Convert.ToBase64String(resultBuffer));
 }
Exemplo n.º 4
0
 /// <summary>
 /// 输入要对称加密的原文,返回加密后的结果,输出密钥和初初始化变量
 /// </summary>
 /// <param name="input">要加密的内容</param>
 /// <param name="key">加密的密钥</param>
 /// <param name="iv">加密的初始化变量</param>
 /// <param name="encryptSymmetryType">要采用的对称加密算法</param>
 /// <returns>加密后的结果</returns>
 public static byte[] EncryptString(this string input, out byte[] key, out byte[] iv, EncryptSymmetryType encryptSymmetryType)
 {
     byte[] resultBuffer;
     byte[] keyBytes;
     byte[] ivandBytes;
     resultBuffer = EncryptString(input, out keyBytes, out ivandBytes, int.MinValue, encryptSymmetryType);
     key          = keyBytes;
     iv           = ivandBytes;
     return(resultBuffer);
 }
Exemplo n.º 5
0
        /// <summary>
        /// 输入要对称加密的原文,返回加密后的结果,输出密钥和初初始化变量
        /// 密钥字节长度采用默认值
        /// </summary>
        /// <param name="input">要加密的内容</param>
        /// <param name="key">加密的密钥</param>
        /// <param name="iv">加密的初始化变量</param>
        /// <param name="encryptSymmetryType">要采用的对称加密算法</param>
        /// <returns>加密后的结果</returns>
        public static string EncryptString(this string input, out string key, out string iv, EncryptSymmetryType encryptSymmetryType)
        {
            string result;
            string keY;
            string iV;

            result = EncryptString(input, out keY, out iV, int.MinValue, encryptSymmetryType);
            key    = keY;
            iv     = iV;
            return(result);
        }
Exemplo n.º 6
0
 /// <summary>
 /// 用输入的密钥和初始化向量,对输入的文本进行加密,返回加密后的结果
 /// 密钥与初始话向量不可为汉语
 /// 密钥字节长度采用默认值
 /// </summary>
 /// <param name="input">要加密的内容</param>
 /// <param name="key">加密用的密钥</param>
 /// <param name="iv">加密用的初始化变量</param>
 /// <param name="encryptSymmetryType">要采用的对称加密算法</param>
 /// <returns>加密后的结果</returns>
 private static byte[] EncryptStringByKeyIV(this string input, byte[] key, byte[] iv, EncryptSymmetryType encryptSymmetryType)
 {
     return(EncryptStringByKeyIV(input, key, iv, int.MinValue, encryptSymmetryType));
 }
Exemplo n.º 7
0
 /// <summary>
 /// 用输入的密钥和初始化向量,对输入的文本进行加密,返回加密后的结果
 /// 密钥与初始话向量不可为汉语
 /// </summary>
 /// <param name="input">要加密的内容</param>
 /// <param name="key">加密用的密钥</param>
 /// <param name="iv">加密用的初始化变量</param>
 /// <param name="bitLength">密钥的字节长度</param>
 /// <param name="encryptSymmetryType">要采用的对称加密算法</param>
 /// <returns>加密后的结果</returns>
 private static string EncryptStringByKeyIV(this string input, string key, string iv, int bitLength, EncryptSymmetryType encryptSymmetryType)
 {
     if (input == null || input.Length <= 0)
     {
         throw new ArgumentNullException("input");
     }
     if (key == null || key.Length <= 0)
     {
         throw new ArgumentNullException("key");
     }
     if (iv == null || iv.Length <= 0)
     {
         throw new ArgumentNullException("iv");
     }
     byte[] resultBuffer;
     byte[] keyBytes   = Encoding.Default.GetBytes(key);
     byte[] ivandBytes = Encoding.Default.GetBytes(iv);
     resultBuffer = EncryptStringByKeyIV(input, keyBytes, ivandBytes, (bitLength == int.MinValue ? keyBytes.Length : bitLength), encryptSymmetryType);
     return(Convert.ToBase64String(resultBuffer));
 }
Exemplo n.º 8
0
 /// <summary>
 /// 用输入的密钥和初始化向量,对输入的文本进行加密,返回加密后的结果
 /// 密钥与初始话向量不可为汉语
 /// 密钥字节长度采用默认值
 /// </summary>
 /// <param name="input">要加密的内容</param>
 /// <param name="key">加密用的密钥</param>
 /// <param name="iv">加密用的初始化变量</param>
 /// <param name="encryptSymmetryType">要采用的对称加密算法</param>
 /// <returns>加密后的结果</returns>
 private static string EncryptStringByKeyIV(this string input, string key, string iv, EncryptSymmetryType encryptSymmetryType)
 {
     return(EncryptStringByKeyIV(input, key, iv, int.MinValue, encryptSymmetryType));
 }
Exemplo n.º 9
0
 /// <summary>
 /// 对称加密输入结果,返回解密后的结果
 /// </summary>
 /// <param name="input">要解密的内容</param>
 /// <param name="key">加密用密钥</param>
 /// <param name="iv">加密用的初始化向量</param>
 /// <param name="encryptSymmetryType">加密算法</param>
 /// <returns>解密后的结果</returns>
 public static string DecryptString(this string input, string key, string iv, EncryptSymmetryType encryptSymmetryType)
 {
     byte[] keY = Convert.FromBase64String(key);
     byte[] iV  = Convert.FromBase64String(iv);
     return(DecryptString(input, keY, iV, encryptSymmetryType));
 }