Senin, 26 April 2021

Decrypt Tokopedia Buyer Info from secret content response

 Halo, kali ini saya akan membagikan contoh salah satu penggunaan class java.crypto.Chiper untuk mendecrypt data yang diperoleh dari response tokopedia seller api.

Contoh response yang didapat dari salah satu endpoint

(kalian bisa mempelajari lebih lanjut di link berikut: https://developer.tokopedia.com/openapi/guide/#/encryption/

Tutorial kali ini hanya fokus pada fungsi decryption, sehingga jika kalian ingin mengintegrasi aplikasi kalian dengan tokopedia, mungkin konten kali ini dapat membantu kalian pada bagian decryprtion.

Sebelumnya kalian harus register public key kalian terlebih dahulu dengan mengikuti langkah-langkah berikut:
1. Download script here
2. Run script generate public_key.pub private_key.pem
3. Upload via API using Postman

POST /v1/fs/:fs_id/register?upload=1

Decryption Process

Decryption process

Step#1
Pada ilustrasi diatas kita perlu mengunduh private_key kita ke dalam aplikasi. Khusus untuk pemrograman menggunakan java kita perlu meng-convert terlebih dahulu private_key.pem menjadi PKCS8 private key (ref).
Bisa menggunakan link berikut: https://decoder.link/rsa_converter
Sebagai contoh program kali ini kita lakukan manual saja dengan meletakkan private_key secara hardcode.

private static PrivateKey readPrivateKey() throws Exception {

String privateKeyPEM = “xxxxx”;
byte[] encoded = Base64.decodeBase64(privateKeyPEM);
KeyFactory keyFactory = KeyFactory.getInstance(“RSA”);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);

return keyFactory.generatePrivate(keySpec);

Step#2
Selanjutnya kita mendecrypt secret, yaitu mendecode dengan base64 terlebih dahulu lalu menggunakan RSA OAEP 256

private static byte[] decryptSecret(String encryptedSecret) throws Exception{

byte[] secret=null;
PrivateKey rsaPrivateKey = readPrivateKey();
Cipher cipher = Cipher.getInstance(“RSA/ECB/OAEPPadding”);
OAEPParameterSpec oaepParams = new OAEPParameterSpec(“SHA-256”, “MGF1”, new MGF1ParameterSpec(“SHA-256”), PSource.PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey, oaepParams);
secret = cipher.doFinal(Base64.decodeBase64(encryptedSecret));

return secret;
}

Step#3
Langkah akhir kita mendecrypt content dengan menggunakan AES GCM dan secret key yang telah di-decrypt pada langkah sebelumnya

private static String decryptContent(byte[] cipherText, SecretKey key, byte[] IV) throws Exception{

Cipher cipher = Cipher.getInstance(“AES/GCM/NoPadding”);
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), “AES”);
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128* 8, IV);
cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);
byte[] decryptedText = cipher.doFinal(cipherText);

return new String(decryptedText);
}

Selamat, return string dari step#3 merupakan json string yang merupakan data buyer_info. Terimakasih

Share:

3 komentar:

  1. Penjelasan yang bagus, terima kasih !

    BalasHapus
  2. There are tons of options and most of casino.edu.kg them have reasonably solid ratings. HUUUGE Games is a recreation studio that does a variety of|quite lots of|a wide selection of} casino-style games. They are all free-to-play games so you’ll get the same old} runaround in terms of|in relation to} mechanics. Most of these require coins to play and you’ll get new coins to play with every so often.

    BalasHapus
  3. Join tens of millions of players and revel in a fantastic expertise on the web or any 1xbet system; from PCs to tablets and cell phones . Lining up three 7's starts a 15-round bonus sport, while three Poké Balls will set off an 8-round bonus. In this bonus sport, players try to completely line up a randomly chosen considered one of Johto's starter Pokémon, and the machine might provide some help. Bonus sport payouts range relying on success, properly as|in addition to} the slot machine's pace during bonus games. At the top of a bonus sport, the background routinely becomes pink.

    BalasHapus