加密解密,CryptoStream()的使用
一:上图

二:代码
主界面代码
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()的使用的更多相关文章
- 利用CryptoStream进行加密解密
public class DBSecurity { //sKey sIV这两个自己随意设定,不能外泄 private const string sKey = "11,22,33,43,34, ...
- C#原生加密方法: System.Security.Cryptography.CryptoStream DataSet加密解密
采用16位密钥形式加密,把数据 dataset或文本转换为二进制流,然后进行加密解密.代码如下: using System; using System.Collections.Generic; usi ...
- .NET和JAVA中BYTE的区别以及JAVA中“DES/CBC/PKCS5PADDING” 加密解密在.NET中的实现
场景:java 作为客户端调用已有的一个.net写的server的webservice,输入string,返回字节数组. 问题:返回的值不是自己想要的,跟.net客户端直接调用总是有差距 分析:平台不 ...
- 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解
原文地址:http://docode.top/Article/Detail/10003 目录: 1..Net(C#)平台下Des加密解密源代码 2..Net(C#)平台下Aes加密解密源代码 3..N ...
- .Net(c#)加密解密之Aes和Des
.Net(c#)加密解密工具类: /// <summary> /// .Net加密解密帮助类 /// </summary> public class NetCryptoHelp ...
- C# 常用加密解密帮助类
public static class EncryptUtil { #region MD5加密 /// <summary> /// MD5加密 /// </summary> p ...
- .Net中的加密解密
返回博客列表 转 .Net中的加密解密 李朝强 发布时间: 2015/11/23 12:55 阅读: 33 收藏: 3 点赞: 0 评论: 0 在一些比较重要的应用场景中,通过网络传递数据需要进行加密 ...
- DES加密解密
加密后生成Base64字符串,并去除'='字符. 加密后替换掉'+',这样加密后的字符串可以作为url参数传递. using System; using System.IO; using System ...
- C#的3DES加密解密算法
C#类如下: using System; using System.Collections.Generic; using System.Text; using System.Security.Cryp ...
随机推荐
- C# ashx与html的联合使用
本文将介绍ashx和html的联合使用方法,尽管目前流行mvc,但handler一般处理程序还是ASP.NET的基础知识,结合html页面,做出来的网页绝对比WebForm的简洁和效率高. 首先,概要 ...
- PL/SQL 记录集合IS TABLE OF的使用
在PL/SQL代码块中使用select into 赋值的话,有可能返回的是一个结果集.此时,如果使用基本类型或自定义的记录类型,将会报错. 因此,需要定义一个变量,是某种类型的集合.下面以一个基于表的 ...
- Quartz.NET配置(Log4net)
最近有个任务关于服务调度,想起以前看过Quartz.NET调度任务非常棒. 今天小试Quartz.NET,前面配置Quartz.NET很轻松,控制台也输出了.但是想配合Log4net来做日志文件,怎么 ...
- [笔记]SD卡相关资料
ESD静电放电模块 我知道的flash分为两种NOR flash和NAND flash,NOR falsh容量一般为1~16M用于单片机代码存储,NAND flash最小的是8M最大的现在听说有90G ...
- oracle 的 startup,startup mount,startup nomount之间的区别
startup,startup mount,startup nomount之间的区别 startup nomount选项:(读初始化参数文件,启动实例) startup nomount选项启动实例 ...
- 【转】 ios开发之倒计时实现的两种方法
原文:http://blog.csdn.net/kylinbl/article/details/8972261 方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTime ...
- Python:标准数据类型6种
#!/usr/bin/python3 #python的基本语法和数据类型 #python3中 一行有多个语句,用分号分割(;) print("aaa") ;print(" ...
- 继承语法含有main()方法
package me.ybleeho; class Cleanser{ //清洁剂 private String s="Cleanser"; public void append( ...
- hibernate映射关系之多对多
多对多: * 关系在第三张表中,和两张表本身没有关系 * 多对多谁维护关系:谁都能维护关系(效率是一样的),一般情况下可以通过页面 来体现 * 关系体现: 第三张表的维护:增加.删除 course类对 ...
- js手机站跳转
var yunzhuanhua_pc_domain = "http://www.域名.com#yht"; //PC站网址var yunzhuanhua_wap_domain = & ...