I have an online eCommerce websites that uses a third party payment portal. The payment portal was working fine until the third party payment portal have asked everyone to start using a hash key with other payment parameters.
Now the problem is that the third party payment portal have only provided one page documentation for implementing the hash key.
This is the documentation provided:-
Encryption Algorithm
In order to mitigate parameter tempering/modification while transfer and posting of data, merchant can encrypt the request using the hash key provided by Telenor POC. This encrypted request is sent along with the main request, which then reconciled at OPS end to detect if parameter is changed or not. The encryption can be done using following algorithm:
Create map of all the fields that are part of the request Map fields = new HashMap();
fields.put("amount", "10");
fields.put("storeId", "28");
fields.put("orderRefNum", "11001");
fields.put("expiryDate", "20150101 151515");
fields.put("postBackURL", "http://localhost:9081/local/status.php");
Get the list of field name from the map created in the first step
List fieldNames = new ArrayList(fields.keySet());
Sort the map fields based on map key in alphabetical order
Collections.sort(fieldNames);
Create a string in following format: amount=10&expiryDate=20150101151515&orderRefNum=11001&postBackURL=http://localhost:9081/local/status.php&storeId=28
Use AES/ECB/PKCS5Padding algorithm to encrypt with the key and string produced in the previous step
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
encryptedValue = new String(Base64.encodeBase64(cipher.doFinal(value.getBytes())));
Now another problem is that I do not have any experience in Java.
I called the third party payment portal helpline and they were only helpful enough to tell me the key.
If anyone can be helpful enough to tell me what would be the Ruby equivalent of step 5 I will be grateful. Thanks
Just tried the provided code on online java compiler:-
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class encryptData {
public static void main(String[] args) {
String data="amount=10&expiryDate=20150101 151515&orderRefNum=11001&postBackURL=http://localhost:9081/local/status.php&storeId=28";
String key="89OUITUPRL3I8H3G";
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
encryptedValue = new String(Base64.encodeBase64(cipher.doFinal(data.getBytes())));
}
}
This is the error:-
/tmp/java_Ramvov/encryptData.java:16: error: cannot find symbol
encryptedValue = new String(Base64.encodeBase64(cipher.doFinal(data.getBytes())));
^
symbol: variable encryptedValue
location: class encryptData
/tmp/java_Ramvov/encryptData.java:16: error: cannot find symbol
encryptedValue = new String(Base64.encodeBase64(cipher.doFinal(data.getBytes())));
^
symbol: method encodeBase64(byte[])
location: class Base64
2 errors
Any help will be appreciated
I have also tried to reproduce this java code in ruby:-
data = "amount=10&expiryDate=20150101151515&orderRefNum=11001&postBackURL=http://localhost:9081/local/status.php&storeId=28"
cipher = OpenSSL::Cipher.new("AES-128-ECB")
cipher.encrypt()
cipher.key = "89OUITUPRL4I9H3G"
crypt = cipher.update(data) + cipher.final()
crypt_string = (Base64.encode64(crypt))
But the generated encryption is rejected by the payment portal