Salesforce URL加密encrypt与解密decrypt处理
【情景引入】:我們使用自定義頁面時,通常需要使用url傳參來賦初始值或作為查詢依據,url如果為明文數據很容易被篡改,從而導致記錄與預期業務不符。因此,我們通常會考慮對url進行加密處理,在url接收方通過解密來獲取數據。
【Example】:我們來看如下url:
https://test.cs6.my.salesforce.com/apex/ENEWS2018JUL_VOTECampaignPage?ContactId=003N000001Byyph
&Retirement_Planning=A&campcode=ENEWS2018JUL_VOTE
這里我們使用ENEWS2018JUL_VOTECampaignPage頁面,需要傳遞三個參數:ContactId/Retirement_Planning/campcode,如果我們不對其加密,客戶很容易篡改數據。
【知識引入】:幾種常見加密算法解析及使用?|?Crypto Class
1、加密算法分類:
a、單向加密:不可逆,加密后無法解密。如將用戶名密碼存在后臺;
b、雙向加密:分對稱性加密算法和非對稱性加密算法。它們可逆,對稱加密只要接受方知道加密算法名algorithmName, 私鑰privateKey和初始向量initializationVector就可以破譯密文cipherText了。實例如下:
decrypt(algorithmName, privateKey, initializationVector, cipherText)
Blob exampleIv = Blob.valueOf('Example of IV123'); Blob key = Crypto.generateAesKey(128); Blob data = Blob.valueOf('Data to be encrypted'); Blob encrypted = Crypto.encrypt('AES128', key, exampleIv, data);Blob decrypted = Crypto.decrypt('AES128', key, exampleIv, encrypted); String decryptedString = decrypted.toString(); System.assertEquals('Data to be encrypted', decryptedString);decryptWithManagedIV(algorithmName, privateKey, IVAndCipherText)
Blob key = Crypto.generateAesKey(128); Blob data = Blob.valueOf('Data to be encrypted'); Blob encrypted = Crypto.encryptWithManagedIV('AES128', key, data);Blob decrypted = Crypto.decryptWithManagedIV('AES128', key, encrypted); String decryptedString = decrypted.toString(); System.assertEquals('Data to be encrypted', decryptedString);上述2種方法唯一區別是是否需要initializationVector,第二種方式是允許salesforce來產生初始向量的。
非對稱算法與之不同,發送雙方A,B事先均生成一堆密匙,然后A將自己的公有密匙發送給B,B將自己的公有密匙發送給A,如果A要給B發送消 息,則先需要用B的公有密匙進行消息加密,然后發送給B端,此時B端再用自己的私有密匙進行消息解密,B向A發送消息時為同樣的道理。
2. 常用算法舉例:
a、幾種對稱性加密算法:AES128, AES192, AES256;AES(Advanced Encryption Standard),使用時私鑰privateKey的長度依次為:128bit, 192bit和256bit,另外初始向量initializationVector的字符長度對應為:16位,24位,36位字符。
length relationship:?
--------------------------------------------------------
AES:? ? ? ? ? ? ? ? ? ? ? ? ?128? ? ????192????? ? 256
privateKey:? ? ? ? ? ? ? ? 128? ??????192????? ? 256
initializationVector:? ? 16????????? 24? ??? ? ? 32
b、幾種非對稱性加密算法:RSA;
c、幾種線性散列算法(簽名算法,Java稱為加鹽):MD5, SHA1, SHA-256, SHA-512;這幾種算法只生成一串不可逆的密文,經常用其效驗數據傳輸過程中是否經過修改,因為相同的生成算法對于同一明文只會生成唯一的密文,若相同算法生成的密文不同,則證明傳輸數據進行過了修改。通常在數據傳說過程前,使用MD5和SHA1算法均需要發送和接收數據雙方在數據傳送之前就知道密匙生成算法。
【解決方案】:
1. 如果參數包含時間信息,通常對時間戳進行加密:EncodingUtil Class |?Encoding Your Data
加密后:EncodingUtil.urlEncode(timestamp, 'UTF-8'); //2018-7-12%27T%2715%3A47%3A43.580%27Z%27
解密還原:EncodingUtil.urlDecode(urlEncodedTimestamp, 'UTF-8'));?//2018-7-12'T'15:47:43.580'Z'
Note:使用時間戳可能產生重復的時間戳,而隨機數算法不會重復。
2. 如果需要解析url中參數通常使用對稱性加密算法,實例如下:
【擴展延伸】:使用Base64輔助加解密 - 當加密數據時,就能打印Blob對應的base64文本值了
public class ApexEncryptionDemo {public static String cryptHelper(Boolean encrypt, String text) {// Use cryptokey as the encryption key. cryptokey is stored in custom metadata settings.Blob cryptoKey = Blob.valueOf([SELECT keys__c FROM secretkey__mdt].keys__c);if(encrypt) {Blob encrypted = Crypto.encryptWithManagedIV('AES128', cryptoKey, Blob.valueOf(text));return EncodingUtil.base64Encode(encrypted);}else {Blob decrypted = Crypto.decryptWithManagedIV('AES128', cryptoKey, EncodingUtil.base64Decode(text));return decrypted.toString();}} }詳見Kingdom Management - Apex Encryption Demo
【工具類】:
public class CryptoUtils {public static String SHA1Hash(String value) {return hash('SHA1', value);}public static String MD5Hash(String value) {return hash('MD5', value);}public static String SHA256Hash(String value) {return hash('SHA-256', value);}public static String SHA512Hash(String value) {return hash('SHA-512', value);}private static String hash(String algorithmName, String input) {return EncodingUtil.convertToHex(Crypto.generateDigest(algorithmName, Blob.valueOf(input)));} }總結
以上是生活随笔為你收集整理的Salesforce URL加密encrypt与解密decrypt处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 王垠:如何掌握所有的程序语言
- 下一篇: 【LaTex】利用ins文件和dtx文件