Salesforce URL加密encrypt与解密decrypt处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/itsme_web/article/details/81017246

情景引入】:我们使用自定义页面时,通常需要使用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 ClassEncoding 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中参数通常使用对称性加密算法,实例如下:

String domain = 'https://test.cs6.my.salesforce.com/apex/ENEWS2018JUL_VOTECampaignPage';
// 加密过程
Blob exampleIv = Blob.valueOf('Example of IV123');
Blob key = Crypto.generateAesKey(128);
Blob data = Blob.valueOf('ContactId=003N000001Byyph&Retirement_Planning=A&campcode=ENEWS2018JUL_VOTE');
Blob encrypted = Crypto.encrypt('AES128', key, exampleIv, data);
// 获取url
String url = domain + '?' + encrypted;
System.debug('encrypt url: ' + url);
// 解密过程
Blob decrypted = Crypto.decrypt('AES128', key, exampleIv, encrypted);
String decryptedString = decrypted.toString();
url = domain + '?' + decryptedString;
System.debug('decrypt url: ' + url);

猜你喜欢

转载自blog.csdn.net/itsme_web/article/details/81017246
今日推荐