Example #1
0
        public byte[] DecryptData(EncryptedPacket encryptedPacket, RSAWithRSAParameterKey rsaParams)
        {
            // Decrypt AES Key with RSA.
            var decryptedSessionKey = rsaParams.DecryptData(encryptedPacket.EncryptedSessionKey);

            // Decrypt our data with  AES using the decrypted session key.
            var decryptedData = _aes.Decrypt(encryptedPacket.EncryptedData,
                                             decryptedSessionKey, encryptedPacket.Iv);

            return(decryptedData);
        }
Example #2
0
        public EncryptedPacket EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams)
        {
            // Generate our session key.
            var sessionKey = _aes.GenerateRandomNumber(32);

            // Create the encrypted packet and generate the IV.
            var encryptedPacket = new EncryptedPacket {
                Iv = _aes.GenerateRandomNumber(16)
            };

            // Encrypt our data with AES.
            encryptedPacket.EncryptedData = _aes.Encrypt(original, sessionKey, encryptedPacket.Iv);

            // Encrypt the session key with RSA
            encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey);

            return(encryptedPacket);
        }
        static void Main()
        {
            const string original = "Very secret and important information that can not fall into the wrong hands.";

            var rsaParams = new RSAWithRSAParameterKey();

            rsaParams.AssignNewKey();

            var hybrid = new HybridEncryption();

            var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), rsaParams);
            var decrpyted      = hybrid.DecryptData(encryptedBlock, rsaParams);

            Console.WriteLine("Hybrid Encryption Demonstration in .NET");
            Console.WriteLine("---------------------------------------");
            Console.WriteLine();
            Console.WriteLine("Original Message = " + original);
            Console.WriteLine();
            Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrpyted));
            Console.ReadLine();
        }
Example #4
0
        static void Main()
        {
            //The example provided is not good since we only have one public/private key pair
            var data = "Hello, World!";

            var rsaKey = new RSAWithRSAParameterKey();

            rsaKey.AssignNewKey();

            var aliceHybridEncryption = new HybridEncryption();
            var encryptedData         = aliceHybridEncryption.EncryptData(Encoding.UTF8.GetBytes(data), rsaKey);

            //Data is transmitted
            Console.WriteLine("Encrypted Data: " + Convert.ToBase64String(encryptedData.EncryptedData));
            Console.WriteLine("Encrypted Session Key: " + Convert.ToBase64String(encryptedData.EncryptedSessionKey));
            Console.WriteLine("Initialization Vector: " + Convert.ToBase64String(encryptedData.Iv));

            var bobHybridEncryption = new HybridEncryption();
            var decryptedData       = bobHybridEncryption.DecryptData(encryptedData, rsaKey);

            Console.WriteLine(Encoding.UTF8.GetString(decryptedData));
        }