Common file encryption and decryption algorithm problems in C# require specific code examples
In modern computer applications, data protection and security are particularly important. File encryption and decryption algorithms are a commonly used data security protection measure to ensure that files are not accessed and modified by unauthorized personnel during transmission and storage. This article will explore common file encryption and decryption algorithm issues in C# and provide corresponding specific code examples.
The symmetric encryption algorithm is an algorithm that uses the same key for encryption and decryption. Common symmetric encryption algorithms in C# include DES, AES and RC4. Here we take the DES algorithm as an example to show the specific implementation of file encryption and decryption.
First, we need to define a function to generate a random key:
public static byte[] GenerateRandomKey() { byte[] key = new byte[8]; using (var rng = new RNGCryptoServiceProvider()) { rng.GetBytes(key); } return key; }
Next, we can use the generated key to encrypt and decrypt files. The following is an example of using the DES algorithm for file encryption:
public static void EncryptFile(string inputFile, string outputFile, byte[] key) { using (var des = new DESCryptoServiceProvider()) { des.Key = key; des.Mode = CipherMode.ECB; using (var fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read)) { using (var fsOutput = new FileStream(outputFile, FileMode.Create, FileAccess.Write)) { using (var cryptoStream = new CryptoStream(fsOutput, des.CreateEncryptor(), CryptoStreamMode.Write)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fsInput.Read(buffer, 0, buffer.Length)) > 0) { cryptoStream.Write(buffer, 0, bytesRead); } } } } } }
In the above example code, we use the DESCryptoServiceProvider class to create an instance of the DES encryption algorithm. Then, we use the CreateEncryptor method to generate the encryptor and write the encrypted data to the output file.
The process of decrypting files is similar to encryption, just change creating an encryptor to creating a decryptor. The following is an example of file decryption using the DES algorithm:
public static void DecryptFile(string inputFile, string outputFile, byte[] key) { using (var des = new DESCryptoServiceProvider()) { des.Key = key; des.Mode = CipherMode.ECB; using (var fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read)) { using (var fsOutput = new FileStream(outputFile, FileMode.Create, FileAccess.Write)) { using (var cryptoStream = new CryptoStream(fsOutput, des.CreateDecryptor(), CryptoStreamMode.Write)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fsInput.Read(buffer, 0, buffer.Length)) > 0) { cryptoStream.Write(buffer, 0, bytesRead); } } } } } }
Asymmetric encryption algorithm is a method that uses a pair of keys for encryption and decryption algorithm, including public and private keys. Common asymmetric encryption algorithms in C# include RSA and DSA.
When using an asymmetric encryption algorithm to encrypt and decrypt files, you first need to generate a pair of keys. The following is an example of using the RSA algorithm to generate a key:
public static void GenerateKeyPair(out string publicKey, out string privateKey) { using (var rsa = new RSACryptoServiceProvider()) { publicKey = rsa.ToXmlString(false); privateKey = rsa.ToXmlString(true); } }
After generating the key, we can use the public key to encrypt the file and the private key to decrypt the file. The following is an example of using the RSA algorithm for file encryption:
public static void EncryptFile(string inputFile, string outputFile, string publicKey) { using (var rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(publicKey); using (var fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read)) { using (var fsOutput = new FileStream(outputFile, FileMode.Create, FileAccess.Write)) { using (var cryptoStream = new CryptoStream(fsOutput, rsa.Encryptor, CryptoStreamMode.Write)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fsInput.Read(buffer, 0, buffer.Length)) > 0) { cryptoStream.Write(buffer, 0, bytesRead); } } } } } }
In the above example code, we create an RSACryptoServiceProvider instance based on the public key, and use the Encryptor attribute to obtain the encryptor and write the encrypted data to the output in the file.
The process of decrypting files is similar to encryption, just change creating an encryptor to creating a decryptor. The following is an example of using the RSA algorithm for file decryption:
public static void DecryptFile(string inputFile, string outputFile, string privateKey) { using (var rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(privateKey); using (var fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read)) { using (var fsOutput = new FileStream(outputFile, FileMode.Create, FileAccess.Write)) { using (var cryptoStream = new CryptoStream(fsOutput, rsa.Decryptor, CryptoStreamMode.Write)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fsInput.Read(buffer, 0, buffer.Length)) > 0) { cryptoStream.Write(buffer, 0, bytesRead); } } } } } }
Summary:
File encryption and decryption algorithms are important means of protecting data security. This article introduces common symmetric and asymmetric encryption algorithms in C# and provides corresponding code examples. By understanding and applying these encryption algorithms, we can protect the confidentiality and integrity of files and ensure the security of data during transmission and storage.
The above is the detailed content of Common file encryption and decryption algorithm issues in C#. For more information, please follow other related articles on the PHP Chinese website!