加密解密 - 用php实现java中的aes加密

WBOY
发布: 2016-10-19 10:40:53
原创
1436 人浏览过

java中的aes加密算法如下:

<code>    public static byte[] decrypt(byte[] data, byte[] key)
            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Security.addProvider(new BouncyCastleProvider());
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
        cipher.init(2, new SecretKeySpec(key, "AES"));
        return cipher.doFinal(data);
    }

    public static byte[] encrypt(byte[] data, byte[] key)
            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Security.addProvider(new BouncyCastleProvider());
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
        cipher.init(1, new SecretKeySpec(key, "AES"));
        return cipher.doFinal(data);
    }</code>
登录后复制
登录后复制

请问如何用php来实现
我测试了很多网上的例子都不能得到相同的密文

回复内容:

java中的aes加密算法如下:

<code>    public static byte[] decrypt(byte[] data, byte[] key)
            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Security.addProvider(new BouncyCastleProvider());
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
        cipher.init(2, new SecretKeySpec(key, "AES"));
        return cipher.doFinal(data);
    }

    public static byte[] encrypt(byte[] data, byte[] key)
            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Security.addProvider(new BouncyCastleProvider());
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
        cipher.init(1, new SecretKeySpec(key, "AES"));
        return cipher.doFinal(data);
    }</code>
登录后复制
登录后复制

请问如何用php来实现
我测试了很多网上的例子都不能得到相同的密文

可能是编码的问题,java默认是GBK编码;
php编码类型与页面的保存编码有关。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!