ab
2024-11-05 bead00668eebce8d39d027515d564376de2f5978
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.nova.sankuai.domain.api.yangqianguan;
 
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
 
/**
 *
 * 生成密钥对: ssh-keygen -m PEM -t rsa -b 4096 -f id_rsa
 * 进行PKCS#8编码:openssl pkcs8 -topk8 -inform PEM -in id_rsa -outform pem -nocrypt -out private_key.pem
 * 根据私钥生成公钥:openssl rsa -in private_key.pem -pubout -out public_key.pem
 *
 * 得到私钥文件  private_key.pem
 * 得到公钥文件  public_key.pem
 *
 */
public class UnifiedRsaUtils {
 
    private static final String RSA_ALGORITHM = "RSA";
 
    private static final String DEFAULT_ENCODING = "UTF-8";
 
    private static final String DEFAULT_SIGN_ALGORITHM = "SHA256withRSA";
 
    public static boolean verifyWithPubKey(String plainText, String signature, String publicKeyStr) {
        try {
            Signature publicSignature = Signature.getInstance(DEFAULT_SIGN_ALGORITHM);
            publicSignature.initVerify(loadRsaPublicKey(publicKeyStr));
            publicSignature.update(plainText.getBytes(DEFAULT_ENCODING));
            byte[] signatureBytes = Base64.getDecoder().decode(signature);
            return publicSignature.verify(signatureBytes);
        } catch (Exception e) {
            throw new RuntimeException("rsa verify error", e);
        }
    }
 
    public static String signWithPriKey(String plainText, String privateKeyStr) {
 
        try {
            Signature privateSignature = Signature.getInstance(DEFAULT_SIGN_ALGORITHM);
            privateSignature.initSign(loadRsaPrivateKey(privateKeyStr));
            privateSignature.update(plainText.getBytes(DEFAULT_ENCODING));
            byte[] signature = privateSignature.sign();
            return Base64.getEncoder().encodeToString(signature);
 
        } catch (Exception e) {
            throw new RuntimeException("rsa sign error", e);
        }
    }
 
    private static RSAPrivateKey loadRsaPrivateKey(String priKeyText) throws NoSuchAlgorithmException, InvalidKeySpecException {
        byte[] keyBytes = Base64.getDecoder().decode(priKeyText);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
        return (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
    }
 
 
    private static RSAPublicKey loadRsaPublicKey(String pubKeyText) throws InvalidKeySpecException, NoSuchAlgorithmException {
        byte[] keyBytes = Base64.getMimeDecoder().decode(pubKeyText);
        KeyFactory kf = KeyFactory.getInstance(RSA_ALGORITHM);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        return (RSAPublicKey) kf.generatePublic(keySpec);
    }
 
}