Пример #1
0
        static void Main()
        {
            const string original = "Very secret and important information that can not fall into the wrong hands.";

            var hybrid = new HybridEncryption();

            var rsaParams = new RSAWithRSAParameterKey();

            rsaParams.AssignNewKey();

            var digitalSignature = new DigitalSignature();

            digitalSignature.AssignNewKey();

            Console.WriteLine();

            try
            {
                var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), rsaParams,
                                                        digitalSignature);

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

                Console.WriteLine("Original Message = " + original);
                Console.WriteLine();
                Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrpyted));
            }
            catch (CryptographicException ex)
            {
                Console.WriteLine("Error : " + ex.Message);
            }

            Console.ReadLine();
        }
Пример #2
0
        static void Main()
        {
            var document = Encoding.UTF8.GetBytes("Document to Sign");

            byte[] hashedDocument;

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

            var digitalSignature = new DigitalSignature();

            digitalSignature.AssignNewKey();

            var signature = digitalSignature.SignData(hashedDocument);
            var verified  = digitalSignature.VerifySignature(hashedDocument, signature);

            Console.WriteLine();
            Console.WriteLine("   Original Text = " +
                              Encoding.Default.GetString(document));

            Console.WriteLine();
            Console.WriteLine("   Digital Signature = " +
                              Convert.ToBase64String(signature));

            Console.WriteLine();

            Console.WriteLine(verified
                ? "The digital signature has been correctly verified."
                : "The digital signature has NOT been correctly verified.");

            Console.ReadLine();
        }
Пример #3
0
        public byte[] DecryptData(EncryptedPacket encryptedPacket, RSAWithRSAParameterKey rsaParams, 
                                  DigitalSignature digitalSignature)
        {            
            var decryptedSessionKey = rsaParams.DecryptData(encryptedPacket.EncryptedSessionKey);
            
            using (var hmac = new HMACSHA256(decryptedSessionKey))
            {                
                var hmacToCheck = hmac.ComputeHash(encryptedPacket.EncryptedData);
                
                if (!Compare(encryptedPacket.Hmac, hmacToCheck)){
                    throw new CryptographicException(
                        "HMAC for decryption does not match encrypted packet.");
                }
                
                if (!digitalSignature.VerifySignature(encryptedPacket.Hmac, 
                                                      encryptedPacket.Signature)){
                    throw new CryptographicException(
                        "Digital Signature can not be verified.");
                }
            }

            var decryptedData = _aes.Decrypt(encryptedPacket.EncryptedData, decryptedSessionKey, 
                                             encryptedPacket.Iv);

            return decryptedData;
        }
Пример #4
0
        public EncryptedPacket EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams, 
                                           DigitalSignature digitalSignature)
        {            
            var sessionKey = _aes.GenerateRandomNumber(32);
            
            var encryptedPacket = new EncryptedPacket { Iv = _aes.GenerateRandomNumber(16) };
                        
            encryptedPacket.EncryptedData = _aes.Encrypt(original, sessionKey, encryptedPacket.Iv);
            
            encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey);          
            
            using (var hmac = new HMACSHA256(sessionKey))
            {
                encryptedPacket.Hmac = hmac.ComputeHash(encryptedPacket.EncryptedData);
            }
            
            encryptedPacket.Signature = digitalSignature.SignData(encryptedPacket.Hmac);

            return encryptedPacket;
        }