Cryptography加密和解密

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Security.Cryptography;

using System.Text;

using System.IO;



namespace WeiXin.Core

{

    public class Cryptography

    {

        #region SHA1加密

        public static string SHA1_EncryptToFormat(string str)

        {

            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();

            StringBuilder encryptText = new StringBuilder();

            byte[] strRes = Encoding.Default.GetBytes(str);

            strRes = sha1.ComputeHash(strRes);

            foreach (byte iByte in strRes)

            {

                encryptText.AppendFormat("{0:x2}", iByte);

            }

            sha1.Dispose();

            return encryptText.ToString();

        }



        public static string SHA1_EncryptToBase64String(string str)

        {

            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();

            byte[] strRes = Encoding.UTF8.GetBytes(str);

            strRes = sha1.ComputeHash(strRes);

            sha1.Dispose();

            return Convert.ToBase64String(strRes);

        }

        public static byte[] SHA1_Encrypt(byte[] str)

        {

            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();

            byte[] strHash = sha1.ComputeHash(str);

            sha1.Dispose();

            return strHash;

        }

        #endregion





        #region 对称加密



        /// <summary>

        /// 对称算法,机密字符串

        /// </summary>

        /// <param name="Value">要加密的字符串</param>

        /// <returns>covert成ToBase64String类型的加密字符串</returns>

        public static string DataEncrypt(string Value)

        {

            try

            {

                byte[] buffer1 = Convert.FromBase64String("iw/k5uRzMdk=");

                byte[] buffer2 = Convert.FromBase64String("c/DP9HnKryk=");

                DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();//实例化des对称加密标准

                provider1.Key = buffer1;//机密密钥

                provider1.IV = buffer2; //初始化向量         

                ICryptoTransform transform1 = provider1.CreateEncryptor(provider1.Key, provider1.IV);//定义基本的加密转换运算

                byte[] buffer3 = Encoding.UTF8.GetBytes(Value);//将要加密的字符串编码为一个字节序列

                MemoryStream stream1 = new MemoryStream();//内存流

                CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Write);//加密流

                stream2.Write(buffer3, 0, buffer3.Length);

                stream2.FlushFinalBlock();//用缓冲区的当前状态更新基础数据源或储存库,随后清除缓冲区。

                stream2.Close();//关闭当前流并释放与之关联的所有资源(如套接字和文件句柄)

                return Convert.ToBase64String(stream1.ToArray());

            }

            catch

            {

                return "error";

            }

        }



        /// <summary>

        /// 对称算法,解密字符串

        /// </summary>

        /// <param name="Value">需要解密的字符串</param>

        /// <returns>将解密后的流写入字节数组,然后将字节数组解码为一个字符串</returns>

        public static string DataDecrypt(string Value)

        {

            try

            {

                byte[] buffer1 = Convert.FromBase64String("iw/k5uRzMdk=");

                byte[] buffer2 = Convert.FromBase64String("c/DP9HnKryk=");

                DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();

                provider1.Key = buffer1;

                provider1.IV = buffer2;

                ICryptoTransform transform1 = provider1.CreateDecryptor(provider1.Key, provider1.IV);//定义基本解密转换运算

                byte[] buffer3 = Convert.FromBase64String(Value);

                MemoryStream stream1 = new MemoryStream();

                CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Write);//CryptoStreamMode.Write对加密流的写访问

                stream2.Write(buffer3, 0, buffer3.Length);//按照transform1解密算法向流中写入需要解密的字符串

                stream2.FlushFinalBlock();

                stream2.Close();

                return Encoding.UTF8.GetString(stream1.ToArray());//将流写入字节数组,然后将字节数组解码为一个字符串

            }

            catch

            {

                return "error";

            }

        }

        #endregion



    }

}

 

你可能感兴趣的:(Cryptography)