Implementation method of encryption and decryption in .NET - 4

xiaoxiao2021-03-06  16

The client's workflow is:

Establish and send a public key to the server.

Receive the encrypted symmetric key from the server.

Decrypt the symmetric key and use it as a private asymmetric key.

Receive and use asymmetric key to decrypt information.

code show as below:

namespace com.billdawson.crypto {public class CryptoClient {private const int RSA_KEY_SIZE_BITS = 1024; private const int RSA_KEY_SIZE_BYTES = 252; private const int TDES_KEY_SIZE_BITS = 192; private const int TDES_KEY_SIZE_BYTES = 128; private const int TDES_IV_SIZE_BYTES = 128; public static void Main (string [] args) {int port; string host; TcpClient client; SymmetricAlgorithm symm; RSACryptoServiceProvider rsa; if (! args.Length = 2) {Console.WriteLine (USAGE); return;} try {host = args [0] Port = int32.parse (args [1]);} catch {console.writeline (usage); return;} try // connection {client = new tclient (); client.connect (host, port);} catch Exception E) {Console.writeLine (E.MESSAGE); console.write (e.stacktrace); return;} try {console.writeline ("Connected. Sending public key."); RSA = New RsacryptoserviceProvider (); RSA. KeySize = RSA_KEY_SIZE_BITS; sendPublicKey (rsa.ExportParameters (false), client); symm = new TripleDESCryptoServiceProvider (); symm.KeySize = TDES_KEY_SIZE_BITS; MemoryStream ms = getRestOfMessage (client); extractSymmet RickeyInfo (RSA, SYMM, MS); ShowsecretMessage (SYMM, MS);} catch (Exception E) {console.writeLine (E.MESSAGE); console.write (e.stacktrace);} finally {Try {Client.Close );} catch {error}}} private static void sendPublicKey (RSAParameters key, TcpClient client) {NetworkStream ns = client.GetStream // (); BinaryFormatter bf = new BinaryFormatter (); bf.Serialize (ns, key);} Private Static MemoryStream GetrestOfMessage (TCPCLIENT Client) {// Get encrypted symmetrical key, initialization vector, secret information.

Symmetric key with public RSA key // encryption, secret information symmetric key encryption MemoryStream ms = new memoryStream (); networkStream ns = client.getStream (); byte [] buffer = new byte [1024]; int LEN = 0; // Write NetStream's data to memory while ((len = ns.read (buffer, 0, buffer.Length)> 0) {ms.write (buffer, 0, len);} ms.position = 0; return ms;} private static void extractSymmetricKeyInfo (RSACryptoServiceProvider rsa, SymmetricAlgorithm symm, MemoryStream msOrig) {MemoryStream ms = new MemoryStream (); // Get TDES keys - it is RSA public key encryption using a private key to decrypt byte [] buffer = new byte [TDES_KEY_SIZE_BYTES]; msOrig.Read (buffer, 0, buffer.Length); symm.Key = rsa.Decrypt (buffer, false); // Get TDES initialization vector buffer = new byte [TDES_IV_SIZE_BYTES] Msorig.read (Buffer, 0, Buffer.Length); symm.iv = rsa.decrypt (buffer, false);} Private static void showsecretmessage (symmetricalgorithm symm, memorystream msorig) {// All data in memory flow encrypted byte [] buffer = new byte [1024]; int len ​​= msOrig.Read (buffer, 0, buffer.Length); MemoryStream ms = new MemoryStream (); ICryptoTransform transform = symm.CreateDecryptor (symm.Key, symm. IV); CryptostReam C Stream = New Cryptostream (MS, Transform, CryptostreamMode.Write); cstractream.write (buffer, 0, len); cstractream.flushfinalBlock (); // The memory stream is now decrypted information, is the form of bytes, converting it to String ms.position = 0; len = ms.read (buffer, 0, (int) ms.Length); ms.close (); string msg = encoding.ascii.getstring (buffer, 0, len); console. WriteLine ("The Host Sent ME this Secret Message:"); Console.WriteLine (MSG);}}}

It is more suitable when encrypting local data using a symmetrical algorithm. When maintaining the code, we can select a variety of algorithms that encrypt this data when the data is used through a specific CRYPTOSTREAM algorithm. The data is required to encrypt the symmetric key using the received public asymmetric key by sending the data.

This article only involves a part of the service in the System.Security.cryptography name. Although the article guarantees that only a private key can decrypt the corresponding public key encryption, it does not guarantee who is sent by the public key, the sender may also be fake. You need to use classes that process digital certificates to deal with this risk.

转载请注明原文地址:https://www.9cbs.com/read-45017.html

New Post(0)