C# 程序简单实现加密解密程序代码

1 篇文章 0 订阅
0 篇文章 0 订阅

本实例是利用异或运算简单的实现对数字的加密功能,加密的过程是需要被加密的数字与加密数字之间的异或 ,解密则是加密的结果与加密数字进行异或的过程 ,具体实现过程 如下:

首先新建一个C# 窗体应用程序,构建窗体界面如下图所示:

具体的窗体控件在工具里选择,此处不再详解,在建立的控件里注意每个控件的Name,在写代码的时候很重要,防止变量搞错,具体的变成代码如下
  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 jiami
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int Num, Key;//定义两个值类型变量
            if (int.TryParse(textBox1.Text, out Num) && int.TryParse(textBox2.Text, out Key))//判断输入是否是数值
            {
                textBox4.Text = (Num ^ Key).ToString();//文本框1的内容与文本框2的内容进行异或运算,实现加密,将加密值赋给文本框4
            }
            else { MessageBox.Show("请输入数值", "出现错误!"); }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            int Key, Jmh;
            if (int.TryParse(textBox4.Text, out Key)&&int.TryParse (textBox2 .Text ,out Jmh ))
            { textBox3.Text = (Jmh ^ Key).ToString(); }//加密后的数值与加密数值(文本框2的数值)进行异或运算
            else { MessageBox.Show("请输入数值", "出现错误!"); }
        }
      
    }
}
理清不同文本框对应的Name,如果需要的话,可以自己定义控件的Name属性。

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供一个简单的示例代码来实现AES加密解密程序。 首先,您需要在您的项目中添加System.Security.Cryptography命名空间,然后可以使用Aes类来执行加密解密操作。 以下是一个简单的代码示例,它使用AES加密算法来加密解密字符串: ```csharp using System; using System.Security.Cryptography; using System.Text; namespace AesEncryptionDemo { public class AesEncryption { private static readonly byte[] Salt = Encoding.ASCII.GetBytes("This is my salt value"); public static string Encrypt(string plainText, string password) { byte[] encryptedBytes; using (Aes aes = Aes.Create()) { Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, Salt); aes.Key = key.GetBytes(aes.KeySize / 8); aes.IV = key.GetBytes(aes.BlockSize / 8); ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length); encryptor.Dispose(); } return Convert.ToBase64String(encryptedBytes); } public static string Decrypt(string encryptedText, string password) { byte[] encryptedBytes = Convert.FromBase64String(encryptedText); string plainText; using (Aes aes = Aes.Create()) { Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, Salt); aes.Key = key.GetBytes(aes.KeySize / 8); aes.IV = key.GetBytes(aes.BlockSize / 8); ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); plainText = Encoding.UTF8.GetString(decryptedBytes); decryptor.Dispose(); } return plainText; } } } ``` 在上面的示例中,我们使用了Rfc2898DeriveBytes类来生成密钥和IV向量。这个类使用密码和盐值作为输入,并生成一个密钥和一个IV向量。在加密解密过程中,我们使用这个密钥和IV向量来执行加密解密操作。 您可以在您的窗体应用程序中使用这个示例代码来加密解密用户输入的数据。例如,您可以在一个文本框中接收用户输入的数据,然后使用Encrypt方法加密它,并将结果显示在另一个文本框中。同样地,您可以使用Decrypt方法将用户输入的密文解密,并显示结果在一个文本框中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序小K

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

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

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

打赏作者

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

抵扣说明:

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

余额充值