Skip to Content
TransferSample CodesJava

Java

Encrypt

import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AESEncrypt { public static String encrypt(String data, String key, String iv) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); int blockSize = cipher.getBlockSize(); byte[] dataBytes = data.getBytes("UTF-8"); int plainTextLength = dataBytes.length; if (plainTextLength % blockSize != 0) { plainTextLength = plainTextLength + (blockSize - plainTextLength % blockSize); } byte[] plaintext = new byte[plainTextLength]; System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); return Base64.encodeBase64URLSafeString(encrypted); } // Sample public static void main(String[] args) throws Exception { String data = "SampleData"; System.out.println(encrypt(data, $ { KEY }, $ { IV })); } }

Decrypt

import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AESDecrypt { public static String decrypt(String data, String key, String iv) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"), new IvParameterSpec(iv.getBytes())); String decryptData = new String(cipher.doFinal(Base64.decodeBase64(data))); return decryptData; } // Sample public static void main(String[] args) throws Exception { String encryptData = "GhKRVa6BHoOJAhjVYMKuFw"; String key = "key1234567Sample"; // $ { KEY } String iv = "iv12345678Sample"; // $ { IV } System.out.println(decrypt(encryptData, key, iv)); // return “SampleData” } }

Call API

Example: Action 47 Get Demo Game Launch URL

AESEncrypt: Please refer to the “Encrypt” sample code

import net.sf.json.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.util.ArrayList; import java.util.List; public class SandAPI { public static void main(String[] args) throws Exception { String dc = $ { DC }; String key = $ { KEY }; String iv = $ { IV }; String apiUrl = $ { API_URL }; // get a client CloseableHttpClient client = HttpClientBuilder.create().build(); // prepare action 47 data JSONObject data = new JSONObject(); data.put("action", 47); data.put("ts", System.currentTimeMillis()); data.put("lang", "en"); data.put("gType", "0"); data.put("mType", "8001"); data.put("windowMode", "2"); // encrypt String x = AESEncrypt.encrypt(data.toString(), key, iv); // build request List<NameValuePair> paramList = new ArrayList<>(); paramList.add(new BasicNameValuePair("dc", dc)); paramList.add(new BasicNameValuePair("x", x)); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "UTF-8"); HttpPost httpPost = new HttpPost(apiUrl); httpPost.setEntity(entity); // gzip // httpPost.setHeader("Accept-Encoding", "gzip"); HttpEntity httpEntity = null; try { // Resolve response HttpResponse response = client.execute(httpPost); httpEntity = response.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } finally { EntityUtils.consume(httpEntity); } } }
Last updated on