php的openssl_encrypt改写成java版

一、需求

我们之前的服务是php的,然后接下来的功能打算用java搞一下,php的功能也要后续迁移过来,然后数据库里的手机号是加密过的,所以java这边也需要按照相同的规则去加解密,下面就是过程

二、实现

1、php

先看下原来的php代码

public static function encryptNew($plaintext)
    //加密模式选择的AES-128-ECB,注意128表示密钥应该是16位的,16x8=128
        $cipher = 'AES-128-ECB';
        //此处是密钥
        $key = ENCRYPT_KEY;
        //此处进行AES-128-ECB加密
        $ciphertext_raw = openssl_encrypt($plaintext, $cipher, $key, OPENSSL_RAW_DATA);
        //得到AES-128-ECB的加密结果,继续进行sha256加密
        $hmac = hash_hmac('sha256', $ciphertext_raw, $key, true);
        //将两个加密串拼起来进行base64,得到最终结果
        $ciphertext = base64_encode($hmac . $ciphertext_raw);
        return $ciphertext;
}

此处需要注意两点:

  • AES-128-ECB的密钥是16位
  • AES-128-ECB是不需要iv的

2、java

然后看一下对应的java代码,java用的是hutool包来做的加解密,pom引用如下:

       <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.22</version>
       </dependency>

3、接下来展示代码:

/**
     * 加密啊
     * @param str 要加密的字符串
     * phpkey就是密钥=ENCRYPT_KEY的前16位
     * @return
    public static String phpEcbEncryptHex(String str) {
        AES aes = new AES(Mode.ECB, Padding.PKCS5Padding, phpkey);
        byte[] encrypted = aes.encrypt(str);
        byte[] hash= new byte[0];
        try {
            hash = macSHA256(encrypted);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        byte[] data3 = new byte[encrypted.length + hash.length];
        System.arraycopy(hash, 0, data3, 0, hash.length);
        System.arraycopy(encrypted, 0, data3, hash.length, encrypted.length);
        String base64Str3 = Base64.encode(data3);
        System.out.println("base 64 is "+base64Str3);
        return base64Str3;
     * sha256加密
     * @param encrypted
     * @return mackey是加密的密钥=ENCRYPT_KEY
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
    private static byte[] macSHA256(byte[] encrypted ) throws NoSuchAlgorithmException, InvalidKeyException {
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(mackey, "HmacSHA256");
        sha256_HMAC.init(secret_key);