加密和解密的算法
System
加密和解密的算法分為對稱(symmetric)算法和不對稱(asymmetric)算法
加密和解密本地文檔
下面的例子是加密和解密本地文本
對稱算法在數據流通過時對它進行加密
接下來定義相應的對象變量
下一步
最後
下面是加密和解密一個文本文件的源程序片斷
namespace com
{
class TextFileCrypt
{
public static void Main(string[] args)
{
string file = args[
string tempfile = Path
//打開指定的文件
FileStream fsIn = File
FileAccess
FileStream fsOut = File
FileAccess
//定義對稱算法對象實例和接口
SymmetricAlgorithm symm = new RijndaelManaged();
ICryptoTransform transform = symm
CryptoStream cstream = new CryptoStream(fsOut
ryptoStreamMode
BinaryReader br = new BinaryReader(fsIn);
// 讀取源文件到cryptostream
cstream
cstream
cstream
fsIn
fsOut
Console
Console
// 反向操作
fsIn = File
transform = symm
cstream = new CryptoStream(fsIn
CryptoStreamMode
StreamReader sr = new StreamReader(cstream);
Console
fsIn
}
}
}
如果我有一個只想自己看到的文檔
非對稱算法就是一種解決方案
不對稱算法比對稱算法計算的花費多
示例是一個TCP程序
從客戶端接收公共密鑰
使用公共密鑰加密未來使用的對稱密鑰
將加密了的對稱密鑰發送給客戶端
給客戶端發送使用該對稱密鑰加密的信息
代碼如下
namespace com
{
public class CryptoServer
{
private const int RSA_KEY_SIZE_BITS =
private const int RSA_KEY_SIZE_BYTES =
private const int TDES_KEY_SIZE_BITS =
public static void Main(string[] args)
{
int port;
string msg;
TcpListener listener;
TcpClient client;
SymmetricAlgorithm symm;
RSACryptoServiceProvider rsa;
//獲取端口
try
{
port = Int
msg = args[
}
catch
{
Console
return;
}
//建立監聽
try
{
listener = new TcpListener(port);
listener
Console
client = listener
Console
}
catch (Exception e)
{
Console
Console
return;
}
try
{
rsa = new RSACryptoServiceProvider();
rsa
// 獲取客戶端公共密鑰
rsa
symm = new TripleDESCryptoServiceProvider();
symm
//使用客戶端的公共密鑰加密對稱密鑰並發送給客
encryptAndSendSymmetricKey(client
//使用對稱密鑰加密信息並發送
encryptAndSendSecretMessage(client
}
catch (Exception e)
{
Console
Console
}
finally
{
try
{
client
listener
}
catch
{
//錯誤
}
Console
}
}
private static RSAParameters getClientPublicKey(TcpClient client)
{
// 從字節流獲取串行化的公共密鑰
byte[] buffer = new byte[RSA_KEY_SIZE_BYTES];
NetworkStream ns = client
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
RSAParameters result;
int len =
int totalLen =
while(totalLen (len = ns
{
totalLen+=len;
ms
}
ms
result = (RSAParameters)bf
ms
return result;
}
private static void encryptAndSendSymmetricKey(
TcpClient client
RSACryptoServiceProvider rsa
SymmetricAlgorithm symm)
{
// 使用客戶端的公共密鑰加密對稱密鑰
byte[] symKeyEncrypted;
byte[] symIVEncrypted;
NetworkStream ns = client
symKeyEncrypted = rsa
symIVEncrypted = rsa
ns
ns
}
private static void encryptAndSendSecretMessage(TcpClient client
SymmetricAlgorithm symm
string secretMsg)
{
// 使用對稱密鑰和初始化矢量加密信息並發送給客戶端
byte[] msgAsBytes;
NetworkStream ns = client
ICryptoTransform transform =
symm
CryptoStream cstream =
new CryptoStream(ns
msgAsBytes = Encoding
cstream
cstream
}
}
客戶端的工作流程是
建立和發送公共密鑰給服務器
從服務器接收被加密的對稱密鑰
解密該對稱密鑰並將它作為私有的不對稱密鑰
接收並使用不對稱密鑰解密信息
代碼如下
namespace com
{
public class CryptoClient
{
private const int RSA_KEY_SIZE_BITS =
private const int RSA_KEY_SIZE_BYTES =
private const int TDES_KEY_SIZE_BITS =
private const int TDES_KEY_SIZE_BYTES =
private const int TDES_IV_SIZE_BYTES =
public static void Main(string[] args)
{
int port;
string host;
TcpClient client;
SymmetricAlgorithm symm;
RSACryptoServiceProvider rsa;
if (args
{
Console
return;
}
try
{
host = args[
port = Int
}
catch
{
Console
return;
}
try //連接
{
client = new TcpClient();
client
}
catch(Exception e)
{
Console
Console
return;
}
try
{
Console
rsa = new RSACryptoServiceProvider();
rsa
sendPublicKey(rsa
symm = new TripleDESCryptoServiceProvider();
symm
MemoryStream ms = getRestOfMessage(client);
extractSymmetricKeyInfo(rsa
showSecretMessage(symm
}
catch(Exception e)
{
Console
Console
}
finally
{
try
{
client
}
catch { //錯誤
}
}
}
private static void sendPublicKey(
RSAParameters key
TcpClient client)
{
NetworkStream ns = client
BinaryFormatter bf = new BinaryFormatter();
bf
}
private static MemoryStream getRestOfMessage(TcpClient client)
{
//獲取加密的對稱密鑰
//加密
MemoryStream ms = new MemoryStream();
NetworkStream ns = client
byte[] buffer = new byte[
int len=
// 將NetStream 的數據寫入內存流
while((len = ns
{
ms
}
ms
return ms;
}
private static void extractSymmetricKeyInfo(
RSACryptoServiceProvider rsa
SymmetricAlgorithm symm
MemoryStream msOrig)
{
MemoryStream ms = new MemoryStream();
// 獲取TDES密鑰
byte[] buffer = new byte[TDES_KEY_SIZE_BYTES];
msOrig
symm
// 獲取TDES初始化矢量
buffer = new byte[TDES_IV_SIZE_BYTES];
msOrig
symm
}
private static void showSecretMessage(
SymmetricAlgorithm symm
MemoryStream msOrig)
{
//內存流中的所有數據都被加密了
byte[] buffer = new byte[
int len = msOrig
MemoryStream ms = new MemoryStream();
ICryptoTransform transform =
symm
CryptoStream cstream =new CryptoStream(ms
CryptoStreamMode
cstream
cstream
// 內存流現在是解密信息
ms
len = ms
ms
string msg = Encoding
Console
Console
}
}
}
結論
使用對稱算法加密本地數據時比較適合
From:http://tw.wingwit.com/Article/program/net/201311/11946.html