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)); } } }
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."); } } }
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()); }