Пример #1
0
        public void Decipher_PrivateKeyIsIncorrect()
        {
            var rsaFileDecipher = new RsaFileDecipher(new FileFactory(4));
            var manager         = new DecipherManager(environmentHelper.Object, rsaFileDecipher, messageHelper.Object, fileUnifier);
            var bytes           = new byte[] {
                4, 7, 0, 54, 0, 0, 0, 0, 0, 0, 0,                                 // rsa header
                6, 53, 47, 52, 49, 87, 159, 197, 184, 234, 6, 229, 57, 107, 51, 226, 235, 36, 89,
                167, 102, 203, 58, 114, 235, 109, 203, 136, 37, 136, 233, 169, 128, 211, 14, 226,
                139, 74, 33, 212, 6, 28, 102, 27, 32, 141, 244, 197, 74, 241, 8, 73, 220, 55, 193,
                12, 0, 0, 240, 100, 185, 0, 0
            };
            // public key: 05#2039A8720B186BDD
            // private key: 0671E7E40596528D#2039A8720B186BDD
            var d             = BigNumber.FromBytes(new[] { 113, 4, 172, 254, 73, 26, 110 });
            var n             = BigNumber.FromBytes(new[] { 173, 144, 32, 44, /*38,*/ 129, 183 });
            var privateKey    = new PrivateKey(d, n);
            var inputFileName = Path.Combine(testFolder, "file.rsa");

            File.WriteAllBytes(inputFileName, bytes);
            var outputDirectoryPath = Path.Combine(testFolder, "output");
            var temporaryFileName   = Path.Combine(testFolder, "temporary.bin");

            environmentHelper.Setup(x => x.GetTempFileName()).Returns(temporaryFileName);

            manager.Decipher(privateKey, inputFileName, outputDirectoryPath);

            messageHelper.Verify(x => x.Show("Incorrect private key.", "Неправильный закрытый ключ."));
        }
Пример #2
0
        public void Decipher_VersionIsTooHigh()
        {
            var fileInfo = new Mock <IFileInfo>();

            fileInfo.Setup(x => x.Exists).Returns(true);
            fileInfo.Setup(x => x.FullName).Returns("hello.rsa");
            fileInfo.Setup(x => x.Extension).Returns(".rsa");

            var rsaFileDecipher = new Mock <IRsaFileDecipher>();

            rsaFileDecipher.Setup(x => x.Decipher(It.IsAny <PrivateKey>(), It.IsAny <string>(), It.IsAny <string>())).Throws(
                new TooHighVersionException("hello"));

            var manager    = new DecipherManager(environmentHelper.Object, rsaFileDecipher.Object, messageHelper.Object, fileUnifier);
            var d          = BigNumber.FromInt(113);
            var n          = BigNumber.FromInt(173);
            var privateKey = new PrivateKey(d, n);

            var outputDirectoryPath = Path.Combine(testFolder, "output");

            manager.Decipher(privateKey, "input.rsa", outputDirectoryPath);

            messageHelper.Verify(
                x => x.Show("To decrypt a file needs a new program.", "Для расшифровки файла нужна более новая программа."));
        }
Пример #3
0
        public void Decipher_ProcessWasStopped()
        {
            var rsaFileDecipher = new Mock <IRsaFileDecipher>();

            rsaFileDecipher.Setup(x => x.Status).Returns(ProcessStatus.Stopped);
            var manager    = new DecipherManager(environmentHelper.Object, rsaFileDecipher.Object, messageHelper.Object, fileUnifier);
            var d          = BigNumber.FromInt(113);
            var n          = BigNumber.FromInt(173);
            var privateKey = new PrivateKey(d, n);

            var outputDirectoryPath = Path.Combine(testFolder, "output");

            manager.Decipher(privateKey, "input.rsa", outputDirectoryPath);

            messageHelper.Verify(x => x.Show("Process was stopped.", "Процесс был остановлен."));
        }
        private void StartDecipher()
        {
            var manager = new DecipherManager(environmentHelper, rsaFileDecipher, messageHelper, fileUnifier);

            manager.Decipher(privateKey, form.InputFileName, form.OutputDirectoryPath);
            if (rsaFileDecipher.Status == ProcessStatus.Complete)
            {
                if (form.CloseWindowAfterComlete)
                {
                    form.DialogResult = DialogResult.Cancel;
                }
            }
            commandsContainer.RefreshDirectoryCommand.Execute();
            form.ProgressPercent = 0;
            form.DecipherEnabled = true;
        }
Пример #5
0
        public void Decipher_ErrorWhenDecipheringFile()
        {
            var rsaFileDecipher = new Mock <IRsaFileDecipher>();

            rsaFileDecipher.Setup(x => x.Decipher(It.IsAny <PrivateKey>(), It.IsAny <string>(), It.IsAny <string>())).Throws(
                new Exception());
            var manager        = new DecipherManager(environmentHelper.Object, rsaFileDecipher.Object, messageHelper.Object, fileUnifier);
            var d              = BigNumber.FromBytes(new[] { 113 });
            var n              = BigNumber.FromBytes(new[] { 173 });
            var privateKey     = new PrivateKey(d, n);
            var sourceFileName = Path.Combine(testFolder, "sourceFile.rsa");

            File.WriteAllText(sourceFileName, @"hello");
            var outputDirectoryPath = Path.Combine(testFolder, "path");

            manager.Decipher(privateKey, sourceFileName, outputDirectoryPath);

            messageHelper.Verify(x => x.Show("Error decipher RSA file.", "Ошибка при расшифровке RSA файла."));
        }
Пример #6
0
        public void Cipher_Decipher()
        {
            var outputFileName    = Path.Combine(testFolder, "outputFileName.rsa");
            var temporaryFileName = Path.Combine(testFolder, "temporary.bin");

            environmentHelper.Setup(x => x.GetTempFileName()).Returns(temporaryFileName);
            var fileFactory   = new FileFactory(4);
            var rsaFileCipher = new RsaFileCipher(fileFactory);
            var fileUnifier   = new FileUnifier();
            var manager       = new CipherManager(rsaFileCipher, fileUnifier, environmentHelper.Object, messageHelper.Object);
            // public key: 05#B781262C2090AD
            // private key: 6E1A49FEAC0471#B781262C2090AD
            var e              = BigNumber.FromInt(5);
            var d              = BigNumber.FromBytes(new[] { 113, 4, 172, 254, 73, 26, 110 });
            var n              = BigNumber.FromBytes(new[] { 173, 144, 32, 44, 38, 129, 183 });
            var publicKey      = new PublicKey(e, n);
            var privateKey     = new PrivateKey(d, n);
            var sourceFileName = Path.Combine(testFolder, "file.bin");

            File.WriteAllBytes(sourceFileName, new byte[] { 45, 129, 240 });
            var directoryInfoWrapper = new DirectoryInfoWrapper(testFolder);
            var fileInfoWrapper      = new FileInfoWrapper(new FileInfo(sourceFileName), directoryInfoWrapper);
            var fileEntity           = new FileEntity(fileInfoWrapper);

            manager.Cipher(publicKey, new FileSystemEntity[] { fileEntity }, outputFileName);

            var decipherManager = new DecipherManager(environmentHelper.Object, new RsaFileDecipher(fileFactory),
                                                      messageHelper.Object, fileUnifier);
            var outputPath = Path.Combine(testFolder, "outputPath");

            decipherManager.Decipher(privateKey, outputFileName, outputPath);

            Assert.IsTrue(Directory.Exists(outputPath));
            var fileName = Path.Combine(outputPath, "file.bin");

            Assert.IsTrue(File.Exists(fileName));
            TestHelper.CheckFile(fileName, new byte[] { 45, 129, 240 });
        }