# 回调数据解密

回调数据解密示例代码

# 解密方式

解密KEY为当前应用的AppSecret

# php 示例代码

function decrypt(string $encryptedData, string $key)
{
    $key = substr($key, 0, 32);
    $decodedData = base64_decode($encryptedData);
    $iv = substr($decodedData, 0, 16);
    $encrypted = substr($decodedData, 16);
    $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
    return $decrypted;
}

$key = 'user-AppSecret';
$decodedData = '密文内容';
var_dump(decrypt($decodedData, $key));

# java 示例代码


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class EncryptionExample {

    private static final String ENCRYPTION_ALGORITHM = "AES/CBC/PKCS5Padding";
    
    private static final String SECRET_KEY = "user-AppSecret-key";

    public static String decrypt(String encryptedData) throws Exception {
        byte[] decodedBytes = Base64.getDecoder().decode(encryptedData);
        byte[] ivBytes = new byte[16];
        byte[] encryptedBytes = new byte[decodedBytes.length - 16];
        System.arraycopy(decodedBytes, 0, ivBytes, 0, 16);
        System.arraycopy(decodedBytes, 16, encryptedBytes, 0, encryptedBytes.length);

        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.substring(0, 32).getBytes("UTF-8"), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes);
    }

    public static void main(String[] args) {
        try {
            // 加密字符串
            String encryptedString = "85Z0kFc/X0EXUUZes5Wdni9jXOuZiWb5uRGZrk+48dkoourTAXBO4jZR8au8vXqFKKyqpTo/7Detwww9p8HdVN4cCD4wh/S3ic1jCTPJVI8cEowGAFoYNAi5mPOg4y8F/T1U8lqyL7TxrYx6P5KRoSwFbjj9z0e9rHvh/fWtaA/XirRLebVqA3zzYigKPx3m";

            // 解密字符串
            String decryptedString = decrypt(encryptedString);
            System.out.println("解密后的字符串: " + decryptedString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}