Example #1
0
        private static void RunHybridEncryptionWithIntegrityCheck()
        {
            Console.WriteLine("Hybrid Encryption With Integrity Check started");
            Console.WriteLine();

            Console.WriteLine(String.Format("Message before encryption: {0}", _hybridMessage));

            // generate our private and public keys
            RsaWithRsaParameterKey rsaParams = new RsaWithRsaParameterKey();

            rsaParams.AssignNewKeys();

            try
            {
                HybridEncryption HE = new HybridEncryption();

                // encrypt the data
                EncryptedPacket encryptedBlock = HE.EncryptDataWithIntegrity(Encoding.UTF8.GetBytes(_hybridMessage), rsaParams);
                Console.WriteLine(String.Format("Message after encryption: {0}", Encoding.UTF8.GetString(encryptedBlock.EncryptedData)));

                // decrypt the data
                // we can put a break point here, alter the encrypted data of the packet before we pass it into the DecryptDataWithIntegrity() method, which will
                // then do the compare of the HMAC hashes, fail and get caught in this try/catch
                byte[] decryptedData = HE.DecryptDataWithIntegrity(encryptedBlock, rsaParams);
                Console.WriteLine(String.Format("Message after decryption: {0}", Encoding.UTF8.GetString(decryptedData)));
            }
            catch (CryptographicException CE)
            {
                Console.WriteLine("Hybrid Encryption With Integrity failed, Error: " + CE.Message);
            }


            Console.WriteLine();
            Console.WriteLine("Hybrid Encryption With Integrity Check ended");
        }
Example #2
0
        private static void RunHybridEncryption()
        {
            Console.WriteLine("Hybrid Encryption started");
            Console.WriteLine();

            Console.WriteLine(String.Format("Message before encryption: {0}", _hybridMessage));

            // generate our private and public keys
            RsaWithRsaParameterKey rsaParams = new RsaWithRsaParameterKey();

            rsaParams.AssignNewKeys();


            HybridEncryption HE = new HybridEncryption();

            // encrypt the data
            EncryptedPacket encryptedBlock = HE.EncryptData(Encoding.UTF8.GetBytes(_hybridMessage), rsaParams);

            Console.WriteLine(String.Format("Message after encryption: {0}", Encoding.UTF8.GetString(encryptedBlock.EncryptedData)));


            byte[] decryptedData = HE.DecryptData(encryptedBlock, rsaParams);
            Console.WriteLine(String.Format("Message after decryption: {0}", Encoding.UTF8.GetString(decryptedData)));

            Console.WriteLine();
            Console.WriteLine("Hybrid Encryption ended");
        }
Example #3
0
        private static void RunHybridEncryptionWithDigitalSignature()
        {
            Console.WriteLine("Hybrid Encryption With Digital Signature started");
            Console.WriteLine();

            Console.WriteLine(String.Format("Message before encryption: {0}", _hybridWithSignatureMessage));

            HybridEncryption HP = new HybridEncryption();

            RsaWithRsaParameterKey rsaParams = new RsaWithRsaParameterKey();

            rsaParams.AssignNewKeys();

            DigitalSignatures DS = new DigitalSignatures();

            DS.AssignNewKey();

            try
            {
                EncryptedPacket encryptedBlock = HP.EncryptDataWithSignature(Encoding.UTF8.GetBytes(_hybridWithSignatureMessage), rsaParams, DS);
                Console.WriteLine(String.Format("Message after encryption: {0}", Encoding.UTF8.GetString(encryptedBlock.EncryptedData)));

                byte[] decryptedData = HP.DecryptDataWithSignature(encryptedBlock, rsaParams, DS);
                Console.WriteLine(String.Format("Message after decryption: {0}", Encoding.UTF8.GetString(decryptedData)));
            }
            catch (CryptographicException CE)
            {
                Console.WriteLine(String.Format("Hybrid Encryption With Digital Signature failed, Error: {0}", CE.Message));
            }

            Console.WriteLine();
            Console.WriteLine("Hybrid Encryption With Digital Signature ended");
        }