AES加密解密就不说了,网上百度一堆。需要注意的是java与php互通要选择

AES/CBC/NoPadding

下面还是只接上代码吧


package com.zns.crypto;

import org.apache.commons.codec.binary.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Administrator on 2017/1/18 0018.
 */
public class AES {

    /**
     * AES加密
     * @param key 加密需要的KEY
     * @param iv 加密需要的向量
     * @param data 需要加密的数据
     * @return
     */
    public static String encrypt(String key, String iv, String data) {
        byte[] encrypted = {};
        byte[] enCodeFormat = key.getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat,"AES");

        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec,new IvParameterSpec(iv.getBytes()));

            int blockSize = cipher.getBlockSize();
            System.out.println(data.length());
            byte[] dataBytes = data.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }
            System.out.println(plaintextLength);
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
            encrypted = cipher.doFinal(plaintext);

        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }

        return new String(Base64.encodeBase64(encrypted));
    }

    /**
     *
     * @param key 解密需要的KEY 同加密
     * @param iv 解密需要的向量 同加密
     * @param data 需要解密的数据
     * @return
     */
    public static String decrypt(String key,String iv,byte[] data){
        String content = "";

        byte[] enCodeFormat = key.getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = null;// 创建密码器
        try {
            cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec,new IvParameterSpec(iv.getBytes()));// 初始化
            byte[] result = cipher.doFinal(data);
            content = new String(result);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }

        return content; // 加密
    }
}

测试用例:

public class Main {

    public static void main(String[] args) {
        String key = DigestUtils.sha1Hex("123456").substring(0,16);
        String iv = "1234567890123456";
        String data = "steven11111";
        String encryptData = AES.encrypt(key,iv ,data );
        System.out.println(encryptData);
        String decryptData = AES.decrypt(key,iv,Base64.decodeBase64(encryptData));
        System.out.println(decryptData);
    }
}


PHP的代码:

crypt方式:

<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/19 0019
 * Time: 上午 10:40
 */
class AES
{
    /**
     * @param $key 加密KEY
     * @param $iv 加密向量
     * @param $data 需要加密的数据
     * @return string
     */
    public static function encrypt($key, $iv, $data)
    {
        /**
         * 打开加密
         */
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, "", MCRYPT_MODE_CBC, "");
        /**
         * 初始化加密
         */
        mcrypt_generic_init($td, $key, $iv);
        /**
         * 加密
         */
        $encrypted = mcrypt_generic($td, $data);
        /**
         * 清理加密
         */
        mcrypt_generic_deinit($td);
        /**
         * 关闭
         */
        mcrypt_module_close($td);
        return base64_encode($encrypted);
    }

    /**
     * @param $key
     * @param $iv
     * @param $data
     * @return string
     */
    public static function decrypt($key, $iv, $data)
    {
        /**
         * 打开加密
         */
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,"",MCRYPT_MODE_CBC,"");
        /**
         * 初始化加密
         */
        mcrypt_generic_init($td, $key, $iv);
        $decode = base64_decode($data);
        /**
         * 解密
         */
        $dencrypted = mdecrypt_generic($td, $decode);
        /**
         * 清理加密
         */
        mcrypt_generic_deinit($td);
        /**
         * 关闭
         */
        mcrypt_module_close($td);

        return $dencrypted;
    }
}

测试用例:

include 'AES.class.php';

$key = substr(sha1("123456"), 0, 16);
$iv = "1234567890123456";
$data = "steven1234567891011121314";
$code = AES::encrypt($key,$iv,$data);
echo $code;
echo PHP_EOL;
echo AES::decrypt($key,$iv,"0IYawS+QIqyPnWvBAwXW7w==");

openssl方式:

$open=openssl_encrypt($data,'aes-128-cbc',$key,false,$iv);
echo $open;
echo PHP_EOL;
echo openssl_decrypt($open,'aes-128-cbc',$key,false,$iv);

代码都测试通过了的。大家有什么不同的意见可以给我留言啊。