| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 545 人关注过本帖
标题:[分享]c#加解密塊
只看楼主 加入收藏
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
 问题点数:0 回复次数:4 
[分享]c#加解密塊

private const string key="lyfhednc";
private const string iv="sikechtf";
public static void Main()
{
string str="hello,world!";
do
{
Console.WriteLine("原始-->{0}",str);
DESCryptoServiceProvider DES=new DESCryptoServiceProvider();
//Console.WriteLine(Encoding.ASCII.GetString(DES.IV));
//Console.WriteLine(Encoding.ASCII.GetString(DES.Key));
//DES.IV=Encoding.ASCII.GetBytes(iv);
//DES.Key=Encoding.ASCII.GetBytes(key); // 這裡可以指定使用的key與iv 沒有指定則是隨機的 所
// 以每 次 都 不一樣
byte[] b=Encrypt(str,DES);
str=Encoding.UTF8.GetString(b);
Console.WriteLine("加密-->{0}",str);

Console.WriteLine("解密-->{0}",Decrypt(b,DES));

}while((str=Console.ReadLine())!="a");


}

public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
{
// Create a memory stream to the passed buffer.
MemoryStream ms = new MemoryStream(CypherText);

// Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);

// Create a StreamReader for reading the stream.
StreamReader sr = new StreamReader(encStream);

// Read the stream as a string.
string val = sr.ReadLine();

// Close the streams.
sr.Close();
encStream.Close();
ms.Close();

return val;
}


public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
{
// Create a memory stream.
MemoryStream ms = new MemoryStream();

// Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);

// Create a StreamWriter to write a string
// to the stream.
StreamWriter sw = new StreamWriter(encStream);

// Write the plaintext to the stream.
sw.WriteLine(PlainText);

// Close the StreamWriter and CryptoStream.
sw.Close();
encStream.Close();

// Get an array of bytes that represents
// the memory stream.
byte[] buffer = ms.ToArray();

// Close the memory stream.
ms.Close();

// Return the encrypted byte array.
return buffer;
}

以後陸續發對稱 非對稱 md5 sha1 sha256之類加密算法例子
以上引用到:
using System.Security.Cryptography;
using System.IO;

搜索更多相关主题的帖子: 解密 分享 
2006-08-23 09:47
CrazyWeed0907
Rank: 2
等 级:新手上路
威 望:5
帖 子:1385
专家分:0
注 册:2006-5-30
收藏
得分:0 

建议你当斑竹


问下你主要做什么的?

[此贴子已经被作者于2006-8-23 9:55:15编辑过]


“十步杀一人,千里不留行。事了拂衣去,深藏身与名。”
2006-08-23 09:53
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
以下是引用CrazyWeed0907在2006-8-23 9:53:07的发言:

建议你当斑竹


问下你主要做什么的?


3Q
c# vb asp這一塊


[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-23 10:07
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 
还用了一个
System.Text
因为用到了Encoding

此号自封于2006年11月30日
2006-08-23 10:15
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 
鉴定完毕
挺好用的
不过没看懂

此号自封于2006年11月30日
2006-08-23 10:19
快速回复:[分享]c#加解密塊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044034 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved