Java AES-256 encryption, string "Hex doubt"

is learning about the cash flow recently, but after reading the API file provided by the counterpart, there is only an example of PHP, which is not very clear, so let"s mention

.

I will have a set of hashKey and HashIv and a whole string of String;

HashKey: 12345678901234567890123456789012
HashIv: 1234567890123456
String: MerchantID = 3430112 RespondType = JSON TimeStamp = 1485232229 Version = 1.4MerchantOrderNo = 1485232229 Amt = 40 ItemDesc = UnitTest;

I am doing this at present, but the results are completely wrong. I am busy with all the experts to see if I am a beginner.

public static String encrypt(String hashKey, String hashIv, String text) {
        try {

            SecretKeySpec skeySpec = new SecretKeySpec(hashKey.getBytes("UTF-8"), "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(hashIv.getBytes("UTF-8"));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
            byte[] original = cipher.doFinal(text.getBytes("UTF-8"));
            return Base64.encodeBase64URLSafeString(original).toLowerCase();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

related codes

function create_mpg_aes_encrypt ($parameter = "" , $key = "", $iv = "") {
 $return_str = "";
 if (!empty($parameter)) {
 // URL ENCODED QUERY STRING
 $return_str = http_build_query($parameter);
 }
 return trim(bin2hex(openssl_encrypt(addpadding($return_str), "aes-256-
cbc", $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv)));
 }
function addpadding($string, $blocksize = 32) {
 $len = strlen($string);
 $pad = $blocksize - ($len % $blocksize);
 $string .= str_repeat(chr($pad), $pad);
 return $string;
 }
$trade_info_arr = array(
"MerchantID" => 3430112,
"RespondType" => "JSON",
"TimeStamp" => 1485232229,
"Version" => 1.4,
"MerchantOrderNo" => "S_1485232229",
"Amt" => 40,
"ItemDesc" => "UnitTest"
);
$mer_key = "12345678901234567890123456789012";
$mer_iv = "1234567890123456";
// AES  TradeInfo
$TradeInfo = create_mpg_aes_encrypt($trade_info_arr, $mer_key, $mer_iv)

the result is
ff91c8aa01379e4de621a44e5f11f72e4d25bdb1a18242db6cef9ef07d80b0165e476fd1d
9acaa53170272c82d122961e1a0700a7427cfa1cf90db7f6d6593bbc93102a4d4b9b66d9
974c13c31a7ab4bba1d4e0790f0cbbbd7ad64c6d3c8012a601ceaa808bff70f94a8efa5a4f
984b9d41304ffd879612177c622f75f4214fa

Nov.10,2021

public static String encryptSpgateway(String hashKey, String hashIv, String value) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(hashKey.getBytes("UTF-8"), "AES");
            IvParameterSpec iv = new IvParameterSpec(hashIv.getBytes("UTF-8"));
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(addPKCS7Padding(value.getBytes("UTF-8"), 32));
            String result = bytesToHex(encrypted);
            return result.toLowerCase();
        } catch (Exception e) {
            logger.log(Level.SEVERE, e.getMessage(), e);
        }
        return null;
    }

    private static byte[] addPKCS7Padding(byte[] data, int iBlockSize) {
        int iLength = data.length;
        byte cPadding = (byte) (iBlockSize - (iLength % iBlockSize));
        byte[] output = new byte[iLength + cPadding];
        System.arraycopy(data, 0, output, 0, iLength);
        for (int i = iLength; i < output.length; iPP)
            output[i] = (byte) cPadding;
        return output;
    }

    public static String bytesToHex(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; iPP) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex);
        }
        return sb.toString();
    }
Menu