The encryption is currently two: private key and public key encryption, also known as symmetry and non-symmetry encryption. The private key encryption only uses a key to add recolve, because the same key is used for encryption and for decryption, Therefore, the private key is encrypted very fast (compared to the public key algorithm), it is especially suitable for encrypting the larger data stream. There are too many things to be introduced, so this time only the private key encryption, private key encryption, only for a block of encryption, that is, convert N-byte encryption to encrypted byte output, since It is encryption. Of course, a key, Right is needed to use a chain pattern called "Password Block Chain (CBC)" in .NET, it requires a key and an IV initialization vector, it is division Blocks are encrypted, and you must use IV vector to encrypt the first block, (everything must be a starting) Next, use the previous block to encrypt the next block until the reading is completed. In addition, send and receive the message, must be The value of the key and IV is conveyed and the key cannot be known for unauthorized users.
The advantages of private key encryption: fast. Disadvantages: patient friends can use the exhaustion method, huh, I can don't be patient. Usage: Usually use the public key encryption, encrypted in public key encryption, first pair public key encryption Then send the public key to the other party. Give the public key plus algorithm a aid. Once, look at the code below: (First build a text file in the execution file directory)
// ------------------------------ File Encryption Process FILESTREAM FIN = New FileStream ("Input.txt", FileMode.Open FileAccess.read; filestream fout = new filestream ("Output.txt", filemode.openorcreate, fileaccess.write); // Create an encrypted class
DES des = new descryptoServiceProvider (); des.generateKey (); // Get randomly generated key, and iv des.generateiv (); byte [] keys = des.key; byte [] ivs = des.iv; long line = Fin.Length; // Get the length of the file long temp = 0; // Temporary variable byte [] buffer = new byte [400]; // Used to store the file content in the cache
CryptostReam cstream = new cryptostream (fout, des.createEncryptor (keys, ivs), cryptostreammode.write); // Create a plus-derived and written to "Output.txt" file while (Temp Console.readLine (); cstream.close (); fout.close (); Fin.Close (); Decryption: // ----------------------------- File Decryption process FILESTREAM FIN1 = New FileStream ("Output.txt", FileMode.Open FileAccess.read; filestream fout1 = new filestream ("Decryption .txt", filemode.openorcreate, fileaccess.write); // des des1 = new descryptoserviceProvider (); //des1.generateKey (); // Get random production Key, and IV //Des1.generateiv (); // byte [] keys1 = des1.key; // Byte [] IVS1 = des1.iv; long length = fin1.length; // Get file length Long Temp1 = 0; // Temporary variable byte [] buffer1 = new byte [400]; // is used to store file content in the cache CryptoStream cStream1 = new CryptoStream (fout1, des.CreateDecryptor (keys, ivs), CryptoStreamMode.Write); // create the encryption stream and writes the "output.txt" file while (temp1 Console.readline (); cstream.close (); fout.close (); Fin.Close (); // ----------------------- --- over OVER