This commit is contained in:
@jyuhsin
2025-10-29 10:47:50 +08:00
parent a3fd6a1c26
commit 6e47110458
9 changed files with 258 additions and 15 deletions
@@ -67,4 +67,9 @@ public class RedisConstant {
* 数据接口锁前缀
*/
public static final String DATE_CENTER_PREFIX = PLATFORM_REDIS_PREFIX + "datacenter:token:";
/**
* 数据接口锁前缀
*/
public static final String RSA_KEY_PREFIX = PLATFORM_REDIS_PREFIX + "rsa:private:";
}
@@ -0,0 +1,42 @@
package com.budwk.app.base.utils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Base64;
/**
* @ClassName RsaUtils
* @Author JyuHsin
* @Date 2025/10/29 9:56
* @Version 1.0
* @Description TODO
*/
public class RsaUtils {
static {
Security.addProvider(new BouncyCastleProvider());
}
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
keyGen.initialize(2048);
return keyGen.generateKeyPair();
}
public static String getPublicKeyBase64(PublicKey publicKey) {
return Base64.getEncoder().encodeToString(publicKey.getEncoded());
}
public static String getPrivateKeyBase64(PrivateKey privateKey) {
return Base64.getEncoder().encodeToString(privateKey.getEncoded());
}
public static String decrypt(String encryptedBase64, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encryptedBase64));
return new String(decrypted, StandardCharsets.UTF_8);
}
}