문제

I need to convert this java code in force.com apex. i tried to use Crypto class to get same encryption but not getting how can i get same value for the variable "fingerprintHash" in the last in APEX . Can Anyone help me in this technical issue?

Random generator = new Random();
sequence =Long.parseLong(sequence+""+generator.nextInt(1000));

timeStamp = System.currentTimeMillis() / 1000;

try {
    SecretKey key = new SecretKeySpec(transactionKey.getBytes(), "HmacMD5"); 
    Mac mac = Mac.getInstance("HmacMD5");
    mac.init(key);

    String inputstring = loginID + "^" + sequence + "^" + timeStamp + "^" + amount + "^";
    byte[] result = mac.doFinal(inputstring.getBytes());

    StringBuffer strbuf = new StringBuffer(result.length * 2);

    for (int i = 0; i < result.length; i++) {
        if (((int) result[i] & 0xff) < 0x10) {
            strbuf.append("0");
        }

        strbuf.append(Long.toString((int) result[i] & 0xff, 16));
    }

    fingerprintHash = strbuf.toString(); //need this result for variable x_fp_hash 

The apex code I was trying is :-

String API_Login_Id='6########';
String TXn_Key='6###############';
String amount='55';
sequence = '300';

long timeStamp = System.currentTimeMillis()/1000;

String inputStr = API_Login_Id + '^' + sequence + '^' + timeStamp + '^' + amount + '^';
String algorithmName = 'hmacMD5';

Blob mac = Crypto.generateMac(algorithmName,Blob.valueOf(inputStr),Blob.valueOf( TXn_Key));
String macUrl =EncodingUtil.urlEncode(EncodingUtil.base64Encode(mac), 'UTF-8');
도움이 되었습니까?

해결책

The problem would seem to be that you are hex encoding the output on the javaside, but base64 encoding the output on the apex side, try using EncodingUtils.convertToHex instead of EncodingUtils.base64Encode

다른 팁

You look like you're heading along the right lines with regards to the encryption, however you're using a time stamp as part of your input string, and so unless you're astronomically lucky you're always encoding different strings. While you're working on porting the code, remove the timestamp so that you can be sure your input strings are the same - if they're not the same then you'll never get the same result.

Once you've established that your encryption is working as desired, then you can put the timestamp back into the code safe in the knowledge that it'll be functioning the same way as the original java code.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top