C#实现简单的RSA非对称加密算法示例
程序员文章站
2023-12-09 17:27:03
本文实例讲述了c#实现简单的rsa非对称加密算法。分享给大家供大家参考,具体如下:
界面控件
namespace rsa算法
{
partial cla...
本文实例讲述了c#实现简单的rsa非对称加密算法。分享给大家供大家参考,具体如下:
界面控件
namespace rsa算法 { 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.strbox = new system.windows.forms.textbox(); this.label1 = new system.windows.forms.label(); this.pubkeybox = new system.windows.forms.textbox(); this.label2 = new system.windows.forms.label(); this.prvkeybox = new system.windows.forms.textbox(); this.label3 = new system.windows.forms.label(); this.encrypebox = new system.windows.forms.textbox(); this.label4 = new system.windows.forms.label(); this.label5 = new system.windows.forms.label(); this.str2box = new system.windows.forms.textbox(); this.crypebtn = new system.windows.forms.button(); this.suspendlayout(); // // strbox // this.strbox.location = new system.drawing.point(115, 12); this.strbox.name = "strbox"; this.strbox.size = new system.drawing.size(258, 21); this.strbox.tabindex = 0; // // label1 // this.label1.autosize = true; this.label1.location = new system.drawing.point(11, 18); this.label1.name = "label1"; this.label1.size = new system.drawing.size(77, 12); this.label1.tabindex = 1; this.label1.text = "加密前的明文"; // // pubkeybox // this.pubkeybox.location = new system.drawing.point(115, 78); this.pubkeybox.multiline = true; this.pubkeybox.name = "pubkeybox"; this.pubkeybox.size = new system.drawing.size(258, 74); this.pubkeybox.tabindex = 2; // // label2 // this.label2.autosize = true; this.label2.location = new system.drawing.point(11, 87); this.label2.name = "label2"; this.label2.size = new system.drawing.size(29, 12); this.label2.tabindex = 3; this.label2.text = "公钥"; // // prvkeybox // this.prvkeybox.location = new system.drawing.point(115, 158); this.prvkeybox.multiline = true; this.prvkeybox.name = "prvkeybox"; this.prvkeybox.size = new system.drawing.size(258, 128); this.prvkeybox.tabindex = 4; // // label3 // this.label3.autosize = true; this.label3.location = new system.drawing.point(13, 167); this.label3.name = "label3"; this.label3.size = new system.drawing.size(101, 12); this.label3.tabindex = 5; this.label3.text = "密钥(包含私钥)"; // // encrypebox // this.encrypebox.location = new system.drawing.point(115, 292); this.encrypebox.name = "encrypebox"; this.encrypebox.size = new system.drawing.size(258, 21); this.encrypebox.tabindex = 6; // // label4 // this.label4.autosize = true; this.label4.location = new system.drawing.point(14, 299); this.label4.name = "label4"; this.label4.size = new system.drawing.size(29, 12); this.label4.tabindex = 7; this.label4.text = "密文"; // // label5 // this.label5.autosize = true; this.label5.location = new system.drawing.point(14, 329); this.label5.name = "label5"; this.label5.size = new system.drawing.size(77, 12); this.label5.tabindex = 8; this.label5.text = "解密后的明文"; // // str2box // this.str2box.location = new system.drawing.point(115, 320); this.str2box.name = "str2box"; this.str2box.size = new system.drawing.size(258, 21); this.str2box.tabindex = 9; // // crypebtn // this.crypebtn.location = new system.drawing.point(117, 43); this.crypebtn.name = "crypebtn"; this.crypebtn.size = new system.drawing.size(104, 23); this.crypebtn.tabindex = 10; this.crypebtn.text = "执行加密解密"; this.crypebtn.usevisualstylebackcolor = true; this.crypebtn.click += new system.eventhandler(this.crypebtn_click); // // form1 // this.autoscaledimensions = new system.drawing.sizef(6f, 12f); this.autoscalemode = system.windows.forms.autoscalemode.font; this.clientsize = new system.drawing.size(385, 353); this.controls.add(this.crypebtn); this.controls.add(this.str2box); this.controls.add(this.label5); this.controls.add(this.label4); this.controls.add(this.encrypebox); this.controls.add(this.label3); this.controls.add(this.prvkeybox); this.controls.add(this.label2); this.controls.add(this.pubkeybox); this.controls.add(this.label1); this.controls.add(this.strbox); this.name = "form1"; this.text = "rsa非对称加密解密"; this.resumelayout(false); this.performlayout(); } #endregion private system.windows.forms.textbox strbox; private system.windows.forms.label label1; private system.windows.forms.textbox pubkeybox; private system.windows.forms.label label2; private system.windows.forms.textbox prvkeybox; private system.windows.forms.label label3; private system.windows.forms.textbox encrypebox; private system.windows.forms.label label4; private system.windows.forms.label label5; private system.windows.forms.textbox str2box; private system.windows.forms.button crypebtn; } }
rsa代码
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.security.cryptography; namespace rsa算法 { public partial class form1 : form { public form1() { initializecomponent(); } private void crypebtn_click(object sender, eventargs e) { //创建rsa加密算法服务提供者 rsacryptoserviceprovider rsa = new rsacryptoserviceprovider(); //显示公钥和私钥 pubkeybox.text = rsa.toxmlstring(false); prvkeybox.text = rsa.toxmlstring(true); //执行加密 byte[] encrypebytes = rsa.encrypt(encoding.utf8.getbytes(strbox.text), true); encrypebox.text = encoding.utf8.getstring(encrypebytes); //执行解密 byte[] decrypebytes = rsa.decrypt(encrypebytes, true); str2box.text = encoding.utf8.getstring(decrypebytes); } } }
运行效果:
ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:
文字在线加密解密工具(包含aes、des、rc4等):
md5在线加密工具:
http://tools.jb51.net/password/createmd5password
在线散列/哈希算法加密工具:
在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:
在线sha1/sha224/sha256/sha384/sha512加密工具:
更多关于c#相关内容还可查看本站专题:《c#加密与解密算法与技巧总结》、《c#窗体操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程》
希望本文所述对大家c#程序设计有所帮助。