网站首页

家园论坛

老版论坛

家园博客

业界新闻

技术文档

下载中心

速查中心

图片中心

硬件资讯
上一篇:.NET中加密和解密的实现方法(一) 下一篇:c#发送需要smtp认证的邮件
.NET中加密和解密的实现方法(二)

来源: 作者: 添加日期:2005-9-4 19:19:56 点击次数:

使用私有密钥解密该文档,这是唯一可以解密的密钥,并且没有通过网络传递。

  不对称算法比对称算法计算的花费多、速度慢。因此我们不希望在线对话中使用不对称算法加密所有信息。相反,我们使用对称算法。下面的例子中我们使用不对称加密来加密对称密钥。接着就使用对称算法加密了。实际上安全接口层(SSL)建立服务器和浏览器之间的安全对话使用的就是这种工作方式。
示例是一个TCP程序,分为服务器端和客户端。服务器端的工作流程是:

   从客户端接收公共密钥。

   使用公共密钥加密未来使用的对称密钥。

   将加密了的对称密钥发送给客户端。

   给客户端发送使用该对称密钥加密的信息。

  代码如下:

namespace com.billdawson.crypto
{
public class CryptoServer
{
private const int RSA_KEY_SIZE_BITS = 1024;
private const int RSA_KEY_SIZE_BYTES = 252;
private const int TDES_KEY_SIZE_BITS = 192;

public static void Main(string[] args)
{
int port;
string msg;
TcpListener listener;
TcpClient client;
SymmetricAlgorithm symm;
RSACryptoServiceProvider rsa;
//
获取端口
try
{
port = Int32.Parse(args[0]);
msg = args[1];
}
catch
{
Console.WriteLine(USAGE);
return;
}
//
建立监听
try
{
listener = new TcpListener(port);
listener.Start();
Console.WriteLine("Listening on port {0}...",port);

client = listener.AcceptTcpClient();
Console.WriteLine("connection....");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
return;
}

try
{
rsa = new RSACryptoServiceProvider();
rsa.KeySize = RSA_KEY_SIZE_BITS;

//
获取客户端公共密钥
rsa.ImportParameters(getClientPublicKey(client));

symm = new TripleDESCryptoServiceProvider();
symm.KeySize = TDES_KEY_SIZE_BITS;
//使用客户端的公共密钥加密对称密钥并发送给客。
encryptAndSendSymmetricKey(client, rsa, symm);

//
使用对称密钥加密信息并发送
encryptAndSendSecretMessage(client, symm, msg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
finally
{
try
{
client.Close();
listener.Stop();
}
catch
{
//
错误
}
Console.WriteLine("Server exiting...");
}
}

private static RSAParameters getClientPublicKey(TcpClient client)
{
//
从字节流获取串行化的公共密钥,通过串并转换写入类的实例
byte[] buffer = new byte[RSA_KEY_SIZE_BYTES];
NetworkStream ns = client.GetStream();
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
RSAParameters result;

int len = 0;
int totalLen = 0;


 
设为首页 | 加入收藏 | 业务办理 | 友情链接 | 论坛版面 | 浙ICP备07502118号 |