Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Sifrator
- {
- using System;
- using System.IO;
- using System.Security.Cryptography;
- public class AesCipher
- {
- public static byte[] AES_Encrypt(string output, byte[] bytesToBeEncrypted, byte[] passwordBytes)
- {
- byte[] encryptedBytes = null;
- byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
- using (var memoryStream = new MemoryStream())
- {
- using (RijndaelManaged AES = new RijndaelManaged())
- {
- AES.KeySize = 256;
- AES.BlockSize = 128;
- var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
- AES.Key = key.GetBytes(AES.KeySize / 8);
- AES.IV = key.GetBytes(AES.BlockSize / 8);
- AES.Mode = CipherMode.CBC;
- using (var cs = new CryptoStream(memoryStream, AES.CreateEncryptor(), CryptoStreamMode.Write))
- {
- byte[] tmp = passwordBytes.Concat(bytesToBeEncrypted).ToArray();
- //new byte[passwordBytes.Length + bytesToBeEncrypted.Length];
- cs.Write(tmp, 0, tmp.Length);
- cs.Close();
- }
- encryptedBytes = memoryStream.ToArray();
- }
- }
- return encryptedBytes;
- }
- public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
- {
- byte[] decryptedBytes = null;
- // Set your salt here, change it to meet your flavor:
- // The salt bytes must be at least 8 bytes.
- byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
- using (MemoryStream ms = new MemoryStream())
- {
- using (RijndaelManaged AES = new RijndaelManaged())
- {
- AES.KeySize = 256;
- AES.BlockSize = 128;
- var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
- AES.Key = key.GetBytes(AES.KeySize / 8);
- AES.IV = key.GetBytes(AES.BlockSize / 8);
- AES.Mode = CipherMode.CBC;
- using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
- {
- cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
- cs.FlushFinalBlock();
- cs.Close();
- }
- decryptedBytes = ms.ToArray();
- }
- }
- return decryptedBytes;
- }
- public static void EncryptFile(string filePath)
- {
- byte[] bytesToBeEncrypted = File.ReadAllBytes(filePath);
- Random rnd = new Random();
- byte[] passwordBytesENCR = new byte[2];//Encoding.UTF8.GetBytes(password);
- rnd.NextBytes(passwordBytesENCR);
- // Hash the password with SHA256
- passwordBytesENCR = SHA256.Create().ComputeHash(passwordBytesENCR);
- string fileEncrypted = "crypted_" + filePath.Split('\\')[filePath.Split('\\').Length - 1];
- byte[] bytesEncrypted = AES_Encrypt(fileEncrypted, bytesToBeEncrypted, passwordBytesENCR);
- File.WriteAllBytes(fileEncrypted, bytesEncrypted);
- }
- public static void DecryptFile(string filePath)
- {
- byte[] bytesToBeDecrypted = File.ReadAllBytes(filePath);
- byte[] passwordBytesDECR = new Byte[2]; //Encoding.UTF8.GetBytes(password);
- for (int i = 0; i < 2; ++i)
- passwordBytesDECR[i] = bytesToBeDecrypted[i];
- passwordBytesDECR = SHA256.Create().ComputeHash(passwordBytesDECR);
- byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytesDECR);
- string file = "decrypted_" + filePath.Split('\\')[filePath.Split('\\').Length - 1];
- File.WriteAllBytes(file, bytesDecrypted);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement