| | |
| | | |
| | | import com.hdl.sdk.common.utils.LogUtils; |
| | | |
| | | import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| | | |
| | | import java.security.InvalidAlgorithmParameterException; |
| | | import java.security.InvalidKeyException; |
| | | import java.security.Key; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.security.Security; |
| | | |
| | | 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 cn.hutool.core.util.HexUtil; |
| | | |
| | | /** |
| | | * Aes 加解密工具类 |
| | |
| | | */ |
| | | public class AesUtil { |
| | | |
| | | private static final String CIPHER_INSTANCE = "AES/ECB/PKCS5Padding"; |
| | | private static final String CIPHER_INSTANCE = "AES/CBC/PKCS7Padding"; |
| | | private static final String AES_ALGORITHM = "AES"; |
| | | |
| | | static { |
| | | //AES/CBC/PKCS7Padding 依赖 |
| | | Security.addProvider(new BouncyCastleProvider()); |
| | | } |
| | | // static { |
| | | // //AES/CBC/PKCS7Padding 依赖 |
| | | // Security.addProvider(new BouncyCastleProvider()); |
| | | // } |
| | | |
| | | /** |
| | | * AES加密 |
| | |
| | | * @throws Exception |
| | | */ |
| | | public static byte[] aesEncrypt(byte[] content, String key) { |
| | | return encrypt(content, key.getBytes(), "AES/CBC/PKCS7Padding", true, null); |
| | | return encrypt(content, key.getBytes(), CIPHER_INSTANCE, true, null); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @throws Exception |
| | | */ |
| | | public static byte[] aesDecrypt(byte[] content, String key) { |
| | | return decrypt(content, key.getBytes(), "AES/CBC/PKCS7Padding", true, null); |
| | | return decrypt(content, key.getBytes(), CIPHER_INSTANCE, true, null); |
| | | } |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param content 待加密字符串 |
| | | * @param keyHex aesKye的hexStr |
| | | * @return |
| | | */ |
| | | public static String encrypt(String content, String keyHex) { |
| | | return HexUtil.encodeHexStr(encrypt(content.getBytes(), HexUtil.decodeHex(keyHex), CIPHER_INSTANCE, false, null)); |
| | | } |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param content 待加密字符串 |
| | | * @param key 密钥 |
| | | * @return |
| | | */ |
| | | public static String encrypt(String content, byte[] key) { |
| | | return HexUtil.encodeHexStr(encrypt(content.getBytes(), key, CIPHER_INSTANCE, false, null)); |
| | | } |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param content 待加密字符串 |
| | | * @param keyHex aesKye的hexStr |
| | | * @return |
| | | */ |
| | | public static byte[] encryptReturnByte(String content, String keyHex) { |
| | | return encrypt(content.getBytes(), HexUtil.decodeHex(keyHex), CIPHER_INSTANCE, false, null); |
| | | } |
| | | |
| | | /** |
| | | * 加密 |
| | | * |
| | | * @param content 待加密字符串 |
| | | * @param key 密钥 |
| | | * @return |
| | | */ |
| | | public static byte[] encryptReturnByte(String content, byte[] key) { |
| | | return encrypt(content.getBytes(), key, CIPHER_INSTANCE, false, null); |
| | | } |
| | | |
| | | /** |
| | | * 加密 |
| | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param contentHex 待解密待字符串hexStr |
| | | * @param keyHex desKye的hexStr |
| | | * @return |
| | | */ |
| | | public static String decrypt(String contentHex, String keyHex) { |
| | | return new String(decrypt(HexUtil.decodeHex(contentHex), HexUtil.decodeHex(keyHex), CIPHER_INSTANCE, false, null)); |
| | | } |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param contentHex 待解密待字符串hexStr |
| | | * @param key 密钥 |
| | | * @return |
| | | */ |
| | | public static String decrypt(String contentHex, byte[] key) { |
| | | return new String(decrypt(HexUtil.decodeHex(contentHex), key, CIPHER_INSTANCE, false, null)); |
| | | } |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param content 待解密待内容 |
| | | * @param keyHex 密钥 |
| | | * @return |
| | | */ |
| | | public static String decrypt(byte[] content, String keyHex) { |
| | | return new String(decrypt(content, HexUtil.decodeHex(keyHex), CIPHER_INSTANCE, false, null)); |
| | | } |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param content 待解密待内容 |
| | | * @param key 密钥 |
| | | * @return |
| | | */ |
| | | public static String decrypt(byte[] content, byte[] key) { |
| | | return new String(decrypt(content, key, CIPHER_INSTANCE, false, null)); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param contentByte 待解密待字符串hexStr |
| | | * @param contentByte 密钥 |
| | | * @param cipherInstance AES算法填充方式 |
| | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | // jiemi(); |
| | | //jiami(); |
| | | } |
| | | |
| | | public static void jiami() { |
| | | String key = "1f6714fc-fb9b-4a"; |
| | | String content = "{\"id\":\"123\",\"objects\":[{\"sid\":\"0001011565879801020200010101\",\"type\":\"PWD\",\"local_id\":\"2\"}],\"time_stamp\":\"1603281282000\"}"; |
| | | byte[] res = aesEncrypt(content.getBytes(), key); |
| | | System.out.println(HexUtil.encodeHexStr(res)); |
| | | } |
| | | |
| | | // /** |
| | | // * |
| | | // * @param str |
| | | // * @param houseId |
| | | // * @return |
| | | // */ |
| | | // public static String jiemi(byte[] str, String houseId) { |
| | | // byte[] rs = aesDecrypt(str, HouseIdSecretUtil.getSecret(houseId)); |
| | | // String content = new String(rs); |
| | | // return content; |
| | | // } |
| | | |
| | | // public static void main(String[] args) { |
| | | // try { |
| | | // //第一步: 生成KEY |
| | | // //KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_ALGORITHM); |
| | | // //keyGenerator.init(256); |
| | | // //第二步: 产生密钥 |
| | | // //SecretKey secretKey = keyGenerator.generateKey(); |
| | | // //第三步: 获取密钥 |
| | | // //byte[] keyBytes = secretKey.getEncoded(); |
| | | // //System.out.println(byteArrayToHexStr(keyBytes)); |
| | | // String src = "aaaaaaaaaVVVVaaaAAAA无"; |
| | | // String pwd = "41151AF257BFDB7859EEC62FB341EE95EE42E648FE24E1F8CE8DADE287CC1E5C"; |
| | | // String mw = encrypt(src,pwd); |
| | | // System.out.println("mw:"+mw); |
| | | // String jm = decrypt(mw,pwd); |
| | | // System.out.println("jm:"+jm); |
| | | // |
| | | // }catch (Exception e){ |
| | | // |
| | | // } |
| | | // |
| | | // } |
| | | } |