当前位置:   article > 正文

java 和 php 的AES 128位 256位 加解密 【java解密php的AES加密方案】_aes128

aes128

项目需要 需要java可以解密 php加密的 字符串
使用的方法是 AES128位加解密

坑一踩完 ,还是直接上代码

package com.xxx.init.utils;


import com.xxx.init.utils.BaseDataUtil;
import com.xxx.init.exception.xxxRuntimeException;
import com.xxx.init.out.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

import java.util.Base64;

/**
 * User:Json
 * Date: 2024/4/23
 * 
 **/
@Slf4j
public class AesHelper {
    /*
     * aes 128
     */
    public static String AES_128_ECB = "AES/ECB/PKCS5Padding";

    /*
     * aes 256
     */
    public static final String AES_256_ECB = "AES/256/ECB/PKCS5Padding"; // 使用 AES-256-ECB 模式

    //获取nacos配置文件下的密钥
    public static String checkSecretKey() {
        return checkSecretKey(BaseDataUtil.getSystemConfigNacos().getAes128Key());
    }

    //获取密钥
    public static String checkSecretKey(String secretKey) {
        if (StringUtils.isEmpty(secretKey)) {
            secretKey = BaseDataUtil.getSystemConfigNacos().getAes128Key();
        }
        if (StringUtils.isEmpty(secretKey)) {
            throw new xxxRuntimeException(ResultCode.AES_KEY_ERROR);
        }
        return secretKey;
    }

    public static String encrypt128(String data) {
           String secretKey = padToLengthBytes(checkSecretKey(), 16);
           return encrypt(data, secretKey, AES_128_ECB, null);
    }


    //加密方法,对数据进行加密,返回加密后的数据.
    public static String encrypt(String data, String secretKey, String method, String iv) {
        try {
            secretKey = checkSecretKey(secretKey);

            SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance(method); // 使用指定的加密模式
            if (StringUtils.isEmpty(iv)) {
                cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            } else {
                IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
                cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
            }

            byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
            //php那边 openssl_encrypt 返回值直接是 base64 然后拿到值后又base64_encode 一次
            // 所以 java 加密 也要base64 两遍 / 解密也一样 需要两遍
            return Base64.getEncoder().encodeToString(Base64.getEncoder().encodeToString(encryptedBytes).getBytes());
        } catch (Exception e) {
            log.error("【AES加密失败】:" + e.getMessage());
            throw new xxxRuntimeException(ResultCode.AES_ENCRYPT_ERROR);
        }
    }


    public static String decrypt128(String data) {
        String secretKey = padToLengthBytes(checkSecretKey(), 16);
        return decrypt(data, secretKey, AES_128_ECB, null);
    }

    //解密方法,对数据进行解密,返回解密后的数据.
    public static String decrypt(String data, String secretKey, String method, String iv) {
        try {
            secretKey = checkSecretKey(secretKey);
            SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance(method); // 使用指定的解密模式
            if (StringUtils.isEmpty(iv)) {
                cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            } else {
                IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
                cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);
            }
            //base64 解密两次
            byte[] decodedBytes = Base64.getDecoder().decode(new String(Base64.getDecoder().decode(data)));
            byte[] decryptedBytes = cipher.doFinal(decodedBytes);
            return new String(decryptedBytes);
        } catch (Exception e) {
            log.error("【AES解密失败】:" + e.getMessage());
            throw new xxxRuntimeException(ResultCode.AES_DECRYPT_ERROR);
        }

    }

    //如果密钥 不足16位 或者超过16位的 处理方案 保证和php 那边一样 php的openssl_encrypt会自动补全 java需要手动补全
    //input 要补全的字符串 , length 跟多少位补全  常用的 16位 32位
    public static String padToLengthBytes(String input, int length) {
        // 转换input为字节数组
        byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
        // 如果输入的字节数大于指定长度,只取前指定长度字节;否则,补全到指定长度字节
        byte[] keyBytes = new byte[length];
        if (inputBytes.length > length) {
            System.arraycopy(inputBytes, 0, keyBytes, 0, length);
        } else {
            int paddingLength = length - inputBytes.length % length;
            byte[] paddedKeyBytes = new byte[inputBytes.length + paddingLength];
            System.arraycopy(inputBytes, 0, paddedKeyBytes, 0, inputBytes.length);
            keyBytes = paddedKeyBytes;
        }
        // 将keyBytes转换为字符串
        return new String(keyBytes, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {

        String encrypted = AesHelper.encrypt("哈哈,我是谁!",padToLengthBytes("11111",16),AES_128_ECB,null);
        String decrypted = AesHelper.decrypt("ZkN4UUoyL0RqOEtYYWJNblk2VjI3YjNjN3l4d0ZGSEYwc1J5WXNzZDlxVT0=",padToLengthBytes("111111",16),AES_128_ECB,null);
        System.out.println("加密值:" + encrypted);
       System.out.println("解密值:" + decrypted);


    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138

如果疑问 php openssl_encrypt() 为什么加密后会直接base64 可以了解一下 openssl_encrypt 下面3个常量
所以java这边需要不需要 base64 两边取决于 php 用没用 下面三个常量

OPENSSL_RAW_DATA=1
OPENSSL_ZERO_PADDING=2
OPENSSL_NO_PADDING=3
  • 1
  • 2
  • 3

结束

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/856604
推荐阅读
相关标签
  

闽ICP备14008679号