// şifrele
public static void SHA_Encrypt(string inFile, string outFile, string password)
{
try
{
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; // read input file number
int value = 0;
// Get the IV and salt
byte[] IV = GenerateRandomBytes(16);
byte[] salt = GenerateRandomBytes(16);
// create an encrypted objects
SymmetricAlgorithm sma = CreateRijndael(password, salt);
sma.IV = IV;
// at the beginning of the output file is written and salt IV
fout.Write(IV, 0, IV.Length);
fout.Write(salt, 0, salt.Length);
// Create a 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 byte block to the encryption stream buffer
while ((read = fin.Read(bytes, 0, bytes.Length)) != 0)
{
cout.Write(bytes, 0, read);
chash.Write(bytes, 0, read);
value += read;
}
// close the stream encryption
chash.Flush();
chash.Close();
// reads the hash
byte[] hash = hasher.Hash;
// input file is written to hash
cout.Write(hash, 0, hash.Length);
// close the file stream
cout.Flush();
cout.Close();
}
}
}
catch (IOException)
{
throw new IOException();
}
catch (Exception)
{
throw new EncryptException("An unkown error occurred while preparing the backups!");
}
}
//deşifrele
public static void SHA_Dencrypt(string inFile, string outFile, string password)
{
// Create a file stream open
using (FileStream fin = File.OpenRead(inFile),
fout = File.OpenWrite(outFile))
{
int size = (int)fin.Length;
byte[] bytes = new byte[BUFFER_SIZE];
int read = -1;
int value = 0;
int outValue = 0;
byte[] IV = new byte[16];
fin.Read(IV, 0, 16);
byte[] salt = new byte[16];
fin.Read(salt, 0, 16);
SymmetricAlgorithm sma = CreateRijndael(password, salt);
sma.IV = IV;
value = 32;
long lSize = -1;
// create a hash object, a checksum file
HashAlgorithm hasher = SHA256.Create();
using (CryptoStream cin = new CryptoStream(fin, sma.CreateDecryptor(), CryptoStreamMode.Read),
chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write))
{
// read the file length
BinaryReader br = new BinaryReader(cin);
lSize = br.ReadInt64();
ulong tag = br.ReadUInt64();
if (FC_TAG != tag)
throw new Exception("The file is corrupted");
long numReads = lSize / BUFFER_SIZE;
long slack = (long)lSize % BUFFER_SIZE;
for (int i = 0; i < numReads; ++i)
{
read = cin.Read(bytes, 0, bytes.Length);
fout.Write(bytes, 0, read);
chash.Write(bytes, 0, read);
value += read;
outValue += read;
}
if (slack > 0)
{
read = cin.Read(bytes, 0, (int)slack);
fout.Write(bytes, 0, read);
chash.Write(bytes, 0, read);
value += read;
outValue += read;
}
chash.Flush();
chash.Close();
fout.Flush();
fout.Close();
byte[] curHash = hasher.Hash;
// Get the hash comparison and old objects
byte[] oldHash = new byte[hasher.HashSize / 8];
read = cin.Read(oldHash, 0, oldHash.Length);
if ((oldHash.Length != read) || (!CheckByteArrays(oldHash, curHash)))
throw new EncryptException("The backup file is corrupted. Please create a new one.");
}
if (outValue != lSize)
throw new EncryptException("Backup couldn't loaded. Please delete this backup and create new one.");
}
}