Csharp(C#.net)中如何对字符串进行TripleDESCryptoServiceProvider加密,确保程序安全

第1部分:应用场景

我们在开发应用的过程中,

少不了使用应用程序的配置文件,

因为这样可以增加程序的灵活性,

但是这一做法存在一个危险,

就是被有心者且稍微了解配置文件的人篡改,

或者获取到我们需要保密的信息,

基于此,我们只能对这些字符串进行加密,

而在使用的时候解密,

这样既能保证程序的灵活性又不降低安全性。

第2部分:源代码


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;
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //调用加密过程
        private void Button1_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                string MyStringBefor = TextBox1.Text;
                MyDES rr = new MyDES();
                string MyStringAfter = rr.CodeInface(MyStringBefor);
                TextBox2.Text = MyStringAfter;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //调用解密过程
        private void Button2_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                string MyStringAfter = TextBox2.Text;
                MyDES rr = new MyDES();
                string MyStringBefor = rr.CodeOutface(MyStringAfter, false);
                TextBox1.Text = MyStringBefor;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }

    public class MyDES
    {
        private byte[] I = new byte[25];
        private byte[] Iv = new byte[25];
        internal string CodeInface(string intxt)
        {
            TripleDESCryptoServiceProvider cryptoProvider = default(TripleDESCryptoServiceProvider);
            MemoryStream ms = default(MemoryStream);
            CryptoStream cs = default(CryptoStream);
            StreamWriter sw = default(StreamWriter);
            cryptoProvider = new TripleDESCryptoServiceProvider();
            ms = new MemoryStream();
            cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(I, Iv), CryptoStreamMode.Write);
            sw = new StreamWriter(cs);
            sw.Write(intxt);
            sw.Flush();
            cs.FlushFinalBlock();
            ms.Flush();
            return Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));
        }
        internal string CodeOutface(string outtxt, bool bCry)
        {
            if (string.IsNullOrEmpty(outtxt) & !bCry)
                return "";
            byte[] cryTxt = null;
            ASCIIEncoding textConverter = default(ASCIIEncoding);
            TripleDESCryptoServiceProvider cryptoProvider = default(TripleDESCryptoServiceProvider);
            MemoryStream ms = default(MemoryStream);
            CryptoStream cs = default(CryptoStream);
            StreamReader sw = default(StreamReader);
            textConverter = new ASCIIEncoding();
            cryptoProvider = new TripleDESCryptoServiceProvider();
            cryTxt = Convert.FromBase64String(outtxt);
            ms = new MemoryStream(cryTxt);
            cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(I, Iv), CryptoStreamMode.Read);
            sw = new StreamReader(cs);
            return sw.ReadToEnd();
        }
        //定义位,可以修改,相当于更改加密方法
        public MyDES()
        {
            I = new byte[] { 149, 19, 93, 156, 178, 4, 220, 232, 65, 167, 43, 80, 26, 230, 155, 112, 70, 94, 11, 204, 2, 35, 184, 97 };
            Iv = new byte[] { 152, 123, 206, 179, 236, 200, 167, 43, 242, 66, 92, 83, 124, 57, 19, 13, 145, 23, 200, 58, 173, 10, 121, 1 };
        }
    }
}

第3部分:结论

通过上面这个源代码,能够很好的实现加密、解密过程

并且加密解密过程可以通过修改位来更改你的加密、解密方式,

具有很好的灵活性,希望各位一起探讨

vb.net的方法参考《visual basic(vb.net)中如何对字符串进行TripleDESCryptoServiceProvider加密,确保程序安全

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容