Binary | encryption | decryption
The following class implements the file encryption and decryption operations, the test of several file types are no problem, and now share with you.
Namespace Mycryptohelp
{
<summary>
Exception Handling Class
</summary>
public class Cryptohelpexception:applicationexception
{
Public cryptohelpexception (String msg): Base (msg) {}
}
<summary>
Crypthelp
</summary>
public class Cryptohelp
{
Private Const ULONG Fc_tag = 0XFC010203040506CF;
private const int buffer_size = 128*1024;
///<summary>
///Verify that 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 (B1. Length = = B2. Length)
{
for (int i = 0; I < B1. Length; ++i)
{
if (B1[i]!= b2[i])
return false;
}
return true;
}
return false;
}
///<summary>
///Create Rijndael symmetricalgorithm
///</ Summary>
///<param name= "password" > password </param>
///<param name= " Salt "></param>
///<returns> Cryptographic object </returns>
private Static SymmetricAlgorithm Createrijndael (string password, byte[] salt)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes (Password,salt, "SHA256", 1000);
symmetricalgorithm SMA = Rijndael.create ();
sma. KeySize = 256;
sma. Key = pdb. GetBytes (32);
sma. Padding = PADDINGMODE.PKCS7;
return SMA;
}
<summary>
Random number generation of encrypted files
</summary>
private static RandomNumberGenerator Rand = new RNGCryptoServiceProvider ();
<summary>
Generate a random byte array of the 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];
Rand. GetBytes (bytes);
return bytes;
}
<summary>
Encrypting files
</summary>
<param name= "InFile" > Encrypted file </param>
<param name= "outfile" > Encrypted input file </param>
<param name= "password" > Encrypted password </param>
public static void EncryptFile (String inFile, string outfile, string password)
{
using (FileStream fin = File.openread (inFile),
Fout = File.openwrite (outfile))
{
Long lsize = Fin. Length; Input file length
int size = (int) lsize;
byte[] bytes = new Byte[buffer_size]; Cache
int read =-1; Input file Read quantity
int value = 0;
Get IV and salt
byte[] IV = generaterandombytes (16);
byte[] Salt = generaterandombytes (16);
Creating an Encrypted object
SymmetricAlgorithm SMA = cryptohelp.createrijndael (password, salt);
SMA.IV = IV;
Writes IV and salt at the beginning of the output file
Fout. Write (Iv,0,iv. Length);
Fout. Write (Salt,0,salt. Length);
Create Hash encryption
HashAlgorithm hasher = SHA256. Create ();
using (CryptoStream cout = new CryptoStream (Fout,sma. CreateEncryptor (), CryptoStreamMode.Write),
Chash = new CryptoStream (stream.null,hasher,cryptostreammode.write))
{
BinaryWriter bw = new BinaryWriter (cout);
Bw. Write (lsize);
Bw. Write (Fc_tag);
Read-write byte block to encrypted stream buffer
while (read = Fin. Read (bytes,0,bytes. Length))!= 0)
{
cout. Write (Bytes,0,read);
Chash. Write (Bytes,0,read);
Value + = read;
}
Turn off the encrypted stream
Chash. Flush ();
Chash. Close ();
Read Hash
Byte[] hash = hasher. Hash;
Input file Write hash
cout. Write (Hash,0,hash. Length);
Close file stream
cout. Flush ();
cout. Close ();
}
}
}
[1] [2] Next page