Tuesday, June 27, 2017

How to encrypt string to Base64 in salesforce

Base64 is often used when you need to encode binary data into characters. Base64 is a good way of taking binary data and turning it into text so that it can easily be transmitted in things like HTML form data and email. Salesforce.com makes it pretty easy to perform Base-64 encoding in Apex via their EncodingUtil class. Below is an Apex code snippet with a very simple example of the base-64 encode/decode.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string before = 'Testing base 64 encode';
 
// create a blob from our parameter value before we send it as part of the url
Blob beforeblob = Blob.valueOf(before);
 
// base64 encode the blob that contains our url param value
string paramvalue = EncodingUtil.base64Encode(beforeblob);
 
// print out the encoded value to the debug log so we can see it before/after base64 encode
System.debug(before + ' is now encoded as: ' + paramvalue);
 
// take the base64 encoded parameter and create base64 decoded Blob from it
Blob afterblob = EncodingUtil.base64Decode(paramvalue);
 
// Convert the blob back to a string and print it in the debug log
System.debug(paramvalue + 'is now decoded as: ' + afterblob.toString());

No comments:

Post a Comment