Advertisement
TizzyT

FlexCryptMethods -TizzyT

Oct 21st, 2018
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1.         private static void FlexEncrypt(string srcFile, string desDirectory, string password)
  2.         {
  3.             // Generate 32 Crypto Random bytes
  4.             byte[] KeyIV = CryptoRandomFill(new byte[32]);
  5.             // Get SHA256 Hash of password
  6.             byte[] PasswordHash = SHA256(Encoding.UTF8.GetBytes(password));
  7.             // Generate bit mask
  8.             byte[] BitMask = Xor(KeyIV, PasswordHash);
  9.             // Encrypt data with KeyIV
  10.             using (AES128 aes = new AES128(KeyIV))
  11.             {
  12.                 byte[] EncryptedData = aes.Encrypt(File.ReadAllBytes(srcFile));
  13.                 // Save EncryptedData in desDirectory
  14.                 File.WriteAllBytes(desDirectory + Path.GetFileName(srcFile) + ".en", EncryptedData);
  15.                 // Save the BitMask
  16.                 File.WriteAllBytes(desDirectory + Path.GetFileName(srcFile) + ".bm", BitMask);
  17.             }
  18.         }
  19.  
  20.         private static void FlexDecrypt(string srcFile, string desDirectory, string password)
  21.         {
  22.             // Get SHA256 Hash of password
  23.             byte[] PasswordHash = SHA256(Encoding.UTF8.GetBytes(password));
  24.             // Read BitMask
  25.             byte[] BitMask = File.ReadAllBytes(Path.GetFileNameWithoutExtension(srcFile) + ".bm");
  26.             // Resolve KeyIV
  27.             byte[] KeyIV = Xor(PasswordHash, BitMask);
  28.             // Decrypt data with KeyIV
  29.             using (AES128 aes = new AES128(KeyIV))
  30.             {
  31.                 byte[] DecryptedData = aes.Decrypt(File.ReadAllBytes(srcFile));
  32.                 // Save DecryptedData in desDirectory
  33.                 File.WriteAllBytes(desDirectory + Path.GetFileNameWithoutExtension(srcFile), DecryptedData);
  34.             }
  35.         }
  36.  
  37.         private static void FlexChange(string srcFile, string currentPassword, string newPassword)
  38.         {
  39.             // Get SHA256 of current password
  40.             byte[] CrntPassHash = SHA256(Encoding.UTF8.GetBytes(currentPassword));
  41.             // Read current BitMask
  42.             byte[] BitMask = File.ReadAllBytes(Path.GetFileNameWithoutExtension(srcFile) + ".bm");
  43.             // Resolve KeyIV
  44.             byte[] KeyIV = Xor(CrntPassHash, BitMask);
  45.             // Get SHA256 of new password
  46.             byte[] NewPassHash = SHA256(Encoding.UTF8.GetBytes(newPassword));
  47.             // Generate new BitMask
  48.             byte[] NewBitMask = Xor(NewPassHash, KeyIV);
  49.             // Replace/Overwrite old BitMask file
  50.             File.WriteAllBytes(Path.GetDirectoryName(srcFile) + Path.GetFileNameWithoutExtension(srcFile) + ".bm", BitMask);
  51.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement