Exemple #1
0
        private static void RunDigitalSignature()
        {
            Console.WriteLine("Digital Signatures started");
            Console.WriteLine();

            Console.WriteLine(String.Format("Message before encryption: {0}", _signatureMessage));
            byte[] messageToSign = Encoding.UTF8.GetBytes(_signatureMessage);
            byte[] hashedDocument;

            using (SHA256 sha256 = SHA256.Create())
            {
                hashedDocument = sha256.ComputeHash(messageToSign);
            }

            DigitalSignatures DS = new DigitalSignatures();

            DS.AssignNewKey();

            byte[] signature = DS.SignData(hashedDocument);

            Console.WriteLine(String.Format("Digital Signature: {0}", Encoding.UTF8.GetString(signature)));

            bool isSignatureVerified = DS.VerifySignature(hashedDocument, signature);

            Console.WriteLine(String.Format("Verify Signature Results: {0}", isSignatureVerified.ToString()));

            Console.WriteLine();
            Console.WriteLine("Digital Signatures ended");
        }
Exemple #2
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");
        }
Exemple #3
0
        public byte[] DecryptDataWithSignature(EncryptedPacket EP, RsaWithRsaParameterKey rsaParams, DigitalSignatures DS)
        {
            // Receiver decrypts AES session key with RSA
            byte[] decryptedSessionKey = rsaParams.DecryptData(EP.EncryptedSessionKey);


            // Receiver compares
            using (HMACSHA256 hmac = new HMACSHA256(decryptedSessionKey))
            {
                byte[] hmacToCheck = hmac.ComputeHash(EP.EncryptedData);

                if (!CompareHashes(EP.Hmac, hmacToCheck))
                {
                    throw new CryptographicException("HMAC for decryption does not match encrypted packet HMAC");
                }

                if (!DS.VerifySignature(EP.Hmac, EP.Signature))
                {
                    throw new CryptographicException("Digital Signature cannot be verified");
                }
            }

            // Receiver decrypts the data wuth AES using the decrypted session key
            byte[] decryptedData = _cryptographyExample.DecryptUsingAES(EP.EncryptedData, decryptedSessionKey, EP.IV);

            return(decryptedData);
        }
Exemple #4
0
        public EncryptedPacket EncryptDataWithSignature(byte[] originalMessage, RsaWithRsaParameterKey rsaParams, DigitalSignatures DS)
        {
            // Sender generates AES session key
            byte[] sessionKey = _cryptographyExample.GenerateRandomNumber(32);

            // Sender generates Initialization Vector
            byte[] initializationVector = _cryptographyExample.GenerateRandomNumber(16);

            // Sender stores that IV in the packet object
            EncryptedPacket EP = new EncryptedPacket
            {
                IV = initializationVector
            };

            // Sender encrypts data using AES
            EP.EncryptedData = _cryptographyExample.EncryptUsingAES(originalMessage, sessionKey, EP.IV);

            //Sender encrypts the session key with RSA
            EP.EncryptedSessionKey = rsaParams.EncryptData(sessionKey);

            // Sender generates hash mac using our session key
            using (HMACSHA256 hmac = new HMACSHA256(sessionKey))
            {
                EP.Hmac = hmac.ComputeHash(EP.EncryptedData);
            }

            //Sender signs the message with a digital signature
            EP.Signature = DS.SignData(EP.Hmac);

            return(EP);
        }