From ffb382ba34aca2dce08ab9e4ef09adc946cf23c8 Mon Sep 17 00:00:00 2001
From: ZY <zc857179121@qq.com>
Date: 星期日, 27 四月 2025 17:26:31 +0800
Subject: [PATCH] 双数据源
---
src/main/java/com/zy/nc/util/KeysFactory.java | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 103 insertions(+), 0 deletions(-)
diff --git a/src/main/java/com/zy/nc/util/KeysFactory.java b/src/main/java/com/zy/nc/util/KeysFactory.java
new file mode 100644
index 0000000..67c4d66
--- /dev/null
+++ b/src/main/java/com/zy/nc/util/KeysFactory.java
@@ -0,0 +1,103 @@
+package com.zy.nc.util;
+
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+import java.security.Key;
+import java.security.KeyFactory;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.X509EncodedKeySpec;
+
+/**
+ * Key锟斤拷锟斤拷锟斤拷
+ *
+ * @author liyang
+ */
+public class KeysFactory {
+
+ /**
+ * buildAsymKey 锟斤拷锟斤拷一锟斤拷嵌猿锟斤拷锟皆�
+ *
+ * @return KeyPair key锟斤拷PublicKey锟斤拷PrivateKey
+ * @throws NoSuchAlgorithmException
+ */
+ public static KeyPairs buildAsymKey() throws Exception {
+
+ /* 锟斤拷始锟斤拷锟斤拷钥锟斤拷锟斤拷锟斤拷 */
+ KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(CipherConstant.RSA);
+ keyPairGenerator.initialize(2048, SecureRandomProxy.getRandomInstance());
+
+ /* 锟斤拷锟斤拷锟斤拷钥 */
+ return new KeyPairs(keyPairGenerator.generateKeyPair());
+ }
+
+ /**
+ * buildAsymKey 锟斤拷锟斤拷一锟斤拷锟皆筹拷锟斤拷钥
+ *
+ * @return 锟皆筹拷锟斤拷钥
+ * @throws NoSuchAlgorithmException
+ * @throws Exception
+ */
+ public static String buildSymKey() throws Exception {
+ // 锟斤拷锟斤拷Key
+ KeyGenerator keyGenerator = KeyGenerator.getInstance(CipherConstant.AES);
+
+ keyGenerator.init(256, SecureRandomProxy.getRandomInstance());
+ // 使锟斤拷锟斤拷锟斤拷锟斤拷锟街筹拷始锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟截讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷钥锟斤拷锟斤拷锟斤拷锟斤拷锟杰猴拷锟斤拷锟斤拷锟斤拷锟轿ㄒ伙拷潭锟斤拷摹锟�
+ SecretKey secretKey = keyGenerator.generateKey();
+
+ return Base64Util.encryptBASE64(secretKey.getEncoded());
+
+ }
+
+ public static Key getPublicKey(String pubKey) throws Exception {
+ Key key = null;
+
+ try {
+ byte[] keyBytes = Base64Util.decryptBASE64(pubKey);
+ KeyFactory keyFactory = KeyFactory.getInstance(CipherConstant.RSA);
+
+ X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
+ key = keyFactory.generatePublic(x509KeySpec);
+
+ } catch (Exception e) {
+ throw new Exception("锟斤拷效锟斤拷锟斤拷钥 " + e.getMessage());
+ }
+
+ return key;
+ }
+
+ public static Key getPrivateKey(String priKey) throws Exception {
+ Key key = null;
+
+ try {
+ byte[] keyBytes = Base64Util.decryptBASE64(priKey);
+
+ KeyFactory keyFactory = KeyFactory.getInstance(CipherConstant.RSA);
+
+ PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
+ key = keyFactory.generatePrivate(pkcs8KeySpec);
+
+ } catch (Exception e) {
+ throw new Exception("锟斤拷效锟斤拷钥 " + e.getMessage());
+ }
+
+ return key;
+ }
+
+ public static Key getSymKey(String symKey) throws Exception {
+ Key key = null;
+
+ try {
+ byte[] keyBytes = Base64Util.decryptBASE64(symKey);
+ // Key转锟斤拷
+ key = new SecretKeySpec(keyBytes, CipherConstant.AES);
+ } catch (Exception e) {
+ throw new Exception("锟斤拷效锟斤拷钥 " + e.getMessage());
+ }
+
+ return key;
+ }
+}
--
Gitblit v1.9.1