static void Main(string[] args) { PublicKeyResponse publicKeyResponse = GetPublicKeyMessageId(); if (publicKeyResponse.MessageId != null && publicKeyResponse.RsaPublicKey != null) { Console.Write("Public key request successful. Enter your message: "); string message = Console.ReadLine(); SymmetricAlgorithmService symmetricService = new SymmetricAlgorithmService(); SymmetricEncryptionResult symmetricEncryptionResult = symmetricService.Encrypt(message); AsymmetricEncryptionService asymmetricService = new AsymmetricEncryptionService(publicKeyResponse.RsaPublicKey); string encryptedAesPublicKey = asymmetricService.GetCipherText(ConfigurationManager.AppSettings["RijndaelManagedKey"]); SendMessageArguments sendMessageArgs = new SendMessageArguments() { EncryptedPublicKey = encryptedAesPublicKey, SymmetricEncryptionArgs = symmetricEncryptionResult, MessageId = publicKeyResponse.MessageId }; string jsonifiedArgs = JsonConvert.SerializeObject(sendMessageArgs); SendSecretMessageToReceiver(jsonifiedArgs); } else { Console.WriteLine("Public key request failed."); } Console.ReadKey(); }
public SymmetricEncryptionResult Encrypt(string plainText) { SymmetricEncryptionResult res = new SymmetricEncryptionResult(); RijndaelManaged rijndael = CreateCipher(); res.InitialisationVector = Convert.ToBase64String(rijndael.IV); ICryptoTransform cryptoTransform = rijndael.CreateEncryptor(); byte[] plain = Encoding.UTF8.GetBytes(plainText); byte[] cipherText = cryptoTransform.TransformFinalBlock(plain, 0, plain.Length); res.CipherText = Convert.ToBase64String(cipherText); return(res); }