예제 #1
0
 public void CreateRSACryptoServiceKeysTest()
 {
     (RSAParameters parameters, string publicKey, string privatePublicKeys) = CryptographyHelpers.CreateRSACryptoServiceKeys();
     Assert.IsNotNull(parameters);
     Assert.AreNotEqual <string>(publicKey, privatePublicKeys);
     Debug.WriteLine(publicKey);
     Debug.WriteLine(string.Empty);
     Debug.WriteLine(privatePublicKeys);
 }
예제 #2
0
        public void XmlSignatureTest()
        {
            const string _xmlFile = @"catalog.example.xml";

            Assert.IsTrue(File.Exists(_xmlFile));
            const string _publicKey = @"PubliKey.xml";

            Assert.IsTrue(File.Exists(_publicKey));
            const string _publicPrivateKeys = @"PubliPrivateKeys.xml";

            Assert.IsTrue(File.Exists(_publicPrivateKeys));
            XmlDocument _documentToSign = new XmlDocument();

            _documentToSign.Load(_xmlFile);
            const string _signedFileName = "SignedXmlFile.xml";

            if (File.Exists(_signedFileName))
            {
                File.Delete(_signedFileName);
            }
            string _rsaKeys = null;

            using (StreamReader _reader = new StreamReader(_publicPrivateKeys, System.Text.Encoding.UTF8))
                _rsaKeys = _reader.ReadToEnd();
            CryptographyHelpers.SignSaveXml(_documentToSign, _rsaKeys, _signedFileName);
            Assert.IsTrue(File.Exists(_signedFileName));
            XmlDocument _signedXmlDocument1 = CryptographyHelpers.LoadVerifyXml(_signedFileName);

            Assert.IsNotNull(_signedXmlDocument1);
            using (StreamReader _reader = new StreamReader(_publicKey, System.Text.Encoding.UTF8))
                _rsaKeys = _reader.ReadToEnd();
            XmlDocument _signedXmlDocument2 = CryptographyHelpers.LoadVerifyXml(_rsaKeys, _signedFileName);

            Assert.IsNotNull(_signedXmlDocument2);
            const string _signedModifiedFileName = "SignedXmlFileWithSpace.xml";

            AddSpace(_signedFileName, _signedModifiedFileName);
            Assert.ThrowsException <CryptographicException>(() => { XmlDocument _ = CryptographyHelpers.LoadVerifyXml(_rsaKeys, _signedModifiedFileName); });
        }
예제 #3
0
        public void EncryptDecryptDataTest()
        {
            //encrypt
            const string _inFileName = @"catalog.example.xml";
            FileInfo     _inFileInfo = new FileInfo(_inFileName);

            Assert.IsTrue(_inFileInfo.Exists);
            const string _encryptedFileName = "encryptedXmlFile.xml";

            if (File.Exists(_encryptedFileName))
            {
                File.Delete(_encryptedFileName);
            }
            ProgressMonitor _logger = new ProgressMonitor();
            TripleDESCryptoServiceProvider _tripleDesProvider = new TripleDESCryptoServiceProvider();

            Assert.AreEqual <int>(192, _tripleDesProvider.KeySize);
            CryptographyHelpers.EncryptData(_inFileName, _encryptedFileName, _tripleDesProvider.Key, _tripleDesProvider.IV, _logger);
            FileInfo _encryptedFileInfo = new FileInfo(_encryptedFileName);

            Assert.IsTrue(_encryptedFileInfo.Exists);
            Assert.AreEqual <long>(_inFileInfo.Length, _logger.ReportedValue);
            //decrypt
            const string _decryptedFileName = "decryptedXmlFile.xml";

            if (File.Exists(_decryptedFileName))
            {
                File.Delete(_decryptedFileName);
            }
            _logger = new ProgressMonitor();
            CryptographyHelpers.DecryptData(_encryptedFileName, _decryptedFileName, _tripleDesProvider.Key, _tripleDesProvider.IV, _logger);
            FileInfo _decryptedFileInfo = new FileInfo(_decryptedFileName);

            Assert.IsTrue(_decryptedFileInfo.Exists);
            Assert.AreEqual <long>(_decryptedFileInfo.Length, _logger.ReportedValue);
            Assert.AreEqual <long>(_decryptedFileInfo.Length, _inFileInfo.Length);
            //TODO Compare input and decrypted files. Must be equal.
        }