public void Cipher_FileContainsThreeBytes()
        {
            var e          = BigNumber.FromBytes(new[] { 17 });
            var n          = BigNumber.FromBytes(new[] { 3, 81, 217, 6, 4, 255, 114, 123, 36 });
            var key        = new PublicKey(e, n);
            var sourceFile = Path.Combine(testFolder, "source.txt");

            File.WriteAllBytes(sourceFile, new byte[] { 49, 50, 51 });
            var destinationFile = Path.Combine(testFolder, "destination.rsa");

            cipher = new RsaFileCipher(new FileFactory(5));
            cipher.Cipher(key, sourceFile, destinationFile);

            var expectedBytes = new byte[] {
                5,                                         // версия
                9, 0,                                      // размер блока
                3, 0, 0, 0,                                // размер исходного файла
                0, 0, 0, 0,                                // размер исходного файла
                32, 44, 185, 98,                           // хэш
                172, 89, 7, 91,                            // хэш
                150, 75, 7, 21,                            // хэш
                45, 35, 75, 112,                           // хэш
                51, 136, 212, 124, 2, 205, 81, 39, 34
            };

            TestHelper.CheckFile(destinationFile, expectedBytes);
            Assert.AreEqual(ProcessStatus.Complete, cipher.Status);
        }
        public void Cipher_FileContainsFiveBytes()
        {
            var e          = BigNumber.FromBytes(new[] { 7 });
            var n          = BigNumber.FromBytes(new[] { 217, 118, 184, 189, 114, 147, 142, 61 });
            var key        = new PublicKey(e, n);
            var sourceFile = Path.Combine(testFolder, "source.txt");

            File.WriteAllBytes(sourceFile, new byte[] { 104, 101, 108, 108, 111 });
            var destinationFile = Path.Combine(testFolder, "destination.rsa");

            cipher = new RsaFileCipher(new FileFactory(10));
            cipher.Cipher(key, sourceFile, destinationFile);

            var expectedBytes = new byte[] {
                10,                                         // версия
                8, 0,                                       // размер блока
                5, 0, 0, 0,                                 // размер исходного файла
                0, 0, 0, 0,                                 // размер исходного файла
                93, 65, 64, 42,                             // хэш
                188, 75, 42, 118,                           // хэш
                185, 113, 157, 145,                         // хэш
                16, 23, 197, 146,                           // хэш
                18, 85, 184, 86, 212, 72, 244, 4
            };

            TestHelper.CheckFile(destinationFile, expectedBytes);
            Assert.AreEqual(ProcessStatus.Complete, cipher.Status);
        }
        public void Cipher_SourceFileDoesntExist()
        {
            var e   = BigNumber.FromBytes(new[] { 17 });
            var n   = BigNumber.FromBytes(new[] { 19, 241, 93, 252, 193, 101, 145, 14 });
            var key = new PublicKey(e, n);

            cipher = new RsaFileCipher(new FileFactory(56));
            Assert.Throws(typeof(SourceFileNotFoundException), () => cipher.Cipher(key, "hello", "world"));
            Assert.AreEqual(ProcessStatus.Stopped, cipher.Status);
        }
        public void Cipher_CheckStatusIfErrorOccured()
        {
            var e          = BigNumber.FromBytes(new[] { 7 });
            var n          = BigNumber.FromBytes(new[] { 217, 118, 184, 189, 114, 147, 142, 61 });
            var key        = new PublicKey(e, n);
            var sourceFile = Path.Combine(testFolder, "source.txt");

            File.WriteAllBytes(sourceFile, new byte[] { 104, 101, 108, 108, 111 });
            var destinationFile = Path.Combine(testFolder, "destination.rsa");
            var fileFactory     = new Mock <IFileFactory>();

            fileFactory.Setup(x => x.CreateFileReader(It.IsAny <string>(), It.IsAny <short>())).Throws(new Exception());
            cipher = new RsaFileCipher(fileFactory.Object);
            Assert.Throws(typeof(Exception), () => cipher.Cipher(key, sourceFile, destinationFile));
            Assert.AreEqual(ProcessStatus.Stopped, cipher.Status);
        }
        public void Decipher_ProcessWasStopped()
        {
            // public key: 0101#0F433A164419165B
            // private key: 02CA8D18E051C519#0F433A164419165B
            var sourceFileName      = Path.Combine(testFolder, "source.rsa");
            var destinationFileName = Path.Combine(testFolder, "destination.bin");
            var e               = BigNumber.FromBytes(new[] { 1, 1 });
            var d               = BigNumber.FromBytes(new[] { 25, 197, 81, 224, 24, 141, 202, 2 });
            var n               = BigNumber.FromBytes(new[] { 91, 22, 25, 68, 22, 58, 67, 15 });
            var privateKey      = new PrivateKey(d, n);
            var publicKey       = new PublicKey(e, n);
            var cipher          = new RsaFileCipher(fileFactory);
            var initialFileName = Path.Combine(testFolder, "initial.exe");

            File.WriteAllBytes(initialFileName, new byte[1000]);
            cipher.Cipher(publicKey, initialFileName, sourceFileName);
            decipher.BlockCompleted += (sender, args) => decipher.Stop();
            decipher.Decipher(privateKey, sourceFileName, destinationFileName);
            Assert.AreEqual(ProcessStatus.Stopped, decipher.Status);
        }
Esempio n. 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 });
        }
 public void Cipher_KeyIsNull()
 {
     cipher = new RsaFileCipher(new FileFactory(56));
     Assert.Throws(typeof(ArgumentNullException), () => cipher.Cipher(null, "hello", "world"));
     Assert.AreEqual(ProcessStatus.Stopped, cipher.Status);
 }