SoFunction
Updated on 2025-03-06

C# Encrypted and Decrypted File Gadget Implementation Code


using System;
using ;
using ;
using ;
using ;
namespace DESFile
{
/// <summary>
///Exception handling class
/// </summary>
public class CryptoHelpException : ApplicationException
{
public CryptoHelpException(string msg) : base(msg) { }
}
/// <summary>
/// CryptHelp
/// </summary>
public class DESFileClass
{
private const ulong FC_TAG = 0xFC010203040506CF;
private const int BUFFER_SIZE = 128 * 1024;
/// <summary>
/// Check whether the two Byte arrays are the same
/// </summary>
/// <param name="b1">Byte array</param>
/// <param name="b2">Byte array</param>
/// <returns>true-equal</returns>
private static bool CheckByteArrays(byte[] b1, byte[] b2)
{
if ( == )
{
for (int i = 0; i < ; ++i)
{
if (b1[i] != b2[i])
return false;
}
return true;
}
return false;
}
/// <summary>
/// Create DebugLZQ,/DebugLZQ
/// </summary>
/// <param name="password">Password</param>
/// <param name="salt"></param>
/// <returns>Encrypted object</returns>
private static SymmetricAlgorithm CreateRijndael(string password, byte[] salt)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt, "SHA256", 1000);
SymmetricAlgorithm sma = ();
= 256;
= (32);
= PaddingMode.PKCS7;
return sma;
}
/// <summary>
/// Generate random number of encrypted files
/// </summary>
private static RandomNumberGenerator rand = new RNGCryptoServiceProvider();
/// <summary>
/// Generate a random Byte array of specified length
/// </summary>
/// <param name="count">Byte array length</param>
/// <returns>Random Byte Array</returns>
private static byte[] GenerateRandomBytes(int count)
{
byte[] bytes = new byte[count];
(bytes);
return bytes;
}
/// <summary>
/// Encrypt files
/// </summary>
/// <param name="inFile">File to be encrypted</param>
/// <param name="outFile">Enter the file after encryption</param>
/// <param name="password">Encrypt password</param>
public static void EncryptFile(string inFile, string outFile, string password)
{
using (FileStream fin = (inFile),
fout = (outFile))
{
long lSize = ; // Enter file length
int size = (int)lSize;
byte[] bytes = new byte[BUFFER_SIZE]; // Cache
int read = -1; // Input file read count
int value = 0;
// Get IV and salt
byte[] IV = GenerateRandomBytes(16);
byte[] salt = GenerateRandomBytes(16);
// Create an encrypted object
SymmetricAlgorithm sma = (password, salt);
= IV;
// Write IV and salt in the start part of the output file
(IV, 0, );
(salt, 0, );
// Create hash encryption
HashAlgorithm hasher = ();
using (CryptoStream cout = new CryptoStream(fout, (), ),
chash = new CryptoStream(, hasher, ))
{
BinaryWriter bw = new BinaryWriter(cout);
(lSize);
(FC_TAG);
// Read and write byte blocks to encrypted stream buffer
while ((read = (bytes, 0, )) != 0)
{
(bytes, 0, read);
(bytes, 0, read);
value += read;
}
// Turn off encrypted streaming
();
();
// Read hash
byte[] hash = ;
// Write hash in the input file
(hash, 0, );
// Close the file stream
();
();
}
}
}
/// <summary>
/// Decrypt the file
/// </summary>
/// <param name="inFile">File to be decrypted</param>
/// <param name="outFile">Decrypted output file</param>
/// <param name="password">Decrypt password</param>
public static void DecryptFile(string inFile, string outFile, string password)
{
// Create an open file stream
using (FileStream fin = (inFile),
fout = (outFile))
{
int size = (int);
byte[] bytes = new byte[BUFFER_SIZE];
int read = -1;
int value = 0;
int outValue = 0;
byte[] IV = new byte[16];
(IV, 0, 16);
byte[] salt = new byte[16];
(salt, 0, 16);
SymmetricAlgorithm sma = (password, salt);
= IV;
value = 32;
long lSize = -1;
// Create hash object and verify file
HashAlgorithm hasher = ();
using (CryptoStream cin = new CryptoStream(fin, (), ),
chash = new CryptoStream(, hasher, ))
{
// Read file length
BinaryReader br = new BinaryReader(cin);
lSize = br.ReadInt64();
ulong tag = br.ReadUInt64();
if (FC_TAG != tag)
throw new CryptoHelpException("File is corrupted");
long numReads = lSize / BUFFER_SIZE;
long slack = (long)lSize % BUFFER_SIZE;
for (int i = 0; i < numReads; ++i)
{
read = (bytes, 0, );
(bytes, 0, read);
(bytes, 0, read);
value += read;
outValue += read;
}
if (slack > 0)
{
read = (bytes, 0, (int)slack);
(bytes, 0, read);
(bytes, 0, read);
value += read;
outValue += read;
}
();
();
();
();
byte[] curHash = ;
// Get comparison and old hash objects
byte[] oldHash = new byte[ / 8];
read = (oldHash, 0, );
if (( != read) || (!CheckByteArrays(oldHash, curHash)))
throw new CryptoHelpException("File is corrupted");
}
if (outValue != lSize)
throw new CryptoHelpException("File size mismatch");
}
}
}
}