码迷,mamicode.com
首页 > Windows程序 > 详细

C#原生加密方法: System.Security.Cryptography.CryptoStream DataSet加密解密

时间:2016-04-07 13:34:10      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:

采用16位密钥形式加密,把数据 dataset或文本转换为二进制流,然后进行加密解密。代码如下:

技术分享
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CryptoHelperLib
{
    public class CryptoHelper
    {

        // 对称加密算法提供器
        private ICryptoTransform encryptor;     // 加密器对象
        private ICryptoTransform decryptor;     // 解密器对象
        //public string key = "ABCDEFGHIJKLMNOP";//长度16
        //public static byte[] DESKey = new byte[] { 11, 23, 93, 102, 72, 41, 18, 12 };
        //public static byte[] DESIV = new byte[] { 75, 158, 46, 97, 78, 57, 17, 36 };
        private const int BufferSize = 1024;
        public CryptoHelper(string algorithmName, string key)
        {
            SymmetricAlgorithm provider = SymmetricAlgorithm.Create(algorithmName);
            provider.Key = Encoding.UTF8.GetBytes(key);

            provider.IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            encryptor = provider.CreateEncryptor();
            decryptor = provider.CreateDecryptor();
        }

        public CryptoHelper(string key) : this("TripleDES", key) { }


        public MemoryStream EncryptMemoryStream(MemoryStream itemStream)
        {
            // 创建空的密文流
            MemoryStream encryptedStream = new MemoryStream();

            CryptoStream cryptoStream =
                new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write);

            // 将明文流写入到buffer中
            // 将buffer中的数据写入到cryptoStream中
            int bytesRead = 0;
            byte[] buffer = new byte[BufferSize];
            itemStream.Position = 0;
            do
            {
                bytesRead = itemStream.Read(buffer, 0, BufferSize);
                cryptoStream.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);

            cryptoStream.FlushFinalBlock();
            byte[] buffer2 = encryptedStream.ToArray();
            string encryptedText = Convert.ToBase64String(buffer2);
            return encryptedStream;
        }
        public Stream EncryptByte(byte[] data)
        {
            MemoryStream clearStream = new MemoryStream(data);
            clearStream.Position = 0;
            // 创建空的密文流
            MemoryStream encryptedStream = new MemoryStream();

            CryptoStream cryptoStream =
                new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write);

            // 将明文流写入到buffer中
            // 将buffer中的数据写入到cryptoStream中
            int bytesRead = 0;
            byte[] buffer = new byte[BufferSize];
            do
            {
                bytesRead = clearStream.Read(buffer, 0, BufferSize);
                cryptoStream.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);
            cryptoStream.FlushFinalBlock();
            // 获取加密后的文本
            byte[] buffer2 = encryptedStream.ToArray();
            string encryptedText = Convert.ToBase64String(buffer2);
            return encryptedStream;
        }


        // 加密算法
        public string EncryptText(string clearText)
        {
            // 创建明文流
            byte[] clearBuffer = Encoding.UTF8.GetBytes(clearText);
            MemoryStream clearStream = new MemoryStream(clearBuffer);

            // 创建空的密文流
            MemoryStream encryptedStream = new MemoryStream();

            CryptoStream cryptoStream =
                new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write);

            // 将明文流写入到buffer中
            // 将buffer中的数据写入到cryptoStream中
            int bytesRead = 0;
            byte[] buffer = new byte[BufferSize];
            do
            {
                bytesRead = clearStream.Read(buffer, 0, BufferSize);
                cryptoStream.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);

            cryptoStream.FlushFinalBlock();

            // 获取加密后的文本
            buffer = encryptedStream.ToArray();
            string encryptedText = Convert.ToBase64String(buffer);
            return encryptedText;
        }


        public MemoryStream DecryptMemoryStream(MemoryStream encryptedStream)
        {
            MemoryStream clearStream = new MemoryStream();
            CryptoStream cryptoStream =
                new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read);

            int bytesRead = 0;
            byte[] buffer = new byte[BufferSize];

            do
            {
                bytesRead = cryptoStream.Read(buffer, 0, BufferSize);
                clearStream.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);

            buffer = clearStream.GetBuffer();
            MemoryStream clearStreamResult = new MemoryStream(buffer);
            return clearStreamResult;
        }


        //
        // 解密算法, http://www.51testing.com/html/67/n-220867-4.html
        // 解密算法
        public string DecryptText(string encryptedText)
        {
            byte[] encryptedBuffer = Convert.FromBase64String(encryptedText);
            Stream encryptedStream = new MemoryStream(encryptedBuffer);

            MemoryStream clearStream = new MemoryStream();
            CryptoStream cryptoStream =
                new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read);

            int bytesRead = 0;
            byte[] buffer = new byte[BufferSize];

            do
            {
                bytesRead = cryptoStream.Read(buffer, 0, BufferSize);
                clearStream.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);

            buffer = clearStream.GetBuffer();
            string clearText =
                Encoding.UTF8.GetString(buffer, 0, (int)clearStream.Length);

            return clearText;
        }

        public static string Encrypt(string clearText, string key)
        {
            CryptoHelper helper = new CryptoHelper(key);
            return helper.EncryptText(clearText);
        }

        public static string Decrypt(string encryptedText, string key)
        {
            CryptoHelper helper = new CryptoHelper(key);
            return helper.DecryptText(encryptedText);
        }
    }
}
View Code

调用示例:

    // string key="ABCDEFGHIJKLMNOP"; //16位字符串

        public byte[] DataSetToBytes(DataSet ds)
        {
            DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
            MemoryStream dataStream = new MemoryStream();
            MemoryStream dataStream2 = new MemoryStream();
            ds.WriteXml(dataStream, XmlWriteMode.WriteSchema);
            
            CryptoHelperLib.CryptoHelper cryhelper = new CryptoHelperLib.CryptoHelper(key);
            dataStream2= cryhelper.EncryptMemoryStream(dataStream);
            byte[] buf = dataStream2.ToArray();

            return buf;
        }

        public DataSet DataSetFromBytes(byte[] buf)
        {

            MemoryStream dataStream = new MemoryStream(buf);
            MemoryStream dataStream2 = new MemoryStream();
            CryptoHelperLib.CryptoHelper cryhelper = new CryptoHelperLib.CryptoHelper(key);
            dataStream2 = cryhelper.DecryptMemoryStream(dataStream);
            dataStream2.Position = 0;
            DataSet ds = new DataSet();
            ds.ReadXml(dataStream2);
            return ds;
        }

 

C#原生加密方法: System.Security.Cryptography.CryptoStream DataSet加密解密

标签:

原文地址:http://www.cnblogs.com/guo2001china/p/5363049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!