예제 #1
0
        public EncryptedPacket EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams, DigitalSignatureFuncs digitalSignature)
        {
            // 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);

            // Calculate a HMAC
            encryptedPacket.HMAC = HMac.ComputeHMACSha256(encryptedPacket.EncryptedData, sessionKey);

            // Generate digital signature of packet to send
            encryptedPacket.Signature = digitalSignature.SignData(encryptedPacket.HMAC);

            return(encryptedPacket);
        }
        public static EncryptedPacket EncryptData(string original, RSAWithRSAParameterKey rsaParams)
        {
            var aes             = new AesEncryption();
            var sessionKey      = RandomCryptography.Random.GenerateRandomNumber(32);
            var encryptedPacket = new EncryptedPacket
            {
                Iv = RandomCryptography.Random.GenerateRandomNumber(16)
            };

            // Encrypt data with AES and AES key with RSA.
            encryptedPacket.EncryptedData       = aes.Encrypt(Encoding.UTF8.GetBytes(original), sessionKey, encryptedPacket.Iv);
            encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey);
            return(encryptedPacket);
        }
        private static void TestRSAWithRSAParameterKey()
        {
            var rsaParams = new RSAWithRSAParameterKey();

            const string original = "Text to encrypt";

            rsaParams.AssignNewKey();

            var encryptedRSAParams = rsaParams.EncryptData(Encoding.UTF8.GetBytes(original));
            var decryptedRSAParams = rsaParams.DecryptData(encryptedRSAParams);

            Console.WriteLine($"Original Text: {original}");
            Console.WriteLine($"Encrypted RSA Params: {Convert.ToBase64String(encryptedRSAParams)}");
            Console.WriteLine($"Decrypted RSA Params: {Convert.ToBase64String(decryptedRSAParams)}");
            Console.WriteLine($"Decrypted Text: {Encoding.Default.GetString(decryptedRSAParams)}");
        }
예제 #4
0
        public EncryptedPacket EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams)
        {
            var sessionKey = _aes.GenerateRandomNumber(32);

            var encryptedPacket = new EncryptedPacket {
                Iv = _aes.GenerateRandomNumber(12)
            };

            (byte[] ciphereText, byte[] tag)encrypted = _aes.Encrypt(original, sessionKey, encryptedPacket.Iv, null);

            encryptedPacket.EncryptedData       = encrypted.ciphereText;
            encryptedPacket.Tag                 = encrypted.tag;
            encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey);

            return(encryptedPacket);
        }
예제 #5
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);
        }