Beispiel #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");
        }
Beispiel #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");
        }