C#利用异或算法实现加密解密

 更新时间:2023年01月03日 09:32:27   作者:芝麻粒儿  
这篇文章主要为大家详细介绍了C#如何利用异或算法实现加密解密的功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下

实践过程

效果

代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (textBox1.Text != "")
            {
                textBox2.Text = (Convert.ToInt32(textBox1.Text) ^ 123).ToString();
            }
        }
        catch { }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            if (textBox2.Text != "")
            {
                textBox1.Text = (Convert.ToInt32(textBox2.Text) ^ 123).ToString();
            }
        }
        catch { }
    }
}
partial class Form1
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.label3 = new System.Windows.Forms.Label();
        this.button2 = new System.Windows.Forms.Button();
        this.button1 = new System.Windows.Forms.Button();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.label2 = new System.Windows.Forms.Label();
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.textBox2);
        this.groupBox1.Controls.Add(this.label3);
        this.groupBox1.Controls.Add(this.button2);
        this.groupBox1.Controls.Add(this.button1);
        this.groupBox1.Controls.Add(this.textBox1);
        this.groupBox1.Controls.Add(this.label2);
        this.groupBox1.Location = new System.Drawing.Point(6, 4);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(282, 112);
        this.groupBox1.TabIndex = 6;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "加密设置";
        // 
        // textBox2
        // 
        this.textBox2.Location = new System.Drawing.Point(115, 49);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(143, 21);
        this.textBox2.TabIndex = 6;
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Location = new System.Drawing.Point(24, 52);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(89, 12);
        this.label3.TabIndex = 7;
        this.label3.Text = "加密后的数字:";
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(183, 76);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(75, 25);
        this.button2.TabIndex = 3;
        this.button2.Text = "解密";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(101, 76);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 25);
        this.button1.TabIndex = 2;
        this.button1.Text = "加密";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(115, 19);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(143, 21);
        this.textBox1.TabIndex = 1;
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(24, 22);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(89, 12);
        this.label2.TabIndex = 5;
        this.label2.Text = "加密前的数字:";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(296, 123);
        this.Controls.Add(this.groupBox1);
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "Form1";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "使用异或算法对数字进行加密解密";
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Label label3;
}

到此这篇关于C#利用异或算法实现加密解密的文章就介绍到这了,更多相关C#加密解密内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#调用OutLokk实现发送邮件

    C#调用OutLokk实现发送邮件

    这篇文章主要为大家详细介绍了如何利用C#调用OutLokk实现发送邮件的功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
    2022-12-12
  • C#中判断某类型是否可以进行隐式类型转换

    C#中判断某类型是否可以进行隐式类型转换

    在我们采用反射动态调用一些方法时,常常涉及到类型的转换,直接判断类型是否相符有时不能判断调用方法是否合适
    2013-04-04
  • 使用C#连接SQL Server的详细图文教程

    使用C#连接SQL Server的详细图文教程

    初学者学习上位机开发遇到数据库连接不上,是很常见的情况,可能会以各种形式呈现出来,下面这篇文章主要给大家介绍了关于使用C#连接SQL Server的详细图文教程,需要的朋友可以参考下
    2023-02-02
  • C#开发WinForm项目实现HTML编辑器

    C#开发WinForm项目实现HTML编辑器

    这篇文章介绍了C#开发WinForm项目实现HTML编辑器的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C#中HashTable的定义与使用方法

    C#中HashTable的定义与使用方法

    Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似keyvalue的键值对,所以Hashtable可以支持任何类型的keyvalue键值对
    2012-12-12
  • c#获取季度时间实例代码(季度的第一天)

    c#获取季度时间实例代码(季度的第一天)

    这篇文章主要介绍了c#获取季度时间:季度的第一天、季度的最后一天等功能,大家参考使用吧
    2013-12-12
  • DevExpress实现禁用TreeListNode CheckBox的方法

    DevExpress实现禁用TreeListNode CheckBox的方法

    这篇文章主要介绍了DevExpress实现禁用TreeListNode CheckBox的方法,在项目开发中有应用价值,需要的朋友可以参考下
    2014-08-08
  • C#中IDispose接口的实现及为何这么实现详解

    C#中IDispose接口的实现及为何这么实现详解

    这篇文章主要给大家介绍了关于C#中IDispose接口的实现及为何这么实现的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-05-05
  • C#语音识别用法实例

    C#语音识别用法实例

    这篇文章主要介绍了C#语音识别用法,实例分析了C#利用微软操作系统自动的语音识别功能,读取信息的技巧,需要的朋友可以参考下
    2015-01-01
  • 使用VS2010 C#开发ActiveX控件(下),完整代码打包下载

    使用VS2010 C#开发ActiveX控件(下),完整代码打包下载

    我们介绍了开发、打包、发布、使用ActiveX控件的全过程。在演示程序中,我们没有调用串口通信和读卡器Dll程序,由于我们读卡器的原始Dll是使用其它语言进行开发的,对C#来说,是非托管代码,因此我们还需要在代码级别进行非托管代码的安全性设置
    2011-05-05

最新评论