c#字符串加密与解密封装

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace 字符串加密解密封装
{

    /// <summary>
    /// 字符串加密
    /// </summary>
    class  Encryption_Decryption
    {
        
        static byte[] Key{get;set;}//加密的密钥
        static byte[] Iv{get;set;} //加密的初始化矢量,初始化加密函数的变量。

        static Encryption_Decryption()
        {
            //算法签名,这里我用的是TripleDES加密算法,TripleDESCryptoServiceProvider构造方法同时会初始化一组随机的key和IV。
            TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();           
            Key=tDESalg.Key;
            Iv=tDESalg.IV;
        }

        /// <summary>
        /// 字符串加密
        /// </summary>
        /// <param name="encStr">需要加密的字符串</param>
        /// <returns>一个已经加密的字符串的二进制流</returns>
        public static byte[] EncStream(string encStr)
        {
            //将用户传进来的字符串转换成编码为UTF8的二进制流
            byte[] encStrStream = Encoding.UTF8.GetBytes(encStr);
            

            //建立一个MemoryStream,这里面存放加密后的数据流
            MemoryStream mStream = new MemoryStream();

            //使用MemoryStream和key,IV新建一个CryptoStream对象  
            CryptoStream cStream = new CryptoStream(mStream, new TripleDESCryptoServiceProvider().CreateEncryptor(Key, Iv), CryptoStreamMode.Write);

            //将加密后的字节流写入到MemoryStream
            cStream.Write(encStrStream, 0, encStrStream.Length);

            //把缓冲区中的最后状态更新到MemoryStream,并清除cStream的缓存区
            cStream.FlushFinalBlock();

            // 把解密后的数据流转成字节流
            byte[] ret = mStream.ToArray();

            //关闭两个streams

            cStream.Close();
            mStream.Close();

            return ret;
        }
    

        /// <summary>
        /// 对加密二进制流进行解密
        /// </summary>
        /// <param name="byteStream">需要解密的二进制流</param>
        /// <returns>返回一个已经解密的字符串</returns>
        public static string DecStrStream(byte[] byteStream)
        {
            // 建立一个MemoryStream,这里面存放加密后的数据流
            MemoryStream msDecrypt = new MemoryStream(byteStream);

            // 使用MemoryStream 和key、IV新建一个CryptoStream 对象
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, new TripleDESCryptoServiceProvider().CreateDecryptor(Key, Iv), CryptoStreamMode.Read);

            // 根据密文byte[]的长度(可能比加密前的明文长),新建一个存放解密后明文的byte[]
            byte[] DecryptDataArray = new byte[byteStream.Length];

            // 把解密后的数据读入到DecryptDataArray
            csDecrypt.Read(DecryptDataArray, 0, DecryptDataArray.Length);

            string DecStr = Encoding.UTF8.GetString(DecryptDataArray);

            msDecrypt.Close();

            csDecrypt.Close();

            return DecStr;
        }
    }


    class Program
    {
        //测试加密,解密都成功
        static void Main(string[] args)
        {
            //加密测试
            byte[] s = Encryption_Decryption.EncStream("你好");

            foreach (byte b in s)
            {
                Console.WriteLine(b);
            }

            //解密测试
            string ss = Encryption_Decryption.DecStrStream(s);
            Console.WriteLine(ss);

            Console.ReadKey();
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值