jhprjv2/ruoyi-common/src/main/java/com/ruoyi/common/utils/AuthRsaUtils.java

185 lines
7.6 KiB
Java
Raw Normal View History

2024-01-13 19:53:12 +08:00
/*
* Copyright (c) 2016-2021. 西 All rights reserved.
*/
package com.ruoyi.common.utils;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* RSA
**/
public class AuthRsaUtils {
// Rsa 私钥
public static String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIcMS8aHgaLNdkABhwxcgsdAGu" +
"Z1X7ZKg4EjshM4376qh4w3mnjxnY4aJCdvQVZVhLpL8cE9pje7veZ16BbLTFATB0/K34DaUterGVUv7p1NfwUxSILKaIZHAyRp9N" +
"jLyFBVIY2fnT+1ZT9L7Ag6D1069AIxIcQw3VRt8HUNd0lBAgMBAAECgYBLPoOd2vRU5EuFgBRhw82t/L7ANxeb9spskpnucdrgXh" +
"1l97ket+iEO3Z3blqmIsHwFs5dT98j4Hv/QySMRrt+dkI2WKUY5ZNKVvpbbZJoNJXd1mF3SyfsRTM73l8fRgJLfhvE+ufV2+dBFM" +
"Nd8LsH8uTJo0aowHwgEXQ3ErZAAQJBAMh/FDQe/9Ku+1U67ABdifYl4CskfKNdd9srjYBBjw6NFmTZp3OEHxoWvTWDJMMC0uUBt5" +
"BV3Sx3+230dBeAQkECQQCsbvm+iQYMYpVkHe0RGU3nEckaehrqCJXlviH7c6kM4l/taV495mCxwvNwNUizV5uBeLEyKB2RPLAj7U" +
"b/10cBAkEAwIFrl6PQA60o+qupX6xwQ5wYQbQ1y/F5nEGUCnpn7hO/VbO52OsZpcYBg7jYejli3qkoY/hddU36ZpeZQ9tNQQJBAI" +
"JJP661fbpx6orBCdS3l+MVzyuQQzG91vTGGosRsxOnH/AUgz6mCT2HHcUUnZ/UfAzxkoFhSiXpAvXCXLkGggECQCWKhootNjqoz6" +
"txZheR5cpZTHNpAUwSSuqpFxK+rnv9PkEADZyaJKgsW4pHHtcUqUTBYQElDsSWOia86MubuyI=";
// Rsa 公钥
public static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHDEvGh4GizXZAAYcMXILHQBrmdV+2SoOBI7I" +
"TON++qoeMN5p48Z2OGiQnb0FWVYS6S/HBPaY3u73mdegWy0xQEwdPyt+A2lLXqxlVL+6dTX8FMUiCymiGRwMkafTYy8hQVSGNn50" +
"/tWU/S+wIOg9dOvQCMSHEMN1UbfB1DXdJQQIDAQAB";
/**
*
*
* @param text
* @return
*/
public static String decryptByPrivateKey(String text) throws Exception {
return decryptByPrivateKey(privateKey, text);
}
/**
*
*
* @param publicKeyString
* @param text
* @return
*/
public static String decryptByPublicKey(String publicKeyString, String text) throws Exception {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(Base64.decodeBase64(text));
return new String(result);
}
/**
*
*
* @param privateKeyString
* @param text
* @return
*/
public static String encryptByPrivateKey(String privateKeyString, String text) throws Exception {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(text.getBytes());
return Base64.encodeBase64String(result);
}
/**
*
*
* @param privateKeyString
* @param text
* @return
*/
public static String decryptByPrivateKey(String privateKeyString, String text) throws Exception {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(Base64.decodeBase64(text));
return new String(result);
}
/**
*
*
* @param publicKeyString
* @param text
* @return
*/
public static String encryptByPublicKey(String publicKeyString, String text) throws Exception {
X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(text.getBytes());
return Base64.encodeBase64String(result);
}
/**
* RSA
*
* @return
*/
public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
return new RsaKeyPair(publicKeyString, privateKeyString);
}
/**
* RSA
*/
public static class RsaKeyPair {
private final String publicKey;
private final String privateKey;
public RsaKeyPair(String publicKey, String privateKey) {
this.publicKey = publicKey;
this.privateKey = privateKey;
}
public String getPublicKey() {
return publicKey;
}
public String getPrivateKey() {
return privateKey;
}
}
public static void main(String[] args) {
// try {
// RsaKeyPair rsaKeyPair = generateKeyPair();
// System.out.println("私钥:" + rsaKeyPair.getPrivateKey());
// System.out.println("公钥:" + rsaKeyPair.getPublicKey());
// } catch (NoSuchAlgorithmException e) {
// throw new RuntimeException(e);
// }
try {
String timestamp = String.valueOf(System.currentTimeMillis());
String str = "oacsry" + timestamp;
System.out.println("明文信息:" + str);
// 公钥加密->私钥解密
String encryptByPublicKeyStr = encryptByPublicKey(publicKey, str);
System.out.println("公钥加密:" + encryptByPublicKeyStr);
String decryptByPrivateKeyStr = decryptByPrivateKey(privateKey, encryptByPublicKeyStr);
System.out.println("私钥解密:" + decryptByPrivateKeyStr);
System.out.println("***********************************");
// 私钥加密->公钥解密
String encryptByPrivateKeyStr = encryptByPrivateKey(privateKey, str);
System.out.println("私钥加密:" + encryptByPrivateKeyStr);
String decryptByPublicKeyStr = decryptByPublicKey(publicKey, encryptByPrivateKeyStr);
System.out.println("公钥解密:" + decryptByPublicKeyStr);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}