Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <summary>
        /// Decrypts the enveloped encrypted message by selecting the correct certificate from My store
        /// </summary>
        /// <param name="msgToEncrypt"></param>
        /// <returns></returns>
        public string Decrypt(string msgToDecrypt)
        {
            if (string.IsNullOrEmpty(msgToDecrypt))
            {
                DynamoDBTracer.Tracer.Write("Message to decrypt is null or empty");
                throw new ArgumentException(string.Format(
                                                "Message to decrypt={0} is null or empty", msgToDecrypt));
            }

            // Convert message to an array of bytes.
            byte[] encodedEnvelopedCms = EncryptionUtilities.ConvertHexStringToByteArray(msgToDecrypt);

            // Decrypt by reading certificate from My certificate store
            byte[] decryptedMsgBytes = DecryptMsg(encodedEnvelopedCms, false);

            // Convert the byte array to string
            UnicodeEncoding unicode      = new UnicodeEncoding();
            string          decryptedMsg = unicode.GetString(decryptedMsgBytes);

            return(decryptedMsg);
        }