Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void FlexEncrypt(string srcFile, string desDirectory, string password)
- {
- // Generate 32 Crypto Random bytes
- byte[] KeyIV = CryptoRandomFill(new byte[32]);
- // Get SHA256 Hash of password
- byte[] PasswordHash = SHA256(Encoding.UTF8.GetBytes(password));
- // Generate bit mask
- byte[] BitMask = Xor(KeyIV, PasswordHash);
- // Encrypt data with KeyIV
- using (AES128 aes = new AES128(KeyIV))
- {
- byte[] EncryptedData = aes.Encrypt(File.ReadAllBytes(srcFile));
- // Save EncryptedData in desDirectory
- File.WriteAllBytes(desDirectory + Path.GetFileName(srcFile) + ".en", EncryptedData);
- // Save the BitMask
- File.WriteAllBytes(desDirectory + Path.GetFileName(srcFile) + ".bm", BitMask);
- }
- }
- private static void FlexDecrypt(string srcFile, string desDirectory, string password)
- {
- // Get SHA256 Hash of password
- byte[] PasswordHash = SHA256(Encoding.UTF8.GetBytes(password));
- // Read BitMask
- byte[] BitMask = File.ReadAllBytes(Path.GetFileNameWithoutExtension(srcFile) + ".bm");
- // Resolve KeyIV
- byte[] KeyIV = Xor(PasswordHash, BitMask);
- // Decrypt data with KeyIV
- using (AES128 aes = new AES128(KeyIV))
- {
- byte[] DecryptedData = aes.Decrypt(File.ReadAllBytes(srcFile));
- // Save DecryptedData in desDirectory
- File.WriteAllBytes(desDirectory + Path.GetFileNameWithoutExtension(srcFile), DecryptedData);
- }
- }
- private static void FlexChange(string srcFile, string currentPassword, string newPassword)
- {
- // Get SHA256 of current password
- byte[] CrntPassHash = SHA256(Encoding.UTF8.GetBytes(currentPassword));
- // Read current BitMask
- byte[] BitMask = File.ReadAllBytes(Path.GetFileNameWithoutExtension(srcFile) + ".bm");
- // Resolve KeyIV
- byte[] KeyIV = Xor(CrntPassHash, BitMask);
- // Get SHA256 of new password
- byte[] NewPassHash = SHA256(Encoding.UTF8.GetBytes(newPassword));
- // Generate new BitMask
- byte[] NewBitMask = Xor(NewPassHash, KeyIV);
- // Replace/Overwrite old BitMask file
- File.WriteAllBytes(Path.GetDirectoryName(srcFile) + Path.GetFileNameWithoutExtension(srcFile) + ".bm", BitMask);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement