小编LZZ*_*LZZ的帖子

无法在Java和PHP之间交换使用AES-256加密的数据

我的问题是:我在Java中加密的东西我可以在Java中完美解密,但PHP mcrypt无法解密.我加密的内容mcrypt可以解密mcrypt,但不能用Java 解密.

我想从Java应用程序发送和接收加密数据到PHP页面,所以我需要它兼容.

这是我的...

JAVA ...

public static String crypt(String input, String key){
    byte[] crypted = null;
    try{
        SecretKeySpec skey = new SecretKeySpec(Base64.decodeBase64(key), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skey);
        crypted = cipher.doFinal(input.getBytes());
    }catch(Exception e){
    }
    return Base64.encodeBase64String(crypted);
}

public static String decrypt(String input, String key){
    byte[] output = null;
    try{
        SecretKeySpec skey = new SecretKeySpec(Base64.decodeBase64(key), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skey);
        output = cipher.doFinal(Base64.decodeBase64(input));
    }catch(Exception e){
    }
    return new String(output);
}
Run Code Online (Sandbox Code Playgroud)

运行: …

php java encryption aes

12
推荐指数
2
解决办法
2万
查看次数

标签 统计

aes ×1

encryption ×1

java ×1

php ×1