一:上图

二:代码

主界面代码

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. Conversion Between DataTable and List in C#

    1.List to DataTable public static DataTable ToDataTable<TSource>(this IList<TSource> dat ...

  2. heibernate增删改查总结一下自己的不足

    难点也就我不熟悉的是数据库语句的书写,要加强复杂查询语句的书写 /* 简单的在共享类中已经可以用的了 * 总结: * * --------------------------------------查 ...

  3. Java-strurs总结

    这里是自己对自学的struts2 的一个整体 的脉络进行的一个概括,需要学习哪些东西,注重哪些东西: struts2 是主流框架SSH 中的一个"S" ,准备MVC开发标准的一个框 ...

  4. maven发布的资源文件到tomcat项目下

    问题:项目中有hibernate的资源文件,src/main/java和src/main/resources都有这些文件,当启动项目时发现出错.但是src/main/java已经修改好了, 经查tom ...

  5. 项目报错,tomcat中引起

    1.项目报错,但发现工程并没有错.此刻错误应该定位如下,即工程里面引用的jar可能有错,可能是路劲变了....

  6. Web性能压力测试之Webbench使用详解

    Webbench是知名的网站压力测试工具,它是由Lionbridge公司(http://www.lionbridge.com)开发.Webbench能测试处在相同硬件上,不同服务的性能以及不同硬件上同 ...

  7. Invalid project description overlaps the location of another project [android]

    解决办法: 1.将工程放到其他目录下,然后执行Android工程的导入,导入时可以选择“Copy projects into workspace”: 2.不用Android工程导入,而用普通的工程导入 ...

  8. Zend Server安装后首次运行就出现Internal Server Error的解决(转)

    新近学习php,结果装了Zend Server上来就报错,网上找到了解决方法,照着做果然可行,转之. 刚才安装了Zend Server,安装后首次运行就爆出了一个Internal Server Err ...

  9. Bootstrap_Javascript_弹出框

    HTML: <button type="button" class="btn btn-default" data-container="body ...

  10. ObjectiveC1基础代码——类和对象

    // //  main.m //  ObjectiveC1 // //  Created by scjy on 15/10/30. //  Copyright © 2015年 lizhipeng. A ...