public void DeleteKeyFile()
        {
            File.Delete(symmetricKeyFile);
            File.Delete(keyedHashKeyFile);

            cryptographyManager = null;
        }
        public void SetUp()
        {
            hashProviders = new Dictionary<string, IHashProvider>();
            symmetricCrytoProviders = new Dictionary<string, ISymmetricCryptoProvider>();

            CreateKeyFile(symmetricKeyFile);
            CreateKeyFile(keyedHashKeyFile);

            configSource = ConfigurationSourceFactory.Create();

            HashProviderFactory factory = new HashProviderFactory(configSource);
            defaultHashProvider = factory.Create(hashInstance);
            hashProviders.Add(hashInstance, defaultHashProvider);

            SymmetricCryptoProviderFactory symmfactory = new SymmetricCryptoProviderFactory(configSource);
            defaultSymmetricCryptoProvider = symmfactory.Create(symmInstance);
            algorithSymmetricCryptoProvider = symmfactory.Create(symmetricAlgorithm1);

            symmetricCrytoProviders.Add(symmInstance, defaultSymmetricCryptoProvider);
            symmetricCrytoProviders.Add(symmetricAlgorithm1, algorithSymmetricCryptoProvider);

            var container = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

            cryptographyManager = container.GetInstance<CryptographyManager>();
        }
		public async Task TestIvGeneration()
		{
			var apiKey = Environment.GetEnvironmentVariable("baelor-test-apikey");
			var fingerprints = await FingerprintManager.GenerateFingerprint(apiKey);
			var cryptoManagaer = new CryptographyManager(new FingerprintManager(fingerprints));

			var ivA = cryptoManagaer.GenerateNewIv();
			var ivB = cryptoManagaer.GenerateNewIv();

			Assert.False(ivA == ivB);
			Assert.True(ivA.Length * 8 == CryptographyManager.BlockBitSize);
			Assert.True(ivB.Length * 8 == CryptographyManager.BlockBitSize);
		}
		public async Task TestEncryptionAndDecryption(string data)
		{
			var apiKey = Environment.GetEnvironmentVariable("baelor-test-apikey");
			var fingerprints = await FingerprintManager.GenerateFingerprint(apiKey);
			var cryptoManagaer = new CryptographyManager(new FingerprintManager(fingerprints));

			var key = cryptoManagaer.GenerateNewKey();
			var iv = cryptoManagaer.GenerateNewIv();

			var encryptedDataFingerprints = cryptoManagaer.Encrypt(Encoding.ASCII.GetBytes(data), key, iv);
			var formattedEncryptedFingerprints = "";
			foreach (var fingerprint in encryptedDataFingerprints)
				formattedEncryptedFingerprints += fingerprint.Lyric + "\n";
			formattedEncryptedFingerprints = formattedEncryptedFingerprints.TrimEnd('\n');

			var decryptedData = cryptoManagaer.Decrypt(formattedEncryptedFingerprints, key, iv);
			var decryptedPlainText = Encoding.ASCII.GetString(decryptedData);

			Assert.True(data == decryptedPlainText);
		}
示例#5
0
		public void Main(string[] args)
		{
			var fingerprintManager = new FingerprintManager("fingerprints.json");
			var cryptoManager = new CryptographyManager(fingerprintManager);
			var key = cryptoManager.GenerateNewKey();
			var iv = cryptoManager.GenerateNewIv();

			System.Console.Write("Enter Text to be encrypted: ");
			var input = System.Console.ReadLine();
			var encryptedFingerprintData = cryptoManager.Encrypt(Encoding.ASCII.GetBytes(input), key, iv);

			var output = "";
			foreach (var fingerprint in encryptedFingerprintData)
				output += fingerprint.Lyric + "\n";
			output = output.TrimEnd('\n');

			var decryptedOutput = cryptoManager.Decrypt(output, key, iv);

			System.Console.WriteLine();
			System.Console.WriteLine("=== ENCRYPTED DATA ===");
			System.Console.WriteLine(output);
			System.Console.WriteLine("======================");

			System.Console.WriteLine();
			System.Console.WriteLine("=== DECRYPTED DATA ===");
			System.Console.WriteLine(Encoding.ASCII.GetString(decryptedOutput));
			System.Console.WriteLine("=====================");

			System.Console.WriteLine();
			System.Console.WriteLine("=== METADATA ===");
			System.Console.WriteLine("Crypto Key: {0}", BitConverter.ToString(key, 0, key.Length).Replace("-", ""));
			System.Console.WriteLine("Crypto Iv: {0}", BitConverter.ToString(iv, 0, iv.Length).Replace("-", ""));
			System.Console.WriteLine("================");

			System.Console.ReadLine();
		}