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;
while(totalLen (len = ns.Read(buffer,0,buffer.Length))>0) { totalLen+=len; ms.Write(buffer, 0, len); }
ms.Position=0;
result = (RSAParameters)bf.Deserialize(ms); ms.Close();
return result;
}
private static void encryptAndSendSymmetricKey( TcpClient client, RSACryptoServiceProvider rsa, SymmetricAlgorithm symm) { // 使用客户端的公共密钥加密对称密钥 byte[] symKeyEncrypted; byte[] symIVEncrypted;
NetworkStream ns = client.GetStream();
symKeyEncrypted = rsa.Encrypt(symm.Key, false); symIVEncrypted = rsa.Encrypt(symm.IV, false);
ns.Write(symKeyEncrypted, 0, symKeyEncrypted.Length); ns.Write(symIVEncrypted, 0, symIVEncrypted.Length);
}
private static void encryptAndSendSecretMessage(TcpClient client, SymmetricAlgorithm symm, string secretMsg) { // 使用对称密钥和初始化矢量加密信息并发送给客户端 byte[] msgAsBytes; NetworkStream ns = client.GetStream(); ICryptoTransform transform = symm.CreateEncryptor(symm.Key,symm.IV); CryptoStream cstream = new CryptoStream(ns, transform, CryptoStreamMode.Write);
msgAsBytes = Encoding.ASCII.GetBytes(secretMsg);
cstream.Write(msgAsBytes, 0, msgAsBytes.Length); cstream.FlushFinalBlock(); } } |