System.Security.Cryptography

本文介绍了一个使用C#进行加密解密的应用实例,涵盖了多种常见的哈希算法(如MD5、SHA1、SHA256等)及非对称加密算法的实现,并展示了如何利用DES和RC2算法进行数据加密。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

System.Security.Cryptography简单应用。

本人对此研究并不多,如有差错,还多请赐教。

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Globalization;

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

        private int oldWith = 0;
        private int oldHight = 0;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Opacity = 0.9;
            oldWith = this.Width;
            oldHight = this.Height;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            byte[] PlainByte = null;
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    PlainByte = Encoding.ASCII.GetBytes(PlainText.Text);
                    break;
                case 1:
                    PlainByte = Encoding.Unicode.GetBytes(PlainText.Text);
                    break;
                case 2:
                    PlainByte = Encoding.BigEndianUnicode.GetBytes(PlainText.Text);
                    break;
                case 3:
                    PlainByte = Encoding.UTF7.GetBytes(PlainText.Text);
                    break;
                case 4:
                    PlainByte = Encoding.UTF8.GetBytes(PlainText.Text);
                    break;
                case 5:
                    PlainByte = Encoding.UTF32.GetBytes(PlainText.Text);
                    break;
                default:
                    MessageBox.Show("Encoding error!");
                    return;
            }

            textBox1.Text = BitConverter.ToString(MD5.Create().ComputeHash(PlainByte)).Replace("-", "");
            textBox2.Text = BitConverter.ToString(SHA1.Create().ComputeHash(PlainByte)).Replace("-", "");
            textBox3.Text = BitConverter.ToString(SHA256.Create().ComputeHash(PlainByte)).Replace("-", "");
            textBox4.Text = BitConverter.ToString(SHA384.Create().ComputeHash(PlainByte)).Replace("-", "");
            textBox5.Text = BitConverter.ToString(SHA512.Create().ComputeHash(PlainByte)).Replace("-", "");

            textBox6.Text = BitConverter.ToString(new DSACryptoServiceProvider().SignData(PlainByte)).Replace("-", "");

            RSAPKCS1SignatureFormatter rsasf = new RSAPKCS1SignatureFormatter(new RSACryptoServiceProvider());
            //rsasf.SetKey(key);
            rsasf.SetHashAlgorithm("SHA1");
            textBox7.Text = BitConverter.ToString(rsasf.CreateSignature(SHA1.Create().ComputeHash(PlainByte))).Replace("-", "");

            ICryptoTransform Encrypt;
            DES des = DES.Create();
            des.GenerateKey();
            des.GenerateIV();
            Encrypt = des.CreateEncryptor();
            textBox8.Text = string.Format("Key:{0}, IV:{1}, Cryptograph:{2}",
                BitConverter.ToString(des.Key),
                BitConverter.ToString(des.IV),
                BitConverter.ToString(Encrypt.TransformFinalBlock(PlainByte, 0, PlainByte.Length))).Replace("-", "");
            RC2 rc2 = RC2.Create();
            rc2.GenerateKey();
            rc2.GenerateIV();
            Encrypt = rc2.CreateEncryptor();
            textBox9.Text = string.Format("Key:{0}, IV:{1}, Cryptograph:{2}",
                BitConverter.ToString(rc2.Key),
                BitConverter.ToString(rc2.IV),
                BitConverter.ToString(Encrypt.TransformFinalBlock(PlainByte, 0, PlainByte.Length))).Replace("-", "");
        }

        private void Form1_ResizeEnd(object sender, EventArgs e)
        {
            this.Height = oldHight;
            if (this.Width < 466) this.Width = 466;
            int v = this.Width - oldWith;
            PlainText.Width += v;
            button1.Width += v;
            foreach (Control i in this.Controls)
                if (i is TextBox) i.Width += v;

            oldWith = this.Width;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值