Пример #1
0
        public string DecryptAndVerifySignature(Message message, string senderPublicKey)
        {
            // Decrypt keys using recipient's private key
            byte[] key = _rsa.Decrypt(message.Key, false);
            byte[] iv = _rsa.Decrypt(message.IV, false);

            // Get sender's public key
            var senderRsa = new RSACryptoServiceProvider();
            senderRsa.FromXmlString(senderPublicKey);

            // Decrypt the cipher text using symmetrical encryption
            var alg = new RijndaelManaged { Key = key, IV = iv };
            var cryptor = new SymCrypt(alg);
            byte[] clearTextBytes = cryptor.Decrypt(message.Data);

            // Verify digital signature using the sender's public key.
            bool verified = verifySignature(clearTextBytes, message.Signature, senderRsa);

            string clearText = (new UTF8Encoding()).GetString(clearTextBytes);

            return verified ? clearText : null;
        }
Пример #2
0
        public Message SignAndEncrypt(string message, string receiverPublicKey)
        {
            // convert message in bytes
            byte[] messageBytes = (new UTF8Encoding()).GetBytes(message);

            // Sign the clear text using the sender's private key
            byte[] signature = signMessage(messageBytes);
            var result = new Message { Signature = signature };

            // Encrypt the clear text using symmetrical encryption
            var cryptor = new SymCrypt(new RijndaelManaged());
            byte[] encryptedData = cryptor.Encrypt(messageBytes);
            result.Data = encryptedData;

            // Get recipient's public key
            var receiverRsa = new RSACryptoServiceProvider();
            receiverRsa.FromXmlString(receiverPublicKey);

            // Encrypt the encryption keys using the receiver's public key.
            result.Key = receiverRsa.Encrypt(cryptor.Key, false);
            result.IV = receiverRsa.Encrypt(cryptor.Iv, false);

            return result;
        }