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/Decryption.java | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 103 insertions(+), 0 deletions(-)
diff --git a/src/main/java/com/zy/nc/util/Decryption.java b/src/main/java/com/zy/nc/util/Decryption.java
new file mode 100644
index 0000000..2046a10
--- /dev/null
+++ b/src/main/java/com/zy/nc/util/Decryption.java
@@ -0,0 +1,103 @@
+package com.zy.nc.util;
+
+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.OAEPParameterSpec;
+import javax.crypto.spec.PSource;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.security.InvalidKeyException;
+import java.security.Key;
+import java.security.NoSuchAlgorithmException;
+import java.security.spec.MGF1ParameterSpec;
+
+/**
+ * 锟斤拷锟斤拷锟斤拷
+ *
+ * @author liyang
+ */
+public class Decryption {
+
+ // RSA锟斤拷锟斤拷锟斤拷锟斤拷锟侥达拷小
+ private static final int MAX_DECRYPT_BLOCK = 256;
+
+ /**
+ * symDecrypt 锟皆称斤拷锟斤拷
+ *
+ * @param strkey 锟皆筹拷锟斤拷钥
+ * @param src 锟斤拷锟斤拷
+ * @return 原锟斤拷
+ * @throws IOException
+ * @throws Exception
+ */
+ public static String symDecrypt(String strkey, String src) throws Exception {
+
+ String target = null;
+ try {
+ Key key = KeysFactory.getSymKey(strkey);
+ // 锟斤拷锟斤拷
+ Cipher cipher = Cipher.getInstance(CipherConstant.AES_ALGORITHM);
+ IvParameterSpec iv = new IvParameterSpec(strkey.substring(0, 16).getBytes());
+ cipher.init(Cipher.DECRYPT_MODE, key, iv);
+ byte[] decodeResult = cipher.doFinal(Base64Util.decryptBASE64(src));
+ target = new String(decodeResult, StandardCharsets.UTF_8);
+
+ } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException |
+ InvalidKeyException e) {
+ throw new Exception("锟斤拷锟斤拷失锟斤拷" + e.getMessage());
+ }
+
+ return target;
+ }
+
+ /**
+ * priDecrypt 私钥锟斤拷锟斤拷
+ *
+ * @param priKey 私钥
+ * @param src 锟斤拷锟斤拷
+ * @return 原锟斤拷
+ * @throws IOException
+ * @throws Exception
+ */
+ public static String priDecrypt(String priKey, String src) throws Exception {
+ String target = null;
+ ByteArrayOutputStream out = null;
+ try {
+ Key key = KeysFactory.getPrivateKey(priKey);
+ // 锟斤拷锟斤拷
+ Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
+ cipher.init(Cipher.DECRYPT_MODE, key, new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), PSource.PSpecified.DEFAULT));
+ byte[] data = Base64Util.decryptBASE64(src);
+ int inputLen = data.length;
+ out = new ByteArrayOutputStream();
+ int offSet = 0;
+ byte[] cache;
+ int i = 0;
+ // 锟斤拷锟斤拷锟捷分段斤拷锟斤拷
+ while (inputLen - offSet > 0) {
+ if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
+ cache = cipher.doFinal(data, offSet, MAX_DECRYPT_BLOCK);
+ } else {
+ cache = cipher.doFinal(data, offSet, inputLen - offSet);
+ }
+ out.write(cache, 0, cache.length);
+ i++;
+ offSet = i * MAX_DECRYPT_BLOCK;
+ }
+ target = new String(out.toByteArray());
+
+ } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException |
+ BadPaddingException e) {
+ throw new Exception("锟斤拷锟斤拷失锟斤拷" + e.getMessage());
+ } finally {
+ if (out != null) {
+ out.close();
+ }
+ }
+ return target;
+ }
+}
--
Gitblit v1.9.1