一:上图

二:代码

主界面代码

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 StringEncrypt
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
} private void btn_Encrypt_Click(object sender, EventArgs e)
{
if (txt_password.Text.Length == )//判断加密密钥长度是否正确
{
try
{
txt_EncryptStr.Text = //调用实例ToEncrypt方法得到加密后的字符串
new Encrypt().ToEncrypt(
txt_password.Text, txt_str.Text);
//Encrypt P_Encrypt = new Encrypt();
//P_Encrypt.ToEncrypt(""
}
catch (Exception ex)//捕获异常
{
MessageBox.Show(ex.Message);//输出异常信息
}
}
else
{
MessageBox.Show("密钥长度不符!", "提示");//提示用户输入密钥长度不正确
}
}
private void btn_UnEncrypt_Click(object sender, EventArgs e)
{
if (txt_password2.Text.Length == )//判断加密密钥长度是否正确
{
try
{
txt_str2.Text = //调用ToDecrypt方法得到解密后的字符串
new Encrypt().ToDecrypt(
txt_password2.Text, txt_EncryptStr2.Text);
}
catch (Exception ex)//捕获异常
{
MessageBox.Show(ex.Message);//输出异常信息
}
}
else
{
MessageBox.Show("密钥长度不符!", "提示");//提示用户输入密钥长度不正确
}
}
}
}

加密解密类代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text; namespace StringEncrypt
{
public class Encrypt
{
/// <summary>
/// 加密
/// </summary>
/// <param name="encryptKey">密钥</param>
/// <param name="str">信息</param>
/// <returns></returns>
internal string ToEncrypt(string encryptKey, string str)
{
try
{
byte[] P_byte_key = //将密钥字符串转换为字节序列
Encoding.Unicode.GetBytes(encryptKey);
byte[] P_byte_data = //将字符串转换为字节序列
Encoding.Unicode.GetBytes(str);
MemoryStream P_Stream_MS = //创建内存流对象
new MemoryStream();
CryptoStream P_CryptStream_Stream = //创建加密流对象
new CryptoStream(P_Stream_MS,new DESCryptoServiceProvider().
CreateEncryptor(P_byte_key, P_byte_key),CryptoStreamMode.Write); P_CryptStream_Stream.Write(//向加密流中写入字节序列
P_byte_data, , P_byte_data.Length);
P_CryptStream_Stream.FlushFinalBlock();//将数据压入基础流,用缓冲区的当前状态更新基础数据源或储存库,随后清除缓冲区。
byte[] P_bt_temp =//从内存流中获取字节序列
P_Stream_MS.ToArray();
P_CryptStream_Stream.Close();//关闭加密流
P_Stream_MS.Close();//关闭内存流
return //方法返回加密后的字符串
Convert.ToBase64String(P_bt_temp);
}
catch (CryptographicException ce)
{
throw new Exception(ce.Message);
}
} internal string ToDecrypt(string encryptKey, string str)
{
try
{
byte[] P_byte_key = //将密钥字符串转换为字节序列
Encoding.Unicode.GetBytes(encryptKey);
byte[] P_byte_data = //将加密后的字符串转换为字节序列
Convert.FromBase64String(str);
MemoryStream P_Stream_MS =//创建内存流对象并写入数据
new MemoryStream(P_byte_data);
CryptoStream P_CryptStream_Stream = //创建加密流对象
new CryptoStream(P_Stream_MS,new DESCryptoServiceProvider().
CreateDecryptor(P_byte_key, P_byte_key),CryptoStreamMode.Read); byte[] P_bt_temp = new byte[];//创建字节序列对象 MemoryStream P_MemoryStream_temp =//创建内存流对象
new MemoryStream();
int i = ;//创建记数器
while ((i = P_CryptStream_Stream.Read(//使用while循环得到解密数据
P_bt_temp, , P_bt_temp.Length)) > )//(1\从当前流中读取200个字节-并将它们存储在 P_bt_temp 中 2\P_bt_temp 中的字节偏移量-从该偏移量开始存储从当前流中读取的数据 3\读入缓冲区中的总字节数)
{
P_MemoryStream_temp.Write(//将解密后的数据放入内存流
P_bt_temp, , i);
}
return //方法返回解密后的字符串
Encoding.Unicode.GetString(P_MemoryStream_temp.ToArray());
}
catch (CryptographicException ce)
{
throw new Exception(ce.Message);
}
}
}
}

加密解密,CryptoStream()的使用的更多相关文章

  1. 利用CryptoStream进行加密解密

    public class DBSecurity { //sKey sIV这两个自己随意设定,不能外泄 private const string sKey = "11,22,33,43,34, ...

  2. C#原生加密方法: System.Security.Cryptography.CryptoStream DataSet加密解密

    采用16位密钥形式加密,把数据 dataset或文本转换为二进制流,然后进行加密解密.代码如下: using System; using System.Collections.Generic; usi ...

  3. .NET和JAVA中BYTE的区别以及JAVA中“DES/CBC/PKCS5PADDING” 加密解密在.NET中的实现

    场景:java 作为客户端调用已有的一个.net写的server的webservice,输入string,返回字节数组. 问题:返回的值不是自己想要的,跟.net客户端直接调用总是有差距 分析:平台不 ...

  4. 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解

    原文地址:http://docode.top/Article/Detail/10003 目录: 1..Net(C#)平台下Des加密解密源代码 2..Net(C#)平台下Aes加密解密源代码 3..N ...

  5. .Net(c#)加密解密之Aes和Des

    .Net(c#)加密解密工具类: /// <summary> /// .Net加密解密帮助类 /// </summary> public class NetCryptoHelp ...

  6. C# 常用加密解密帮助类

    public static class EncryptUtil { #region MD5加密 /// <summary> /// MD5加密 /// </summary> p ...

  7. .Net中的加密解密

    返回博客列表 转 .Net中的加密解密 李朝强 发布时间: 2015/11/23 12:55 阅读: 33 收藏: 3 点赞: 0 评论: 0 在一些比较重要的应用场景中,通过网络传递数据需要进行加密 ...

  8. DES加密解密

    加密后生成Base64字符串,并去除'='字符. 加密后替换掉'+',这样加密后的字符串可以作为url参数传递. using System; using System.IO; using System ...

  9. C#的3DES加密解密算法

    C#类如下: using System; using System.Collections.Generic; using System.Text; using System.Security.Cryp ...

随机推荐

  1. CentOS7下配置Openvpn 2.3.12

    1.下载安装包 #wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.09.tar.gz#wget http://swupdate. ...

  2. 外部式css样式,写在单独的一个文件中

    外部式css样式(也可称为外联式)就是把css代码写一个单独的外部文件中,这个css样式文件以“.css”为扩展名,在<head>内(不是在<style>标签内)使用<l ...

  3. Asp与Asp.Net的区别

    今天在网上看到一位朋友问asp与asp.net的区别.编辑本人也是从asp转型到.net来的,几年了,几乎都忘记了asp的存在,也说不出它们之间的区别,因为感觉两者是根本就没有联系,非要说有联系,那就 ...

  4. LA 6856 Circle of digits 解题报告

    题目链接 先用后缀数组给串排好序.dc3 O(n) 二分答案+贪心check 答案的长度len=(n+k-1)/k 如果起点为i长为len串大于当前枚举的答案,i的长度取len-1 从起点判断k个串的 ...

  5. 【POJ2752】【KMP】Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  6. javascript中的基本数据类型

    在javascipt中有五大基本数据类型,列表如下: 1.数字 他们又又包括(正负整数,浮点数)十进制数,十六进制数与八进制数,指数和特殊数值NaN,(Infinity,-Infinity)正负无穷 ...

  7. WPF之核心面板(容器)控件简单介绍

    一.Canvas 1.官方表述:定义一个区域,在该区域中可以使用相对于该区域的坐标显式定位子元素. 2.对于canvas 的元素的位置,是靠控件的大小及Canvas.Top.Canvas.Left.C ...

  8. 我的css reset

    @charset "utf-8"; /*reset*/ body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,f ...

  9. Java常量和变量

    1.Java运行原理 编译+解释型语言: 程序代码经编译后转换为一种称为java字节码(.class文件)的中间语言 file.java--->Class.class java虚拟机JVM将字节 ...

  10. Python新手学习基础之初识python——与众不同2

    看完了Python的缩进,现在来看看Python的标识符.引号和注释. 标识符 关于Python的标识符,其实不是与众不同,只是有一定的规则. 标识符是编程时使用的名字.在Python中,标识符有几点 ...