Ejemplo n.º 1
0
        // Тест метод за успешна RSA декрипција со јавен клуч
        public string Decrypt(EncryptedPacket encryptedBlock)
        {
            var rsa        = (RSACryptoServiceProvider)_certForUJP.PrivateKey;
            var privateKey = rsa.ExportParameters(true);
            var rsaParams  = new RsaWithRsaParameterKey {
                PrivateKey = privateKey
            };
            var decrypted = _hybrid.DecryptData(encryptedBlock, rsaParams);

            return(Encoding.UTF8.GetString(decrypted));
        }
Ejemplo n.º 2
0
        public string DecryptSoapBody(byte[] encryptedData, byte[] sessionKey, byte[] iVector, RsaWithRsaParameterKey rsaParams)
        {
            var encryptedBlock = new EncryptedPacket();

            encryptedBlock.EncryptedData       = encryptedData;
            encryptedBlock.EncryptedSessionKey = sessionKey;
            encryptedBlock.Iv        = iVector;
            encryptedBlock.RsaParams = rsaParams;

            var hybrid          = new HybridEncryption();
            var decrpyted       = hybrid.DecryptData(encryptedBlock, encryptedBlock.RsaParams);
            var decryptedString = Encoding.UTF8.GetString(decrpyted);

            return(decryptedString);
        }
        private static void TestHybrid()
        {
            const string original = "Very secret and important information that must not fall in the hands of the enemy.";

            var rsaParams = new RSAWithRSAParameterKey();

            rsaParams.AssignNewKey();

            var hybrid = new HybridEncryption();

            var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), rsaParams);
            var decryptedBlock = hybrid.DecryptData(encryptedBlock, rsaParams);

            Console.WriteLine($"Original Message: {original}");
            Console.WriteLine($"Encrypted Block Data: {Convert.ToBase64String(encryptedBlock.EncryptedData)}");
            Console.WriteLine($"Decrypted Block: {Convert.ToBase64String(decryptedBlock)}");
            Console.WriteLine($"Decrypted Message: {Encoding.UTF8.GetString(decryptedBlock)}");
        }
        public void HybridEncryption_Test_Pass()
        {
            const string expected = "Very secret and important information that can not fall into the wrong hands.";

            using (var encryption = new HybridEncryption())
            {
                (RSAParameters, RSAParameters)encryptionKeys = new Helpers().GenerateRSAKeys();
                (RSAParameters, RSAParameters)signingnKeys   = new Helpers().GenerateRSAKeys();

                var    encryptedBlock    = encryption.EncryptData(Encoding.UTF8.GetBytes(expected), encryptionKeys.Item1, signingnKeys.Item2);
                var    decrypted         = encryption.DecryptData(encryptedBlock, encryptionKeys.Item2, signingnKeys.Item1);
                string actual            = Encoding.UTF8.GetString(decrypted);
                string encryptedAsString = Encoding.UTF8.GetString(encryptedBlock.EncryptedData);

                Assert.AreEqual(expected, actual);
                Assert.AreNotEqual(expected, encryptedAsString);
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            const string original = "Very secret and important information that can not fall into the wrong hands.";

            var rsaParams = new RsaWithRsaParameterKey();

            rsaParams.AssignNewKey();

            var hybrid = new HybridEncryption();

            var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), rsaParams);
            var decrpyted      = hybrid.DecryptData(encryptedBlock, rsaParams);

            Console.WriteLine("Hybrid Encryption Demonstration in .NET");
            Console.WriteLine("---------------------------------------");
            Console.WriteLine();
            Console.WriteLine("Original Message = " + original);
            Console.WriteLine();
            Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrpyted));
            Console.ReadLine();
        }
Ejemplo n.º 6
0
		static void Main()
		{
			//const string original = "Very secret and important information that can not fall into the wrong hands.";
			//string original = new String('0', 127);
			string original = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrs";
			original = GenerateRandomText();
			//string original = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890";
			//string original = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non dictum diam. Donec feugiat libero sed arcu interdum consectetur vitae amet.";
			//string original = @"?=??Y@쳘?{?? &긳 ? v ? ";

			var hybrid = new HybridEncryption();

			var rsaParams = new RSAWithRSAParameterKey();
			rsaParams.ImportKeys();

			var digitalSignature = new DigitalSignature();
			digitalSignature.AssignNewKey();

			Console.WriteLine("Hybrid Encryption with Integrity Check Demonstration in .NET");
			Console.WriteLine("------------------------------------------------------------");
			Console.WriteLine();

			try
			{
				var originalData = Encoding.UTF8.GetBytes(original);

				byte[] compressedBytes = Compress(originalData);
				byte[] decompressedBytes = Decompress(compressedBytes);

				var encryptedBlock = hybrid.EncryptData(
					originalData, rsaParams, digitalSignature);

				var decrpyted = hybrid.DecryptData(encryptedBlock, rsaParams, digitalSignature);

				//byte[] gzippedBytes = GetGZippedBytes(encryptedBlock.EncryptedData);
				//byte[] ungzippedBytes = GetUnGZippedBytes(gzippedBytes);
				byte[] gzippedBytes = Compress(encryptedBlock.EncryptedData);
				byte[] ungzippedBytes = Decompress(gzippedBytes);

				Console.WriteLine("Original Message = " + original);
				Console.WriteLine("Original Message Length: {0}", original.Length);
				Console.WriteLine("Compressed Original Message = " + Convert.ToBase64String(compressedBytes));
				Console.WriteLine("Compressed Original Message Length: {0}", compressedBytes.Length);
				Console.WriteLine("DeCompressed Original Message = " + Convert.ToBase64String(decompressedBytes));
				Console.WriteLine("DeCompressed Original Message Length: {0}", decompressedBytes.Length);
				Console.WriteLine("Encrypted Data: {0}", Convert.ToBase64String(encryptedBlock.EncryptedData));
				Console.WriteLine("Encrypted Data Size: {0}", encryptedBlock.EncryptedData.Length);
				Console.WriteLine("GZipped Encrypted Data: {0}", Convert.ToBase64String(gzippedBytes));
				Console.WriteLine("GZipped Encrypted Data Size: {0}", gzippedBytes.Length);
				Console.WriteLine("UnGZipped Encrypted Data: {0}", Convert.ToBase64String(ungzippedBytes));
				Console.WriteLine("UnGZipped Encrypted Data Size: {0}", ungzippedBytes.Length);
				Console.WriteLine();
				Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrpyted));
			}
			catch (CryptographicException ex)
			{
				Console.WriteLine("Error : " + ex.Message);
			}

			Console.ReadLine();
		}