数据加密 凯撒加密 DES对称加密 AES加密的简单应用

数据加密 附资源包DEMO 凯撒加密 DES对称加密 AES加密

在学数据加密解密的时候了解到了各种加密解密的方式

1凯撒加密

输入加密值 key值, 点击加密 会出现加密后的值
以下是凯撒加密的代码段

string inPut = insert.Text;//定义一个字符串,接收用户的输入  
 
            int key = Convert.ToInt32(Kcode.Text) % 26;
            char res;
            foreach (char c in inPut)//遍历字符串  
            {
                if (c != 32)
                {
                    if (!char.IsUpper(c))
                    {
                        if (c + key > 122)
                        {
                            int a1 = c + key - 122;
                            res = Convert.ToChar(96 + a1);
                         }
                        else
                        {
                            res = Convert.ToChar(c + key);
                        }
                        output.AppendText(res.ToString());
                    }
                    if (char.IsUpper(c))
                    {
                        if (c + key > 90)
                        {
                            int a1 = c + key - 90;
                            res = Convert.ToChar(64 + a1);
                        }
                        else
                        {
                            res = Convert.ToChar(c + key);
                        }
                        output.AppendText(res.ToString());
                    }
                }
 
            }
            MessageBox.Show(output.Text);

在这里插入图片描述

2DES 对称加密

1选择文件

在这里插入图片描述

2选择对应的加密方式

在这里插入图片描述

3在输入完成秘钥后将得到指定的密文。

以下是代码片段

string key = textBox8.Text;
            string path = textBox1.Text;
            string path1 = textBox2.Text;
            string content=null;
         
            if (path.Equals(null) || path.Equals(""))
            {
                MessageBox.Show("文件路径不能为空");
                return;
            }
            try
            {
                FileStream myfs = new FileStream(path, FileMode.Open);
 
                //创建读取器
                StreamReader mySr = new StreamReader(myfs,Encoding.Default);
 
                //读取文件所有内容
                content = mySr.ReadToEnd();
                DES des = new DES();
              string str= des.DesEncrypt(content, key);
              MessageBox.Show("加密完成加密前文件内的值为" + content + "加密后的值" + str+"已写入文件");
                //关闭读取器
                mySr.Close();
                //关闭文件流
                myfs.Close();

                 FileStream myfs1 = new FileStream(path1, FileMode.Create);
 
                //创建写入器
                StreamWriter mySw = new StreamWriter(myfs1);
 
                //将录入的内容写入文件
                mySw.Write(str);
 
                //关闭写入器
                mySw.Close();
                //关闭文件流
                myfs1.Close();
            }
            catch (Exception ex)
            { 
                MessageBox.Show(ex.Message);
            }

在这里插入图片描述

在这里插入图片描述
完整代码如下

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 System.IO;
namespace Jiami
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            output.Clear();
            jiami();
        }
        public void jiami()
        {
            string inPut = insert.Text;//定义一个字符串,接收用户的输入  
 
            int key = Convert.ToInt32(Kcode.Text) % 26;
            char res;
            foreach (char c in inPut)//遍历字符串  
            {
                if (c != 32)
                {
                    if (!char.IsUpper(c))
                    {
                        if (c + key > 122)
                        {
                            int a1 = c + key - 122;
                            res = Convert.ToChar(96 + a1);
                        }
                        else
                        {
                            res = Convert.ToChar(c + key);
                        }
                        output.AppendText(res.ToString());
                    }
                    if (char.IsUpper(c))
                    {
                        if (c + key > 90)
                        {
                            int a1 = c + key - 90;
                            res = Convert.ToChar(64 + a1);
                        }
                        else
                        {
                            res = Convert.ToChar(c + key);
                        }
                        output.AppendText(res.ToString());
                     }
                }
            }
            MessageBox.Show(output.Text);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = @"F:\visual2010\text-input.txt";
            textBox2.Text = @"F:\visual2010\text-output.txt";
            textBox4.Text = @"F:\visual2010\text-output.txt";
            textBox3.Text = @"F:\visual2010\text-output2.txt";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string key = textBox8.Text;
            string path = textBox1.Text;
            string path1 = textBox2.Text;
            string content=null;
         
            if (path.Equals(null) || path.Equals(""))
            {
                MessageBox.Show("文件路径不能为空");
                return;
            }
            try
            {
                FileStream myfs = new FileStream(path, FileMode.Open);
 
                //创建读取器
                StreamReader mySr = new StreamReader(myfs,Encoding.Default);
 
                //读取文件所有内容
                content = mySr.ReadToEnd();
                DES des = new DES();
              string str= des.DesEncrypt(content, key);
              MessageBox.Show("加密完成加密前文件内的值为" + content + "加密后的值" + str+"已写入文件");
                //关闭读取器
                mySr.Close();
                //关闭文件流
                myfs.Close();

                 FileStream myfs1 = new FileStream(path1, FileMode.Create);
 
                //创建写入器
                StreamWriter mySw = new StreamWriter(myfs1);
 
                //将录入的内容写入文件
                mySw.Write(str);
 
                //关闭写入器
                mySw.Close();
                //关闭文件流
                myfs1.Close();
            }
            catch (Exception ex)
            { 
                MessageBox.Show(ex.Message);
            }
        }

        private void groupBox4_Enter(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.ofdMain.ShowDialog();
            textBox1.Text = ofdMain.FileName;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.ofdMain.ShowDialog();
            textBox2.Text = ofdMain.FileName;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.ofdMain.ShowDialog();
            textBox4.Text = ofdMain.FileName;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.ofdMain.ShowDialog();
            textBox3.Text = ofdMain.FileName;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string path1 = textBox3.Text;
            string key = textBox8.Text;
            string path = textBox4.Text;
            string content = null;

            if (path.Equals(null) || path.Equals(""))
            {
                MessageBox.Show("文件路径不能为空");
                return;
            }
            try
            {
                FileStream myfs = new FileStream(path, FileMode.Open);
                //创建读取器
                StreamReader mySr = new StreamReader(myfs, Encoding.Default);
                //读取文件所有内容
                content = mySr.ReadToEnd();
                DES des = new DES();
                string str = des.DesDecrypt(content, key);
                MessageBox.Show("解密完成解密前文件内的值为" + content + "解密后的值为" + str + "已写入文件");
                //关闭读取器
                mySr.Close();
                //关闭文件流
                myfs.Close();
                FileStream myfs1 = new FileStream(path1, FileMode.Create);
                //创建写入器
                StreamWriter mySw = new StreamWriter(myfs1);
                //将录入的内容写入文件
                mySw.Write(str);
                //关闭写入器
                mySw.Close();
                //关闭文件流
                myfs1.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void button9_Click(object sender, EventArgs e)
        {
            string path = textBox1.Text;
            string path1 = textBox2.Text;
            string content = null;

            if (path.Equals(null) || path.Equals(""))
            {
                MessageBox.Show("文件路径不能为空");
                return;
            }
            try
            {
                FileStream myfs = new FileStream(path, FileMode.Open);
                //创建读取器
                StreamReader mySr = new StreamReader(myfs, Encoding.Default);
                //读取文件所有内容
                content = mySr.ReadToEnd();
                Rijndael rijndel = new Rijndael();
                rijndel.SymmetricMethod(textBox6.Text);
                string str = rijndel.Encrypto(content);
                MessageBox.Show("加密成功,加密后的信息为" + str);
                mySr.Close();
                //关闭文件流
                myfs.Close();
                FileStream myfs1 = new FileStream(path1, FileMode.Create);
                //创建写入器
                StreamWriter mySw = new StreamWriter(myfs1);
                //将录入的内容写入文件
                mySw.Write(str);
                //关闭写入器
                mySw.Close();
                //关闭文件流
                myfs1.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void button5_Click_1(object sender, EventArgs e)
        {
            
            string path = textBox4.Text;
            string path1 = textBox3.Text;
            string content = null;
            if (path.Equals(null) || path.Equals(""))
            {
                MessageBox.Show("文件路径不能为空");
                return;
            }
            try
            {
                FileStream myfs = new FileStream(path, FileMode.Open);
                //创建读取器
                StreamReader mySr = new StreamReader(myfs, Encoding.Default);
                //读取文件所有内容
                content = mySr.ReadToEnd();
                Rijndael rijndel = new Rijndael();
                rijndel.SymmetricMethod(textBox6.Text);
                string str = rijndel.Decrypto(content);
                MessageBox.Show("解密成功,解密后的信息为" + str);
                mySr.Close();
                //关闭文件流
                myfs.Close();

                FileStream myfs1 = new FileStream(path1, FileMode.Create);

                //创建写入器
                StreamWriter mySw = new StreamWriter(myfs1);

                //将录入的内容写入文件
                mySw.Write(str);

                //关闭写入器
                mySw.Close();
                //关闭文件流
                myfs1.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

若要下载 点击下面蓝色字体跳转。
加密源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RJGCWJH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值