From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001 From: xiejun <xiejun@vci-tech.com> Date: 星期五, 01 十一月 2024 15:11:19 +0800 Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成" --- Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/RsaUtil.java | 381 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 381 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/RsaUtil.java b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/RsaUtil.java new file mode 100644 index 0000000..e89471c --- /dev/null +++ b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/RsaUtil.java @@ -0,0 +1,381 @@ +/* + * Copyright (c) 2018-2028, DreamLu All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: DreamLu 鍗㈡槬姊� (596392912@qq.com) + */ +package org.springblade.core.tool.utils; + +import org.springframework.lang.Nullable; +import org.springframework.util.Base64Utils; + +import org.springblade.core.tool.tuple.KeyPair; +import javax.crypto.Cipher; +import java.math.BigInteger; +import java.security.*; +import java.security.spec.*; +import java.util.Objects; + +/** + * RSA鍔犮�佽В瀵嗗伐鍏� + * + * <p> + * 1. 鍏挜璐熻矗鍔犲瘑锛岀閽ヨ礋璐hВ瀵嗭紱 + * 2. 绉侀挜璐熻矗绛惧悕锛屽叕閽ヨ礋璐i獙璇併�� + * </p> + * + * @author L.cm + */ +public class RsaUtil { + /** + * 鏁板瓧绛惧悕锛屽瘑閽ョ畻娉� + */ + public static final String RSA_ALGORITHM = "RSA"; + public static final String RSA_PADDING = "RSA/ECB/PKCS1Padding"; + + /** + * 鑾峰彇 KeyPair + * + * @return KeyPair + */ + public static KeyPair genKeyPair() { + return genKeyPair(1024); + } + + /** + * 鑾峰彇 KeyPair + * + * @param keySize key size + * @return KeyPair + */ + public static KeyPair genKeyPair(int keySize) { + try { + KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(RSA_ALGORITHM); + // 瀵嗛挜浣嶆暟 + keyPairGen.initialize(keySize); + // 瀵嗛挜瀵� + return new KeyPair(keyPairGen.generateKeyPair()); + } catch (NoSuchAlgorithmException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 鐢熸垚RSA绉侀挜 + * + * @param modulus N鐗瑰緛鍊� + * @param exponent d鐗瑰緛鍊� + * @return {@link PrivateKey} + */ + public static PrivateKey generatePrivateKey(String modulus, String exponent) { + return generatePrivateKey(new BigInteger(modulus), new BigInteger(exponent)); + } + + /** + * 鐢熸垚RSA绉侀挜 + * + * @param modulus N鐗瑰緛鍊� + * @param exponent d鐗瑰緛鍊� + * @return {@link PrivateKey} + */ + public static PrivateKey generatePrivateKey(BigInteger modulus, BigInteger exponent) { + RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(modulus, exponent); + try { + KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); + return keyFactory.generatePrivate(keySpec); + } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 鐢熸垚RSA鍏挜 + * + * @param modulus N鐗瑰緛鍊� + * @param exponent e鐗瑰緛鍊� + * @return {@link PublicKey} + */ + public static PublicKey generatePublicKey(String modulus, String exponent) { + return generatePublicKey(new BigInteger(modulus), new BigInteger(exponent)); + } + + /** + * 鐢熸垚RSA鍏挜 + * + * @param modulus N鐗瑰緛鍊� + * @param exponent e鐗瑰緛鍊� + * @return {@link PublicKey} + */ + public static PublicKey generatePublicKey(BigInteger modulus, BigInteger exponent) { + RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, exponent); + try { + KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); + return keyFactory.generatePublic(keySpec); + } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 寰楀埌鍏挜 + * + * @param base64PubKey 瀵嗛挜瀛楃涓诧紙缁忚繃base64缂栫爜锛� + * @return PublicKey + */ + public static PublicKey getPublicKey(String base64PubKey) { + Objects.requireNonNull(base64PubKey, "base64 public key is null."); + byte[] keyBytes = Base64Utils.decodeFromString(base64PubKey); + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); + try { + KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); + return keyFactory.generatePublic(keySpec); + } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 寰楀埌鍏挜瀛楃涓� + * + * @param base64PubKey 瀵嗛挜瀛楃涓诧紙缁忚繃base64缂栫爜锛� + * @return PublicKey String + */ + public static String getPublicKeyToBase64(String base64PubKey) { + PublicKey publicKey = getPublicKey(base64PubKey); + return getKeyString(publicKey); + } + + /** + * 寰楀埌绉侀挜 + * + * @param base64PriKey 瀵嗛挜瀛楃涓诧紙缁忚繃base64缂栫爜锛� + * @return PrivateKey + */ + public static PrivateKey getPrivateKey(String base64PriKey) { + Objects.requireNonNull(base64PriKey, "base64 private key is null."); + byte[] keyBytes = Base64Utils.decodeFromString(base64PriKey); + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); + try { + KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); + return keyFactory.generatePrivate(keySpec); + } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 寰楀埌瀵嗛挜瀛楃涓诧紙缁忚繃base64缂栫爜锛� + * + * @param key key + * @return base 64 缂栫爜鍚庣殑 key + */ + public static String getKeyString(Key key) { + return Base64Utils.encodeToString(key.getEncoded()); + } + + /** + * 寰楀埌绉侀挜 base64 + * + * @param base64PriKey 瀵嗛挜瀛楃涓诧紙缁忚繃base64缂栫爜锛� + * @return PrivateKey String + */ + public static String getPrivateKeyToBase64(String base64PriKey) { + PrivateKey privateKey = getPrivateKey(base64PriKey); + return getKeyString(privateKey); + } + + /** + * 鍏辫鍔犲瘑 + * + * @param base64PublicKey base64 鐨勫叕閽� + * @param data 寰呭姞瀵嗙殑鍐呭 + * @return 鍔犲瘑鍚庣殑鍐呭 + */ + public static byte[] encrypt(String base64PublicKey, byte[] data) { + return encrypt(getPublicKey(base64PublicKey), data); + } + + /** + * 鍏辫鍔犲瘑 + * + * @param publicKey 鍏挜 + * @param data 寰呭姞瀵嗙殑鍐呭 + * @return 鍔犲瘑鍚庣殑鍐呭 + */ + public static byte[] encrypt(PublicKey publicKey, byte[] data) { + return rsa(publicKey, data, Cipher.ENCRYPT_MODE); + } + + /** + * 绉侀挜鍔犲瘑锛岀敤浜� qpp 鍐咃紝鍏挜瑙e瘑 + * + * @param base64PrivateKey base64 鐨勭閽� + * @param data 寰呭姞瀵嗙殑鍐呭 + * @return 鍔犲瘑鍚庣殑鍐呭 + */ + public static byte[] encryptByPrivateKey(String base64PrivateKey, byte[] data) { + return encryptByPrivateKey(getPrivateKey(base64PrivateKey), data); + } + + /** + * 绉侀挜鍔犲瘑锛屽姞瀵嗘垚 base64 瀛楃涓诧紝鐢ㄤ簬 qpp 鍐咃紝鍏挜瑙e瘑 + * + * @param base64PrivateKey base64 鐨勭閽� + * @param data 寰呭姞瀵嗙殑鍐呭 + * @return 鍔犲瘑鍚庣殑鍐呭 + */ + public static String encryptByPrivateKeyToBase64(String base64PrivateKey, byte[] data) { + return Base64Util.encodeToString(encryptByPrivateKey(base64PrivateKey, data)); + } + + /** + * 绉侀挜鍔犲瘑锛岀敤浜� qpp 鍐咃紝鍏挜瑙e瘑 + * + * @param privateKey 绉侀挜 + * @param data 寰呭姞瀵嗙殑鍐呭 + * @return 鍔犲瘑鍚庣殑鍐呭 + */ + public static byte[] encryptByPrivateKey(PrivateKey privateKey, byte[] data) { + return rsa(privateKey, data, Cipher.ENCRYPT_MODE); + } + + /** + * 鍏挜鍔犲瘑 + * + * @param base64PublicKey base64 鍏挜 + * @param data 寰呭姞瀵嗙殑鍐呭 + * @return 鍔犲瘑鍚庣殑鍐呭 + */ + @Nullable + public static String encryptToBase64(String base64PublicKey, @Nullable String data) { + if (StringUtil.isBlank(data)) { + return null; + } + return Base64Utils.encodeToString(encrypt(base64PublicKey, data.getBytes(Charsets.UTF_8))); + } + + /** + * 瑙e瘑 + * + * @param base64PrivateKey base64 绉侀挜 + * @param data 鏁版嵁 + * @return 瑙e瘑鍚庣殑鏁版嵁 + */ + public static byte[] decrypt(String base64PrivateKey, byte[] data) { + return decrypt(getPrivateKey(base64PrivateKey), data); + } + + /** + * 瑙e瘑 + * + * @param base64publicKey base64 鍏挜 + * @param data 鏁版嵁 + * @return 瑙e瘑鍚庣殑鏁版嵁 + */ + public static byte[] decryptByPublicKey(String base64publicKey, byte[] data) { + return decryptByPublicKey(getPublicKey(base64publicKey), data); + } + + /** + * 瑙e瘑 + * + * @param privateKey privateKey + * @param data 鏁版嵁 + * @return 瑙e瘑鍚庣殑鏁版嵁 + */ + public static byte[] decrypt(PrivateKey privateKey, byte[] data) { + return rsa(privateKey, data, Cipher.DECRYPT_MODE); + } + + /** + * 瑙e瘑 + * + * @param publicKey PublicKey + * @param data 鏁版嵁 + * @return 瑙e瘑鍚庣殑鏁版嵁 + */ + public static byte[] decryptByPublicKey(PublicKey publicKey, byte[] data) { + return rsa(publicKey, data, Cipher.DECRYPT_MODE); + } + + /** + * rsa 鍔犮�佽В瀵� + * + * @param key key + * @param data 鏁版嵁 + * @param mode 妯″紡 + * @return 瑙e瘑鍚庣殑鏁版嵁 + */ + private static byte[] rsa(Key key, byte[] data, int mode) { + try { + Cipher cipher = Cipher.getInstance(RSA_PADDING); + cipher.init(mode, key); + return cipher.doFinal(data); + } catch (Exception e) { + throw Exceptions.unchecked(e); + } + } + + /** + * base64 鏁版嵁瑙e瘑 + * + * @param base64PublicKey base64 鍏挜 + * @param base64Data base64鏁版嵁 + * @return 瑙e瘑鍚庣殑鏁版嵁 + */ + public static byte[] decryptByPublicKeyFromBase64(String base64PublicKey, byte[] base64Data) { + return decryptByPublicKey(getPublicKey(base64PublicKey), base64Data); + } + + /** + * base64 鏁版嵁瑙e瘑 + * + * @param base64PrivateKey base64 绉侀挜 + * @param base64Data base64鏁版嵁 + * @return 瑙e瘑鍚庣殑鏁版嵁 + */ + @Nullable + public static String decryptFromBase64(String base64PrivateKey, @Nullable String base64Data) { + if (StringUtil.isBlank(base64Data)) { + return null; + } + return new String(decrypt(base64PrivateKey, Base64Utils.decodeFromString(base64Data)), Charsets.UTF_8); + } + + /** + * base64 鏁版嵁瑙e瘑 + * + * @param base64PrivateKey base64 绉侀挜 + * @param base64Data base64鏁版嵁 + * @return 瑙e瘑鍚庣殑鏁版嵁 + */ + public static byte[] decryptFromBase64(String base64PrivateKey, byte[] base64Data) { + return decrypt(base64PrivateKey, Base64Utils.decode(base64Data)); + } + + /** + * base64 鏁版嵁瑙e瘑 + * + * @param base64PublicKey base64 鍏挜 + * @param base64Data base64鏁版嵁 + * @return 瑙e瘑鍚庣殑鏁版嵁 + */ + @Nullable + public static String decryptByPublicKeyFromBase64(String base64PublicKey, @Nullable String base64Data) { + if (StringUtil.isBlank(base64Data)) { + return null; + } + return new String(decryptByPublicKeyFromBase64(base64PublicKey, Base64Utils.decodeFromString(base64Data)), Charsets.UTF_8); + } + +} -- Gitblit v1.9.3