You need to sign in to do that
Don't have an account?
shrey.tyagi88@tcs.com
Apex code for Encrytion / Decrytion of a field.---Plz Help!!!!
Hi All,
I am making a POC to get the feel of encrytion/decrytion of fields in salesforce using apex. Her are my requirements.
I have a custom object say XYZ__c with field say ABC__c. I want to enter this field in a visual force page , hit encrypt button(command button) that runs the apex code and display encryted text in the output field of the page. Can anyone please help me with the code.
Thanks
Shrey Tyagi
This page demonstrates the basic power of encryption/decryption in salesforce. Obviously, the key generated is lost when the page is reloaded, so normally the key would be stored in a custom setting or database object. The "withmanagediv" is simply used so that we don't need an initialization vector, but you could use the normal encrypt/decrypt methods as well with an IV (this will reduce the output by a decent margin). Finally, do not store the key or IV in the view state at all (this is only a demo page).
See: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_crypto.htm
All Answers
This page demonstrates the basic power of encryption/decryption in salesforce. Obviously, the key generated is lost when the page is reloaded, so normally the key would be stored in a custom setting or database object. The "withmanagediv" is simply used so that we don't need an initialization vector, but you could use the normal encrypt/decrypt methods as well with an IV (this will reduce the output by a decent margin). Finally, do not store the key or IV in the view state at all (this is only a demo page).
See: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_crypto.htm
Here's a simple encrypt / decrypt class I wrote that would help you out. Pretty much once you have this class decryption and encryption should be cake to eventually display on your vf page.
Thanks a lot for your quick reply alex, It helped a lot!!!
Hi sfdcfox,
Your code works perfectly fine. Just wanna know few things here. You are using encryptwithmanagediv , where iv stands for initialization vector. Could you please tell me how and where have you defined it ? I mean , how do we encode/decode using initialization vector, and where do we define /mention this vector?
Thanks
key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
iv() As Byte = {65, 110, 68, 26, 69, 178, 200, 219}
How can I encrypt using these 2 parametres? Thanks a lot for your help.
The IV is "managed" in my example (it is prepended to the actual data so that it can be pulled back out later).
Here's a code sample using the bytes you provided:
Other than that, the code is basically the same as I provided previously.