private void decodeToolStripMenuItem_Click(object sender, EventArgs e) { if (FolderList.SelectedNode != null && FolderList.SelectedNode.Tag != null) { TreeNode SelectedNode = FolderList.SelectedNode; var ext = (Path.GetExtension(SelectedNode.Tag.ToString()) ?? string.Empty).ToLower(); if (FileCryptor.DRIVE_CRYPT_EXTENSTION == ext) { var atributes = File.GetAttributes(SelectedNode.Tag.ToString()); if ((atributes & FileAttributes.Directory) != FileAttributes.Directory) { while (IsFileLocked(SelectedNode.Tag.ToString())) { Thread.Sleep(100); } string filePath = SelectedNode.Tag.ToString(); string newFilePath = filePath.Replace(GDriveManager.MySharingFolder + Path.DirectorySeparatorChar, ""); newFilePath = newFilePath.Replace(GDriveManager.SharedWithMeFolder + Path.DirectorySeparatorChar, ""); newFilePath = newFilePath.Replace(FileCryptor.DRIVE_CRYPT_EXTENSTION, ""); FileCryptor.DecryptFile(SelectedNode.Tag.ToString(), newFilePath, _authorizationForm._userCryptor); } } } else { MessageBox.Show("Select the item (left click on it) and try again!", "Drive Crypt", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
public void onCreateDecryptedEvent(object source, FileSystemEventArgs e) { var ext = (Path.GetExtension(e.FullPath) ?? string.Empty).ToLower(); if (_extensionsToBeIgnoredByWatcher[0] == ext) { string newPath = this._directoryPath + "\\Shared with me"; FileAttributes atributes = File.GetAttributes(e.FullPath); if ((atributes & FileAttributes.Directory) != FileAttributes.Directory) { while (IsFileLocked(e.FullPath)) { //MessageBox.Show("The requested file " + Path.GetFileName(e.FullPath) + " already exists and is used by another process!", "Drive Crypt", MessageBoxButtons.OK, MessageBoxIcon.Information); //return; Thread.Sleep(100); } string newFilePath = e.FullPath; string decryptedFile = FileCryptor.DecryptFile(e.FullPath, _authorizationForm._userCryptor); string[] decryptedFileName = decryptedFile.Split(Path.DirectorySeparatorChar); string toDelete = newFilePath; newPath = newFilePath.Replace(newPath, this._directoryPath); string[] fileName = newPath.Split(Path.DirectorySeparatorChar); newPath = newPath.Replace(fileName.Last(), decryptedFileName.Last()); toDelete = toDelete.Replace(fileName.Last(), decryptedFileName.Last()); File.Copy(decryptedFile, newPath, true); File.Delete(toDelete); while (IsFileLocked(newPath)) { //MessageBox.Show("The requested file " + Path.GetFileName(e.FullPath) + " already exists and is used by another process!", "Drive Crypt", MessageBoxButtons.OK, MessageBoxIcon.Information); //return; Thread.Sleep(100); } } } }
public void EncryptFile(OriginalFile input, FileStream output, ProgressReporter reporter = null) { var cert = new X509Certificate2(this.receiver.PublicCertificate); if (cert == null) { reporter?.Log("Receiver certificate error. Aborting."); } else { reporter?.Log("Verifying certificate..."); if (CertificateValidator.VerifyCertificate(cert) is false) { reporter?.Log("Receiver's certificate is invalid. Aborting."); } else { reporter?.Log("Receiver's certificate is valid."); reporter?.Log("Encrypting file..."); FileCryptor cryptor = new FileCryptor(this.currentUser.PrivateKey, ((RSACryptoServiceProvider)cert.PublicKey.Key).ExportParameters(false)); cryptor.Encrypt(input, output, this.combo, reporter.SetPercentage); reporter?.Log("File encryption complete."); } } }
public void TestTwofishFileDecryption() { byte[] data = new byte[10000]; new Random().NextBytes(data); // fill random data using (var senderRsa = new RSACryptoServiceProvider()) { var senderPrivateKey = senderRsa.ExportParameters(true); using (var receiverRsa = new RSACryptoServiceProvider()) { var receiverPrivateKey = receiverRsa.ExportParameters(true); var receiverPublicKey = receiverRsa.ExportParameters(false); CryptCombo combo = new CryptCombo(MD5.Create(), new TwofishMachine()); MemoryStream cryptedFileStream = new MemoryStream(); OriginalFile originFile = new OriginalFile(new MemoryStream(data), ".java"); FileCryptor cryptor = new FileCryptor(senderPrivateKey, receiverPublicKey); cryptor.Encrypt(originFile, cryptedFileStream, combo); MemoryStream decryptedStream = new MemoryStream(); EncryptedFile newCryptedFile = EncryptedFileChecker.Parse(cryptedFileStream); FileDecryptor decryptor = new FileDecryptor(receiverPrivateKey); decryptor.Decrypt(newCryptedFile, decryptedStream); Assert.IsTrue(decryptedStream.ToArray().SequenceEqual(data)); } } }
private static void FileDecrypt() { Console.WriteLine("Введите приватную экспоненту"); var d = new BigInt(Console.ReadLine()); Console.WriteLine("Введите модуль"); var n = new BigInt(Console.ReadLine()); Console.WriteLine("Введите имя файла"); FileCryptor.Decrypt(Console.ReadLine(), d, n); }
private static void FileEncrypt() { Console.WriteLine("Введите простые числа P и Q"); var p = ulong.Parse(Console.ReadLine() ?? throw new InvalidOperationException("Пустая строка - число")); var q = ulong.Parse(Console.ReadLine() ?? throw new InvalidOperationException("Пустая строка - число")); var fc = new FileCryptor(p, q); Console.WriteLine("Введите имя файла"); fc.Encrypt(Console.ReadLine()); }
private void button2_Click(object sender, EventArgs e) { var ofd = new OpenFileDialog(); ofd.InitialDirectory = _directoryPath; ofd.Filter = "Drive Crypt Files(*" + FileCryptor.DRIVE_CRYPT_EXTENSTION + ") | *" + FileCryptor.DRIVE_CRYPT_EXTENSTION; ofd.FilterIndex = 1; if (ofd.ShowDialog() == DialogResult.OK) { FileCryptor.DecryptFile(ofd.FileName, _authorizationForm._userCryptor); } }
// Encode File private void button1_Click(object sender, EventArgs e) { var ofd = new OpenFileDialog(); ofd.InitialDirectory = _directoryPath; ofd.Filter = "All Files(*.*) | *.*"; ofd.FilterIndex = 1; if (ofd.ShowDialog() == DialogResult.OK) { FileCryptor.EncryptFile(ofd.FileName, _authorizationForm._userCryptor); } }
public void onCreateEncryptEvent(object source, FileSystemEventArgs e) { string newPath = this._directoryPath + "\\My sharing"; FileAttributes atributes; try { atributes = File.GetAttributes(e.FullPath); } catch { return; } if ((atributes & FileAttributes.Directory) != FileAttributes.Directory) { var ext = (Path.GetExtension(e.FullPath) ?? string.Empty).ToLower(); if (!_extensionsToBeIgnoredByWatcher.Any(ext.Equals)) { while (IsFileLocked(e.FullPath)) { //MessageBox.Show("The requested file " + Path.GetFileName(e.FullPath) + " already exists and is used by another process!", "Drive Crypt", MessageBoxButtons.OK, MessageBoxIcon.Information); //return; Thread.Sleep(100); } string newFilePath = e.FullPath; newPath = newFilePath.Replace(newPath, this._directoryPath); System.IO.FileInfo file = new System.IO.FileInfo(newPath); file.Directory.Create(); File.Copy(e.FullPath, newPath, true); while (IsFileLocked(newPath)) { //MessageBox.Show("The requested file " + Path.GetFileName(e.FullPath) + " already exists and is used by another process!", "Drive Crypt", MessageBoxButtons.OK, MessageBoxIcon.Information); //return; Thread.Sleep(100); } } if (!_extensionsToBeIgnoredByWatcher.Any(ext.Equals)) { if ((atributes & FileAttributes.Directory) != FileAttributes.Directory) { FileCryptor.EncryptFile(e.FullPath, _authorizationForm._userCryptor); File.Delete(e.FullPath); } } } }
private void share_Click(object sender, EventArgs e) { var ofd = new OpenFileDialog(); ofd.InitialDirectory = _directoryPath; ofd.Filter = "All Files(*.*) | *.*"; ofd.FilterIndex = 1; if (ofd.ShowDialog() == DialogResult.OK) { var emailToShare = emailInput.Text; if (!IsValidEmail(emailToShare)) { MessageBox.Show("Invalid email address!", "Drive Crypt", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } GDriveManager.ShareFile(ofd.FileName, emailToShare, _authorizationForm._userInfo.Name); var userId = Base64Utils.EncodeBase64(emailToShare); var shareKeyCryptor = new UserCryptor(userId); var keyFilePath = GetUserKeysFolder() + Path.DirectorySeparatorChar + userId + UserCryptor.PUB_KEY_EXTENSION; if (File.Exists(keyFilePath)) { shareKeyCryptor.LoadPublicKey(keyFilePath); } else { MessageBox.Show("The requested user did not share his keys with you yet!", "Drive Crypt", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } var keyFilename = FileCryptor.PrepareKeyForSharing(ofd.FileName, _authorizationForm._userCryptor, shareKeyCryptor); var keyFilenameWithoutPath = Path.GetFileName(keyFilename); GDriveManager.UploadFile(keyFilename, keyFilenameWithoutPath); GDriveManager.ShareFile(keyFilename, emailToShare, _authorizationForm._userInfo.Name); } }
private void shareToolStripMenuItem_Click(object sender, EventArgs e) { if (FolderList.SelectedNode != null) { TreeNode SelectedNode = FolderList.SelectedNode; string FilePath = SelectedNode.Tag.ToString(); var emailToShare = emailInput.Text; if (!IsValidEmail(emailToShare)) { MessageBox.Show("Invalid email address!", "Drive Crypt", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } GDriveManager.ShareFile(FilePath, emailToShare, _authorizationForm._userInfo.Name); var userId = Base64Utils.EncodeBase64(emailToShare); var shareKeyCryptor = new UserCryptor(userId); var keyFilePath = GetUserKeysFolder() + Path.DirectorySeparatorChar + userId + UserCryptor.PUB_KEY_EXTENSION; if (File.Exists(keyFilePath)) { shareKeyCryptor.LoadPublicKey(keyFilePath); } else { MessageBox.Show("The requested user did not share his keys with you yet!", "Drive Crypt", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } var keyFilename = FileCryptor.PrepareKeyForSharing(FilePath, _authorizationForm._userCryptor, shareKeyCryptor); var keyFilenameWithoutPath = Path.GetFileName(keyFilename); GDriveManager.UploadFile(keyFilename, keyFilenameWithoutPath); GDriveManager.ShareFile(keyFilename, emailToShare, _authorizationForm._userInfo.Name); } }