加密解密,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 ...
随机推荐
- Python之路【第十一篇】:CSS --暂无内容-待更新
Python之路[第十一篇]:CSS --暂无内容-待更新
- 点击其它地方隐藏div/事件冒泡/sweet-alert阻止冒泡
点击document时把div隐藏,但点击div时阻止点击事件冒泡到document,从而实现“点击文档其它地方隐藏div,点击div本身不隐藏”.js代码如下:$("#div") ...
- ASIHttpRequest网络请求第三方类库使用方法详解
一. 第一步首先你要从网络上下载ASIHttpRequestlib, 下载以后解压,增加到你的工程里面, 这个时间检查工程内部是否已经导入成功,文件结构如下: ASIHTTPRequestConfig ...
- mysqldump备份、还原数据库路径名含有空格的处理方法(如:Program Files)
虽然以下的方法也可以解决,不过最简单直接的,还是直接在路径前后加双引号-" ",这个方法简单有效. 首先要说明的是mysqldump.exe在哪里不重要,重要的是要处理好路径中的非 ...
- [转]mysql自动定时备份数据库的最佳方法-支持windows系统
网上有很多关于window下Mysql自动备份的方法,可是真的能用的也没有几个,有些说的还非常的复杂,难以操作. 我们都知道mssql本身就自带了计划任务可以用来自动备份,可是mysql咱们要怎么样自 ...
- delegate-使用笔记
public class testclass { public class ProductImages : Page { protected Repeater rptSmallUrls; protec ...
- 绑定下拉框时避免触发SelectedIndexChanged事件
在从数据库读取数据集绑定到下拉框时会立即触发其SelectedIndexChanged事件造成异常,可对其SelectedIndexChanged事件采取先解除后附加的方法解决. cmbXl_gt.V ...
- Magento 编译 php5.6.21 命令
./configure '--prefix=/alidata/server/php' '--enable-opcache' '--with-config-file-path=/alidata/ser ...
- python中文字符串前加u
我明明在编码前就加上了# -*- coding: UTF-8 -*-可是运行时还是出错了, # -*- coding: UTF-8 -*- 这句是告诉python程序中的文本是utf-8编码,让pyt ...
- C语言结构体(struct)使用方法
基本定义:结构体,通俗讲就像是打包封装,把一些变量有共同特征(比如同属于某一类事物的属性)的变量封装在内部,通过一定方法访问修改内部变量. 结构体定义: 第一种:只有结构体定义 struct stuf ...