From e6dc29ec338003ce0e9d8fb2ef1c6b205ebc0556 Mon Sep 17 00:00:00 2001 From: Danny Du <50359598+DannyDu26@users.noreply.github.com> Date: Sat, 18 May 2024 14:10:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20cookie=E5=88=B7=E6=96=B0=E7=94=9F?= =?UTF-8?q?=E6=88=90CorrespondPath=E7=AE=97=E6=B3=95=E6=B7=BB=E5=8A=A0java?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=AE=9E=E7=8E=B0=20(#1017)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Duyulin --- docs/login/cookie_refresh.md | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/docs/login/cookie_refresh.md b/docs/login/cookie_refresh.md index f090f24..137e210 100644 --- a/docs/login/cookie_refresh.md +++ b/docs/login/cookie_refresh.md @@ -278,6 +278,68 @@ JNrRuoEUXpabUzGB8QIDAQAB 97759947aa357ed5d88cf9bf1172737570b7bba2d6788d39006f082b2b25ddf53b581f1f0c61ed8573317485ef525d2789faa25a277b4602a4b9cbf837681093a03e96cb9773a11df4bb1e20f1587180b3e958194de922d7dd94d0a2f0b9b0ef74e426e8041f99b99e7c02407ef4ab38040e61be81e4fdfbdb73461e3a2ad810 ``` +### Java +```Java +import javax.crypto.Cipher; +import javax.crypto.spec.OAEPParameterSpec; +import javax.crypto.spec.PSource; +import java.math.BigInteger; +import java.security.KeyFactory; +import java.security.PublicKey; +import java.security.spec.MGF1ParameterSpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; + +public class CookieRefresh { + private static final String PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n" + + "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLgd2OAkcGVtoE3ThUREbio0Eg\n" + + "Uc/prcajMKXvkCKFCWhJYJcLkcM2DKKcSeFpD/j6Boy538YXnR6VhcuUJOhH2x71\n" + + "nzPjfdTcqMz7djHum0qSZA0AyCBDABUqCrfNgCiJ00Ra7GmRj+YCK1NJEuewlb40\n" + + "JNrRuoEUXpabUzGB8QIDAQAB\n" + + "-----END PUBLIC KEY-----"; + + public static void main(String[] args) { + try { + String correspondPath = getCorrespondPath(String.format("refresh_%d", System.currentTimeMillis()), PUBLIC_KEY); + System.out.println(correspondPath); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static String getCorrespondPath(String plaintext, String publicKeyStr) throws Exception { + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + publicKeyStr = publicKeyStr + .replace("-----BEGIN PUBLIC KEY-----", "") + .replace("-----END PUBLIC KEY-----", "") + .replace("\n", "") + .trim(); + byte[] publicBytes = Base64.getDecoder().decode(publicKeyStr); + X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicBytes); + PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); + + String algorithm = "RSA/ECB/OAEPPadding"; + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.ENCRYPT_MODE, publicKey); + + // Encode the plaintext to bytes + byte[] plaintextBytes = plaintext.getBytes("UTF-8"); + + // Add OAEP padding to the plaintext bytes + OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT); + cipher.init(Cipher.ENCRYPT_MODE, publicKey, oaepParams); + // Encrypt the padded plaintext bytes + byte[] encryptedBytes = cipher.doFinal(plaintextBytes); + // Convert the encrypted bytes to a Base64-encoded string + return new BigInteger(1, encryptedBytes).toString(16); + } +} +``` + +``` +f87666152da692735123f4e49053e5a98c16854673b2e632f31a3ff0c029640772873661a9a8412db6be447a0bfa03a295d15548cbfd2bb35634e98ba5f25b1205519d6e6119b483f4c516c1e106d45b04ff98c73560949d379d3edaf3c0ecd10a1d46134fb9ca443122ab33c16d1dd48280496f949ed960a2fbcd65f10935e +``` + #### vercel云函数 ```bash