/// <summary> /// Encrypt to enveloped cms encryptes txt using x509 certificate /// </summary> /// <param name="msgToEncrypt"></param> /// <returns></returns> public string Encrypt(string msgToEncrypt) { // Validate the msg to be encrypted if (string.IsNullOrEmpty(msgToEncrypt)) { DynamoDBTracer.Tracer.Write("Message to encrypt is null or empty"); throw new ArgumentException(string.Format("Message to encrypt is null or empty")); } // Validate the certificate if (this.RecipientCert == null) { DynamoDBTracer.Tracer.Write("Recipient certificate for encrytion is null"); throw new ArgumentException(string.Format( "Recipient certificate for encrytion")); } // Convert message to an array of Unicode bytes for signing. UnicodeEncoding unicode = new UnicodeEncoding(); byte[] msgBytes = unicode.GetBytes(msgToEncrypt); // Encrypt using the certificate byte[] encodedEnvelopedCms = EncryptMsg(msgBytes, this.RecipientCert); string encodedMessage = EncryptionUtilities.ConvertToHexString(encodedEnvelopedCms); return(encodedMessage); }