c image encryption decryption instance code
- 2020-05-19 05:32:21
- OfStack
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Net.Template.Common
{
/// <summary>
/// The encryption and decryption of images
/// </summary>
public class DEncrypt4ImageHelper
{
public DEncrypt4ImageHelper() { }
#region Encryption methods Image encryption
/// <summary>
/// Image encryption
/// </summary>
/// <param name="filePath"> The source file </param>
/// <param name="savePath"> Save as the file name </param>
/// <param name="keyStr"> The key </param>
public static void EncryptFile(string filePath, string savePath, string keyStr)
{
// through des encryption
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
// Open the file through the stream
FileStream fs = File.OpenRead(filePath);
// Access to the file 2 Hexadecimal characters
byte[] inputByteArray = new byte[fs.Length];
// Read the stream file
fs.Read(inputByteArray, 0, (int)fs.Length);
// Close the stream
fs.Close();
// Get the encrypted string 2 Hexadecimal characters
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
// Computes the hash value of the specified region for the specified byte group
SHA1 ha = new SHA1Managed();
byte[] hb = ha.ComputeHash(keyByteArray);
// Encryption key array
byte[] sKey = new byte[8];
// Encryption variables
byte[] sIV = new byte[8];
for (int i = 0; i < 8; i++)
sKey[i] = hb[i];
for (int i = 8; i < 16; i++)
sIV[i - 8] = hb[i];
// Get the encryption key
des.Key = sKey;
// Sets the encryption initialization vector
des.IV = sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs = File.OpenWrite(savePath);
foreach (byte b in ms.ToArray())
{
fs.WriteByte(b);
}
fs.Close();
cs.Close();
ms.Close();
}
#endregion
#region Decryption method Image decryption
/// <summary>
/// Image decryption
/// </summary>
/// <param name="filePath"> The source file </param>
/// <param name="savePath"> Save the file </param>
/// <param name="keyStr"> The key </param>
public static void DecryptFile(string filePath, string savePath, string keyStr)
{
// through des decryption
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
// The file is read through the stream
FileStream fs = File.OpenRead(filePath);
// Access to the file 2 Hexadecimal characters
byte[] inputByteArray = new byte[fs.Length];
// Read stream file
fs.Read(inputByteArray, 0, (int)fs.Length);
// Close the stream
fs.Close();
// Key array
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
// Define hash variables
SHA1 ha = new SHA1Managed();
// Computes the hash value of the specified region for the specified byte group
byte[] hb = ha.ComputeHash(keyByteArray);
// Encryption key array
byte[] sKey = new byte[8];
// Encryption variables
byte[] sIV = new byte[8];
for (int i = 0; i < 8; i++)
sKey[i] = hb[i];
for (int i = 8; i < 16; i++)
sIV[i - 8] = hb[i];
// Get the encryption key
des.Key = sKey;
// Encryption variables
des.IV = sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs = File.OpenWrite(savePath);
foreach (byte b in ms.ToArray())
{
fs.WriteByte(b);
}
fs.Close();
cs.Close();
ms.Close();
}
#endregion
}
}