只想把字符串明文加密成密文显示,并可以再转换成明文,密文不是只简单的把明文里的A换B,B换C这样。
试了一下用DSA(对这些不太了解,莫笑),是把明文换密文了,代码如下:
string a="hello world",b;//a是明文,b是密文
byte[] bytes = Encoding.ASCII.GetBytes(a);
DSACryptoServiceProvider dsac = new DSACryptoServiceProvider();
byte[] sign = dsac.SignData(bytes);
b = Encoding.ASCII.GetString(sign);
这样一来b字符串的内容确实是乱七八糟的字符了,但是不知道怎么才能把b字符串的内容再转回明文。
如有其它方法,也望指教,谢谢。
试了一下用DSA(对这些不太了解,莫笑),是把明文换密文了,代码如下:
string a="hello world",b;//a是明文,b是密文
byte[] bytes = Encoding.ASCII.GetBytes(a);
DSACryptoServiceProvider dsac = new DSACryptoServiceProvider();
byte[] sign = dsac.SignData(bytes);
b = Encoding.ASCII.GetString(sign);
这样一来b字符串的内容确实是乱七八糟的字符了,但是不知道怎么才能把b字符串的内容再转回明文。
如有其它方法,也望指教,谢谢。
解决方案 »
- stream 怎么转成 struct对象
- C#调用IDL的问题
- 悬赏!!!微软MCITP SQL2008数据库认证 有什么用???
- 根据计算机名称如何获取IP?
- C#如何激活已运行程序
- 怎么实现winform中从listBoxA中拖拽某一项到istBoxB!
- 问一个关系类型转换的小白问题
- 如何获取公共类库的注释信息,如:枚举的各个成员的中文注释?
- 有关Invalidate()函数显示问题
- C# winform 中 DataGridView控件SelectionChanged事件中字段编写问题
- 局域网内如何通过机器的IP地址获取该机器的MAC地址?
- 用C#开发一个基于WEB的图书管理系统请问谁有这方面的资料?帮帮我这个菜烏!!!
//注:利用DotNET密码系统之一的DES对称加密算法保证数据安全 //
///////////////////////////////////////////////////////////// using System;
using System.IO;
using System.Text;
using System.Security.Cryptography; namespace EzoneInternationalSecurityCryptography
{
class EzoneSecurityCryptographyDemo
{ [STAThread]
public static void Main(string[] args)
{
//加密数据(从内存到文件)
EzoneEncryptorDemo();
//解密数据(从文件到内存)
EzoneDecryptorDemo();
} /// <summary>
/// 加密
/// </summary>
public static void EzoneEncryptorDemo()
{
//创建一个文件对象,文件的模式是创建新文件,文件的访问权限是可写!
FileStream fs=new FileStream("EzoneDemo.txt",FileMode.Create,FileAccess.Write);
Console.WriteLine("请输入你想要进行加密的字符串:");
//输入你想要进行加密的字符串
string YourInput=Console.ReadLine();
//将字符串转换成字节
byte[] YourInputStorage=System.Text.Encoding.UTF8.GetBytes(YourInput);
//创建一个DES算法的加密类
DESCryptoServiceProvider MyServiceProvider=new DESCryptoServiceProvider();
//从DES算法的加密类对象的CreateEncryptor方法,创建一个加密转换接口对象
//第一个参数的含义是:对称算法的机密密钥(长度为64位,也就是8个字节)
// 可以人工输入,也可以随机生成方法是:MyServiceProvider.GenerateKey();
//第二个参数的含义是:对称算法的初始化向量(长度为64位,也就是8个字节)
// 可以人工输入,也可以随机生成方法是:MyServiceProvider.GenerateIV();
ICryptoTransform MyTransform=MyServiceProvider.CreateEncryptor(new byte[]{100,110,120,130,100,110,120,130},new byte[]{100,110,120,130,100,110,120,130});
//CryptoStream对象的作用是将数据流连接到加密转换的流
CryptoStream MyCryptoStream=new CryptoStream(fs,MyTransform,CryptoStreamMode.Write);
//将字节数组中的数据写入到加密流中
MyCryptoStream.Write(YourInputStorage,0,YourInputStorage.Length);
//关闭加密流对象
MyCryptoStream.Close(); } /// <summary>
/// 解密
/// </summary>
public static void EzoneDecryptorDemo()
{
FileStream fs=new FileStream("EzoneDemo.txt",FileMode.Open,FileAccess.Read);
DESCryptoServiceProvider MyServiceProvider=new DESCryptoServiceProvider();
//从DES算法的加密类对象的CreateEncryptor方法,创建一个解密转换接口对象
//[对称算法的机密密钥]必须是加密时候的[对称算法的机密密钥]
//[对称算法的初始化向量]必须是加密时候的[对称算法的初始化向量]
//如果不一样,则会抛出一个异常。
ICryptoTransform MyTransform=MyServiceProvider.CreateDecryptor(new byte[]{100,110,120,130,100,110,120,130},new byte[]{100,110,120,130,100,110,120,130});
CryptoStream MyCryptoStream=new CryptoStream(fs,MyTransform,CryptoStreamMode.Read);
byte[] YourInputStorage=new byte[1000];
int len=MyCryptoStream.Read(YourInputStorage,0,YourInputStorage.Length);
Console.WriteLine("你刚才输入的字符串是:");
Console.WriteLine(System.Text.Encoding.UTF8.GetString(YourInputStorage,0,len));
} }
}