加密解密,CryptoStream()的使用

一:上圖

二:代碼

主界面代碼ide

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StringEncrypt
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        private void btn_Encrypt_Click(object sender, EventArgs e)
        {
            if (txt_password.Text.Length == 4)//判斷加密密鑰長度是否正確
            {
                try
                {
                    txt_EncryptStr.Text = //調用實例ToEncrypt方法獲得加密後的字符串
                        new Encrypt().ToEncrypt(
                        txt_password.Text, txt_str.Text);
                    //Encrypt P_Encrypt = new Encrypt();
                    //P_Encrypt.ToEncrypt(""
                }
                catch (Exception ex)//捕獲異常
                {
                    MessageBox.Show(ex.Message);//輸出異常信息
                }
            }
            else
            {
                MessageBox.Show("密鑰長度不符!", "提示");//提示用戶輸入密鑰長度不正確
            }
        }
        private void btn_UnEncrypt_Click(object sender, EventArgs e)
        {
            if (txt_password2.Text.Length == 4)//判斷加密密鑰長度是否正確
            {
                try
                {
                    txt_str2.Text = //調用ToDecrypt方法獲得解密後的字符串
                        new Encrypt().ToDecrypt(
                        txt_password2.Text, txt_EncryptStr2.Text);
                }
                catch (Exception ex)//捕獲異常
                {
                    MessageBox.Show(ex.Message);//輸出異常信息
                }
            }
            else
            {
                MessageBox.Show("密鑰長度不符!", "提示");//提示用戶輸入密鑰長度不正確
            }
        }
    }
}

加密解密類代碼加密

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

namespace StringEncrypt
{
    public class Encrypt
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="encryptKey">密鑰</param>
        /// <param name="str">信息</param>
        /// <returns></returns>
        internal string ToEncrypt(string encryptKey, string str)
        {
            try
            {
                byte[] P_byte_key = //將密鑰字符串轉換爲字節序列
                    Encoding.Unicode.GetBytes(encryptKey);
                byte[] P_byte_data = //將字符串轉換爲字節序列
                    Encoding.Unicode.GetBytes(str);
                MemoryStream P_Stream_MS = //建立內存流對象
                    new MemoryStream();
                CryptoStream P_CryptStream_Stream = //建立加密流對象
                    new CryptoStream(P_Stream_MS,new DESCryptoServiceProvider().
                   CreateEncryptor(P_byte_key, P_byte_key),CryptoStreamMode.Write);



                P_CryptStream_Stream.Write(//向加密流中寫入字節序列
                    P_byte_data, 0, P_byte_data.Length);
                P_CryptStream_Stream.FlushFinalBlock();//將數據壓入基礎流,用緩衝區的當前狀態更新基礎數據源或儲存庫,隨後清除緩衝區。
                byte[] P_bt_temp =//從內存流中獲取字節序列
                    P_Stream_MS.ToArray();
                P_CryptStream_Stream.Close();//關閉加密流
                P_Stream_MS.Close();//關閉內存流
                return //方法返回加密後的字符串
                    Convert.ToBase64String(P_bt_temp);
            }
            catch (CryptographicException ce)
            {
                throw new Exception(ce.Message);
            }
        }

        internal string ToDecrypt(string encryptKey, string str)
        {
            try
            {
                byte[] P_byte_key = //將密鑰字符串轉換爲字節序列
                    Encoding.Unicode.GetBytes(encryptKey);
                byte[] P_byte_data = //將加密後的字符串轉換爲字節序列
                    Convert.FromBase64String(str);
                MemoryStream P_Stream_MS =//建立內存流對象並寫入數據
                    new MemoryStream(P_byte_data);
                CryptoStream P_CryptStream_Stream = //建立加密流對象
                    new CryptoStream(P_Stream_MS,new DESCryptoServiceProvider().
                    CreateDecryptor(P_byte_key, P_byte_key),CryptoStreamMode.Read);

                byte[] P_bt_temp = new byte[200];//建立字節序列對象

                MemoryStream P_MemoryStream_temp =//建立內存流對象
                    new MemoryStream();
                int i = 0;//建立記數器
                while ((i = P_CryptStream_Stream.Read(//使用while循環獲得解密數據
                    P_bt_temp, 0, P_bt_temp.Length)) > 0)//(1\從當前流中讀取200個字節-並將它們存儲在 P_bt_temp 中 2\P_bt_temp 中的字節偏移量-從該偏移量開始存儲從當前流中讀取的數據 3\讀入緩衝區中的總字節數)
                {
                    P_MemoryStream_temp.Write(//將解密後的數據放入內存流
                        P_bt_temp, 0, i);
                }
                return //方法返回解密後的字符串
                    Encoding.Unicode.GetString(P_MemoryStream_temp.ToArray());
            }
            catch (CryptographicException ce)
            {
                throw new Exception(ce.Message);
            }
        }
    }
}
相關文章
相關標籤/搜索