Beispiel #1
0
        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 DecryptFile(EncryptedFile input, FileStream output, ProgressReporter reporter = null)
        {
            var cert = new X509Certificate2(this.sender.PublicCertificate);

            if (cert == null)
            {
                reporter?.Log("Sender certificate error. Unable to verify integrity.");
            }
            else
            {
                reporter?.Log("Certificate located.");
                if (CertificateValidator.VerifyCertificate(cert) == false)
                {
                    reporter?.Log("Sender's certificate is INVALID. Continuing.");
                }

                reporter?.Log("Verifying file integrity...");
                RSACryptoServiceProvider publicKeyProvider = (RSACryptoServiceProvider)cert.PublicKey.Key;
                bool verifySuccess = EncryptedFileChecker.VerifySignature(input, publicKeyProvider.ExportParameters(false));
                if (verifySuccess)
                {
                    reporter?.Log("File verification: SUCCESS");
                }
                else
                {
                    reporter?.Log("File verification: FAILED");
                }

                reporter?.SetPercentage(25);
            }

            reporter?.Log("Decrypting file...");
            FileDecryptor decryptor = new FileDecryptor(this.currentUser.PrivateKey);

            decryptor.Decrypt(input, output, reporter.SetPercentage);
            reporter?.Log("File decryption complete.");
        }