Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 55831

Create a plugin to decrypt custom field of Case Entity while Retrieve in MS Dynamics CRM Online

$
0
0

I have a requirement that to Encrypt the custom Field and Decrypt Automatically while viewing the case in MS Dynamics CRM online Portal. I created two Plugins one is for Encrypt at PreCaseCreate and the other is to Decrypt at PostCaseRetrieve.The Plugin for Encryption is Working fine,but plugin for decrypt is not working(which means the encrypted content is not decrypting while viewing in online portal). Below is the code for decryption

protected void ExecutePostCaseRetrieve(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            // TODO: Implement your custom Plug-in business logic.
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            if (context.Depth == 1)
            {
                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
                {
                    // Obtain the target entity from the input parmameters.
                    EntityReference entityreference = (EntityReference)context.InputParameters["Target"];

                    ColumnSet cols = new ColumnSet(new String[] { "title", "description", "new_phicontent" });
                    Entity incident = service.Retrieve(entityreference.LogicalName, entityreference.Id, cols);
                    if (incident.LogicalName.ToLower().Equals("incident"))
                    {
                        try
                        {
                            if (incident.Attributes.Contains("new_phicontent"))
                            {
                                string PHIContent = incident.Attributes["new_phicontent"].ToString();

                                byte[] bInput = Convert.FromBase64String(PHIContent);
                                UTF8Encoding UTF8 = new UTF8Encoding();
                                //Encrypt/Decrypt strings which in turn uses 3DES (Triple Data Encryption standard) algorithm
                                TripleDESCryptoServiceProvider tripledescryptoserviceprovider = new TripleDESCryptoServiceProvider();

                                //Alow to compute a hash value for Encryption/Decryption
                                MD5CryptoServiceProvider md5cryptoserviceprovider = new MD5CryptoServiceProvider();

                                tripledescryptoserviceprovider.Key = md5cryptoserviceprovider.ComputeHash(ASCIIEncoding.ASCII.GetBytes("secretkey"));
                                tripledescryptoserviceprovider.Mode = CipherMode.ECB;
                                ICryptoTransform icryptotransform = tripledescryptoserviceprovider.CreateDecryptor();

                                string DecryptedText = UTF8.GetString(icryptotransform.TransformFinalBlock(bInput, 0, bInput.Length));
                                incident.Attributes["new_phicontent"] = DecryptedText;
                                //service.Update(incident);
                            }

                        }
                        catch (FaultException ex)
                        {
                            throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                        }
                    }
                }
            }
        }


I tried with PreCaseRetrieve event also,but i didn't got result

If i uncomment " service.Update(incident); " then decryption is happen.But the Update method permanently decrypting  the text, which means the update is happen in Database level.

My Actual Requirement is to decrypt the content to show user only(if not mess of characters will create confuse to the user).

Kindly help me to resolve this.

  

I have a requirement that to Encrypt the custom Field and Decrypt Automatically while viewing the case in MS Dynamics CRM online Portal. I created two Plugins one is for Encrypt at PreCaseCreate and the other is to Decrypt at PostCaseRetrieve.The Plugin for Encryption is Working fine,but plugin for decrypt is not working(which means the encrypted content is not decrypting while viewing in online portal). Below is the code for decryption


Viewing all articles
Browse latest Browse all 55831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>