Ejemplo n.º 1
0
        public EncryptedPacket EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams)
        {
            var sessionKey      = _aes.GenerateRandomNumber(32);
            var encryptedPacket = new EncryptedPacket {
                Iv = _aes.GenerateRandomNumber(16)
            };

            // Encrypt data with AES and AES Key with RSA
            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);
            }

            return(encryptedPacket);
        }
Ejemplo n.º 2
0
        static void Main()
        {
            var          aes      = new AesEncryption();
            var          key      = aes.GenerateRandomNumber(32);
            var          iv       = aes.GenerateRandomNumber(16);
            const string original = "Text to encrypt";

            var encrypted = aes.Encrypt(Encoding.UTF8.GetBytes(original), key, iv);
            var decrypted = aes.Decrypt(encrypted, key, iv);

            var decryptedMessage = Encoding.UTF8.GetString(decrypted);

            Console.WriteLine();
            Console.WriteLine("Original Text = " + original);
            Console.WriteLine("Encrypted Text = " + Convert.ToBase64String(encrypted));
            Console.WriteLine("Decrypted Text = " + decryptedMessage);

            Console.ReadLine();
        }
Ejemplo n.º 3
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;
        }