|
| 1 | +/* |
| 2 | + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are met: |
| 6 | + * |
| 7 | + * Redistributions of source code must retain the above copyright notice, |
| 8 | + * this list of conditions and the following disclaimer. |
| 9 | + * Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * Neither the name of the dreamlu.net developer nor the names of its |
| 13 | + * contributors may be used to endorse or promote products derived from |
| 14 | + * this software without specific prior written permission. |
| 15 | + * Author: Chill 庄骞 ([email protected]) |
| 16 | + */ |
| 17 | +package org.springblade.gateway.utils; |
| 18 | + |
| 19 | +import lombok.SneakyThrows; |
| 20 | +import org.springframework.util.Assert; |
| 21 | +import org.springframework.util.Base64Utils; |
| 22 | +import org.springframework.util.StringUtils; |
| 23 | +import reactor.util.annotation.Nullable; |
| 24 | + |
| 25 | +import javax.crypto.Cipher; |
| 26 | +import javax.crypto.spec.IvParameterSpec; |
| 27 | +import javax.crypto.spec.SecretKeySpec; |
| 28 | +import java.nio.charset.Charset; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +/** |
| 34 | + * JwtCrypto |
| 35 | + * |
| 36 | + * @author Chill |
| 37 | + */ |
| 38 | +public class JwtCrypto { |
| 39 | + |
| 40 | + public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; |
| 41 | + public static final String BLADE_CRYPTO_AES_KEY = "blade.token.aes-key"; |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Base64加密 |
| 46 | + * |
| 47 | + * @param content 文本内容 |
| 48 | + * @param aesTextKey 文本密钥 |
| 49 | + * @return {String} |
| 50 | + */ |
| 51 | + public static String encryptToString(String content, String aesTextKey) { |
| 52 | + return Base64Utils.encodeToString(encrypt(content, aesTextKey)); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Base64加密 |
| 57 | + * |
| 58 | + * @param content 内容 |
| 59 | + * @param aesTextKey 文本密钥 |
| 60 | + * @return {String} |
| 61 | + */ |
| 62 | + public static String encryptToString(byte[] content, String aesTextKey) { |
| 63 | + return Base64Utils.encodeToString(encrypt(content, aesTextKey)); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * 加密 |
| 68 | + * |
| 69 | + * @param content 文本内容 |
| 70 | + * @param aesTextKey 文本密钥 |
| 71 | + * @return byte[] |
| 72 | + */ |
| 73 | + public static byte[] encrypt(String content, String aesTextKey) { |
| 74 | + return encrypt(content.getBytes(DEFAULT_CHARSET), aesTextKey); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * 加密 |
| 79 | + * |
| 80 | + * @param content 文本内容 |
| 81 | + * @param charset 编码 |
| 82 | + * @param aesTextKey 文本密钥 |
| 83 | + * @return byte[] |
| 84 | + */ |
| 85 | + public static byte[] encrypt(String content, Charset charset, String aesTextKey) { |
| 86 | + return encrypt(content.getBytes(charset), aesTextKey); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * 加密 |
| 91 | + * |
| 92 | + * @param content 内容 |
| 93 | + * @param aesTextKey 文本密钥 |
| 94 | + * @return byte[] |
| 95 | + */ |
| 96 | + public static byte[] encrypt(byte[] content, String aesTextKey) { |
| 97 | + return encrypt(content, Objects.requireNonNull(aesTextKey).getBytes(DEFAULT_CHARSET)); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Base64解密 |
| 102 | + * |
| 103 | + * @param content 文本内容 |
| 104 | + * @param aesTextKey 文本密钥 |
| 105 | + * @return {String} |
| 106 | + */ |
| 107 | + @Nullable |
| 108 | + public static String decryptToString(@Nullable String content, @Nullable String aesTextKey) { |
| 109 | + if (!StringUtils.hasText(content) || !StringUtils.hasText(aesTextKey)) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + byte[] hexBytes = decrypt(Base64Utils.decode(content.getBytes(DEFAULT_CHARSET)), aesTextKey); |
| 113 | + return new String(hexBytes, DEFAULT_CHARSET); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + /** |
| 118 | + * 解密 |
| 119 | + * |
| 120 | + * @param content 内容 |
| 121 | + * @param aesTextKey 文本密钥 |
| 122 | + * @return byte[] |
| 123 | + */ |
| 124 | + public static byte[] decrypt(byte[] content, String aesTextKey) { |
| 125 | + return decrypt(content, Objects.requireNonNull(aesTextKey).getBytes(DEFAULT_CHARSET)); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + /** |
| 130 | + * 解密 |
| 131 | + * |
| 132 | + * @param content 内容 |
| 133 | + * @param aesKey 密钥 |
| 134 | + * @return byte[] |
| 135 | + */ |
| 136 | + public static byte[] encrypt(byte[] content, byte[] aesKey) { |
| 137 | + return aes(Pkcs7Encoder.encode(content), aesKey, Cipher.ENCRYPT_MODE); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * 加密 |
| 142 | + * |
| 143 | + * @param encrypted 内容 |
| 144 | + * @param aesKey 密钥 |
| 145 | + * @return byte[] |
| 146 | + */ |
| 147 | + public static byte[] decrypt(byte[] encrypted, byte[] aesKey) { |
| 148 | + return Pkcs7Encoder.decode(aes(encrypted, aesKey, Cipher.DECRYPT_MODE)); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * ase加密 |
| 153 | + * |
| 154 | + * @param encrypted 内容 |
| 155 | + * @param aesKey 密钥 |
| 156 | + * @param mode 模式 |
| 157 | + * @return byte[] |
| 158 | + */ |
| 159 | + @SneakyThrows |
| 160 | + private static byte[] aes(byte[] encrypted, byte[] aesKey, int mode) { |
| 161 | + Assert.isTrue(aesKey.length == 32, "IllegalAesKey, aesKey's length must be 32"); |
| 162 | + Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); |
| 163 | + SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES"); |
| 164 | + IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16)); |
| 165 | + cipher.init(mode, keySpec, iv); |
| 166 | + return cipher.doFinal(encrypted); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * 提供基于PKCS7算法的加解密接口. |
| 171 | + */ |
| 172 | + private static class Pkcs7Encoder { |
| 173 | + private static final int BLOCK_SIZE = 32; |
| 174 | + |
| 175 | + private static byte[] encode(byte[] src) { |
| 176 | + int count = src.length; |
| 177 | + // 计算需要填充的位数 |
| 178 | + int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE); |
| 179 | + // 获得补位所用的字符 |
| 180 | + byte pad = (byte) (amountToPad & 0xFF); |
| 181 | + byte[] pads = new byte[amountToPad]; |
| 182 | + for (int index = 0; index < amountToPad; index++) { |
| 183 | + pads[index] = pad; |
| 184 | + } |
| 185 | + int length = count + amountToPad; |
| 186 | + byte[] dest = new byte[length]; |
| 187 | + System.arraycopy(src, 0, dest, 0, count); |
| 188 | + System.arraycopy(pads, 0, dest, count, amountToPad); |
| 189 | + return dest; |
| 190 | + } |
| 191 | + |
| 192 | + private static byte[] decode(byte[] decrypted) { |
| 193 | + int pad = decrypted[decrypted.length - 1]; |
| 194 | + if (pad < 1 || pad > BLOCK_SIZE) { |
| 195 | + pad = 0; |
| 196 | + } |
| 197 | + if (pad > 0) { |
| 198 | + return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad); |
| 199 | + } |
| 200 | + return decrypted; |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments