Ejemplo n.º 1
0
        /// <summary>
        /// DES¼ÓÃÜ×Ö·û´®
        /// </summary>
        /// <param name="encryptString">´ý¼ÓÃܵÄ×Ö·û´®</param>
        /// <param name="encryptKey">¼ÓÃÜÃÜÔ¿,ÒªÇóΪ8λ</param>
        /// <returns>¼ÓÃܳɹ¦·µ»Ø¼ÓÃܺóµÄ×Ö·û´®,ʧ°Ü·µ»ØÔ´´®</returns>
        public static string Encrypt(string encryptString, string encryptKey)
        {
            encryptKey = JosonString.GetSubString(encryptKey, 8, "");
            encryptKey = encryptKey.PadRight(8, ' ');
            byte[] rgbKey                    = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            byte[] rgbIV                     = Keys;
            byte[] inputByteArray            = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider dCSP    = new DESCryptoServiceProvider();
            MemoryStream             mStream = new MemoryStream();
            CryptoStream             cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);

            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return(Convert.ToBase64String(mStream.ToArray()));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// DES¼ÓÃÜ×Ö·û´®
        /// </summary>
        /// <param name="encryptString">´ý¼ÓÃܵÄ×Ö·û´®</param>
        /// <param name="encryptKey">¼ÓÃÜÃÜÔ¿,ÒªÇóΪ8λ</param>
        /// <returns>¼ÓÃܳɹ¦·µ»Ø¼ÓÃܺóµÄ×Ö·û´®,ʧ°Ü·µ»ØÔ´´®</returns>
        public static string Encrypt(string encryptString, string encryptKey)
        {
            encryptKey = JosonString.GetSubString(encryptKey, 32, "");
            encryptKey = encryptKey.PadRight(32, ' ');

            RijndaelManaged rijndaelProvider = new RijndaelManaged();

            rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
            rijndaelProvider.IV  = Keys;
            ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();

            byte[] inputData     = Encoding.UTF8.GetBytes(encryptString);
            byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);

            return(Convert.ToBase64String(encryptedData));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// DES½âÃÜ×Ö·û´®
        /// </summary>
        /// <param name="decryptString">´ý½âÃܵÄ×Ö·û´®</param>
        /// <param name="decryptKey">½âÃÜÃÜÔ¿,ÒªÇóΪ8λ,ºÍ¼ÓÃÜÃÜÔ¿Ïàͬ</param>
        /// <returns>½âÃܳɹ¦·µ»Ø½âÃܺóµÄ×Ö·û´®,ʧ°Ü·µÔ´´®</returns>
        public static string Decrypt(string decryptString, string decryptKey)
        {
            try
            {
                decryptKey = JosonString.GetSubString(decryptKey, 8, "");
                decryptKey = decryptKey.PadRight(8, ' ');
                byte[] rgbKey                 = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV                  = Keys;
                byte[] inputByteArray         = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();

                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return(Encoding.UTF8.GetString(mStream.ToArray()));
            }
            catch
            {
                return("");
            }
        }