Exemplo n.º 1
0
        /// <summary>
        /// 进行DES加密。
        /// </summary>
        /// <param name="pToEncrypt">要加密的字符串。</param>
        /// <param name="sKey">密钥,且必须为8位。</param>
        /// <returns>以Base64格式返回的加密字符串。</returns>
        public static bool Encrypt(string pToEncrypt, string sKey, out string encryptString)
        {
            //如果出错,出错信息将储存于encryptString返回
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                if (!des.ValidKeySize(sKey.Length))
                {
                    encryptString = "Error:Key is not valid size";
                    return(false);
                }

                bool   bl             = des.ValidKeySize(sKey.Length);
                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV  = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                try
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    encryptString = Convert.ToBase64String(ms.ToArray());
                    ms.Close();
                    return(true);
                }
                catch (Exception e)
                {
                    encryptString = e.ToString();
                    return(false);
                }
            }
        }
Exemplo n.º 2
0
        public static string Decrypt(string cryptedString, ref byte[] bytes)
        {
            if (string.IsNullOrEmpty(cryptedString))
            {
                throw new ArgumentNullException("Error Decrypt.");
            }
            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();

            if (dESCryptoServiceProvider.ValidKeySize(bytes.Length))
            {
                throw new ArgumentNullException("Encrypt: Key not valid.");
            }
            MemoryStream stream       = new MemoryStream(Convert.FromBase64String(cryptedString));
            CryptoStream stream2      = new CryptoStream(stream, dESCryptoServiceProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
            StreamReader streamReader = new StreamReader(stream2);

            return(streamReader.ReadToEnd());
        }
Exemplo n.º 3
0
        public static string Encrypt(string originalString)
        {
            if (string.IsNullOrEmpty(originalString))
            {
                throw new ArgumentNullException("Error Encrypt.");
            }
            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            MemoryStream             memoryStream             = new MemoryStream();

            if (dESCryptoServiceProvider.ValidKeySize(bytes.Length))
            {
                return("0");
            }
            CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
            StreamWriter streamWriter = new StreamWriter(cryptoStream);

            streamWriter.Write(originalString);
            streamWriter.Flush();
            cryptoStream.FlushFinalBlock();
            streamWriter.Flush();
            return(Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length, Base64FormattingOptions.None));
        }
Exemplo n.º 4
0
        public static bool ValidateKeySize(EncryptionAlgorithm algID, int Lenght)
        {
            switch (algID)
            {
            case EncryptionAlgorithm.DES:
                DES des = new DESCryptoServiceProvider();
                return(des.ValidKeySize(Lenght));

            case EncryptionAlgorithm.Rc2:
                RC2 rc = new RC2CryptoServiceProvider();
                return(rc.ValidKeySize(Lenght));

            case EncryptionAlgorithm.Rijndael:
                Rijndael rj = new RijndaelManaged();
                return(rj.ValidKeySize(Lenght));

            case EncryptionAlgorithm.TripleDes:
                TripleDES tDes = new TripleDESCryptoServiceProvider();
                return(tDes.ValidKeySize(Lenght));

            default:
                throw new CryptographicException("Algorithm " + algID + " Not Supported!");
            }
        }