# Java

# Java AES256 代码示例

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Base64;
import java.util.Random;
import java.util.Iterator;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import net.sf.json.JSONArray;
import net.sf.json.JSONFunction;
import net.sf.json.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.RequestBody;
import okhttp3.MediaType;

public class App {
    private static String SNUSER = "xxx";
    private static String SNKEY = "xxxx";

    private static byte[] decryptAES(String phoneno) {
        try {
            byte[] asBytes = Base64.getDecoder().decode(phoneno.toString().replace("\n", ""));
            byte[] iv = new byte[16];
            byte[] encryptBytes = new byte[asBytes.length - iv.length];

            System.arraycopy(asBytes, 0, iv, 0, iv.length);
            System.arraycopy(asBytes, iv.length, encryptBytes, 0, encryptBytes.length);

            SecretKeySpec key = new SecretKeySpec(SNKEY.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
            IvParameterSpec ivspec = new IvParameterSpec(iv);

            cipher.init(Cipher.DECRYPT_MODE, key, ivspec);
            byte[] result = cipher.doFinal(encryptBytes);
            return result;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void Check() {
        OkHttpClient client = new OkHttpClient();

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType,
            "{\"snuser\": \"" + SNUSER + "\"}");
        Request request = new Request.Builder()
            .url("https://api.yazx.com/phone/check/v3/callback")
            .post(body)
            .addHeader("content-type", "application/json")
            .build();

        try (Response response = client.newCall(request).execute()) {
            String jsonData = response.body().string();;
            System.out.println("response plain =>" + jsonData);

            JSONObject object = JSONObject.fromObject(jsonData);
            String encryptData = object.getString("data");
            byte[] deDataBytes = decryptAES(encryptData);
            System.out.println("result => success" + new String(deDataBytes, "utf-8"));
        }  catch (Exception e) {
            System.out.println("result => failed");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Check();
    }
}
Last Updated: 1/6/2023, 3:12:40 PM