C#winform的加密与解密源代码测试成功
来源:互联网 发布:mac搜狗五笔输入法 编辑:程序博客网 时间:2024/05/29 10:47
using System;using System.Text;using System.Windows.Forms;using System.Security.Cryptography;using System.IO;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e)//加密 { DES desjiami = new DES(); textBox2.Text =desjiami.Encrypt(textBox1.Text, "goodmany"); } private void button2_Click(object sender, EventArgs e)//解密 { DES desjiami = new DES(); textBox1.Text = desjiami.Decrypt(textBox2.Text, "goodmany"); } } public class DES { public string Encrypt(string pToEncrypt, string sKey)//加密方法 { DESCryptoServiceProvider desCSP = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); desCSP.Key = ASCIIEncoding.ASCII.GetBytes(sKey); desCSP.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, desCSP.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder SB= new StringBuilder(); foreach (byte b in ms.ToArray()) { SB.AppendFormat("{0:X2}", b); } SB.ToString(); return SB.ToString(); } public string Decrypt(string pToDecrypt, string sKey)//解密 { DESCryptoServiceProvider desCSP = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } desCSP.Key = ASCIIEncoding.ASCII.GetBytes(sKey); desCSP.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, desCSP.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } }}
0 0
- C#winform的加密与解密源代码测试成功
- DES加密与解密源代码
- c# winform 文件内容的加密与解密
- [C#] 加密与解密
- 文件加密移动与解密(winform版
- RC加密解密算法C源代码
- RC加密解密算法C源代码
- 【加密解密】 文件的加密解密 <含源代码>
- 【加密解密】对exe文件的加密解密 <含源代码》
- DES 方式:文件加密与解密的简单测试
- winform 加密 解密
- 使用crypto api的加密解密源代码
- 加密与解密:一个简单的C语言示例
- Hill密码的加密与解密(C语言)
- C语言之文件的加密与解密
- 加密-解密源代码
- Des 加密解密源代码
- 简单的加密与解密
- HTTPServer细节
- sdutacm-走迷宫
- I am OFShare
- 浅拷贝、深拷贝(普、简)、写时拷贝
- CAS实现SSO单点登录原理
- C#winform的加密与解密源代码测试成功
- 【Spring Cloud】Spring Cloud构建微服务架构
- Head first java 第五章(二)
- sdutacm-数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历
- 一起来看看angular的ui-route--VS--ng-route
- htons(), ntohl(), ntohs(),htons() 函数功能
- spring 第一个helloWord程序
- SSO之CAS单点登录实例演示
- Android之Notification的使用与简单介绍