private CreateEncryptor ( ) : ICryptoTransform | ||
return | ICryptoTransform |
using System.Security.Cryptography; public static byte[] EncryptString(string plainText, byte[] key, byte[] IV) { byte[] encrypted; using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { des.Key = key; des.IV = IV; ICryptoTransform encryptor = des.CreateEncryptor(des.Key, des.IV); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { using (StreamWriter sw = new StreamWriter(cs)) { sw.Write(plainText); } encrypted = ms.ToArray(); } } } return encrypted; }
using System.Security.Cryptography; public static void EncryptFile(string inputFile, string outputFile, byte[] key, byte[] IV) { using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { des.Key = key; des.IV = IV; ICryptoTransform encryptor = des.CreateEncryptor(des.Key, des.IV); using (FileStream fsOutput = new FileStream(outputFile, FileMode.Create)) { using (CryptoStream cs = new CryptoStream(fsOutput, encryptor, CryptoStreamMode.Write)) { using (FileStream fsInput = new FileStream(inputFile, FileMode.Open)) { byte[] buffer = new byte[4096]; int bytesRead; do { bytesRead = fsInput.Read(buffer, 0, buffer.Length); cs.Write(buffer, 0, bytesRead); } while (bytesRead != 0); } } } } }This example uses the DESCryptoServiceProvider class to encrypt a file using a given key and initialization vector. The method takes an input file path and an output file path and encrypts the contents of the input file to the output file. The System.Security.Cryptography namespace provides classes for a variety of cryptographic operations, including encryption, decryption, hashing, and signature generation.