Exemple #1
0
        /// <summary>
        /// 生成随机密钥
        /// </summary>
        /// <returns></returns>
        public static byte[] CreateKeyBytes(enmSymmetricAlgorithmType enmType)
        {
            SymmetricAlgorithm desCrypto = null;

            switch (enmType)
            {
            case enmSymmetricAlgorithmType.DES:
                desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
                return(desCrypto.Key);

            case enmSymmetricAlgorithmType.RIJNDAEL:
                desCrypto         = Rijndael.Create();
                desCrypto.KeySize = 256;
                return(desCrypto.Key);
            }
            return(null);
        }
Exemple #2
0
        /// <summary>
        /// 生成加密服务对象
        /// </summary>
        /// <param name="isNew">是否生成具备新密钥的加密服务对象;true 为是;false 为否,为否时将按照默认密钥生成对象。</param>
        /// <returns></returns>
        public SymmetricAlgorithm Create(enmSymmetricAlgorithmType enmType, bool blnIsNew)
        {
            SymmetricAlgorithm desProvide = null;

            byte[] byteKey = null;
            switch (enmType)
            {
            case enmSymmetricAlgorithmType.DES:
                desProvide = new DESCryptoServiceProvider();
                if (blnIsNew)
                {
                    byteKey = clsSymmetricAlgorithm.CreateKeyBytes(enmType);
                }
                else
                {
                    byteKey = new byte[8];
                    Array.ConstrainedCopy(this.KEY, 0, byteKey, 0, 8);
                }
                desProvide.IV  = byteKey;
                desProvide.Key = byteKey;
                break;

            case enmSymmetricAlgorithmType.RIJNDAEL:
                desProvide = Rijndael.Create();
                if (blnIsNew)
                {
                    byte[] byteNewKey = clsSymmetricAlgorithm.CreateKeyBytes(enmType);
                    byteKey = new byte[16];
                    Array.ConstrainedCopy(byteNewKey, 0, byteKey, 0, 16);
                }
                else
                {
                    byteKey = new byte[16];
                    Array.ConstrainedCopy(this.KEY, 0, byteKey, 0, 16);
                }
                desProvide.IV  = byteKey;
                desProvide.Key = this.KEY;
                break;
            }
            return(desProvide);
        }
Exemple #3
0
 /// <summary>
 /// 跟默认密钥加密
 /// </summary>
 /// <param name="strText">明文</param>
 /// <returns></returns>
 public string Encrypt(string strText, enmSymmetricAlgorithmType enmType)
 {
     return(this.Encrypt(strText, this.Create(enmType, false)));
 }
Exemple #4
0
 /// <summary>
 /// 生成随机密钥
 /// </summary>
 /// <returns></returns>
 public static string CreateKeyString(enmSymmetricAlgorithmType enmType)
 {
     return(Convert.ToBase64String(clsSymmetricAlgorithm.CreateKeyBytes(enmType)));
 }