RSA加密、解密、加签、验签
我这里的是RSA第二版的,即密钥长度2048,网上很多都是1024。加签和验签使用的是SHA256WithRSA。
/**
* RSA2加解密、加签、验签。
* <p>
* 密钥格式Java版本的请使用PKCS8,其他语言请使用PKCS1。密钥长度2048。
*
* @author xanthuim
*/
public class Rsa2Utils {
public static final String CHARSET = "UTF-8";
public static final String RSA_ALGORITHM = "RSA";
public static final String RSA_ALGORITHM_SIGN = "SHA256WithRSA";
//private ;
//private RSAPublicKey rsaPublicKey;
private static RSAPublicKey rsaPublicKey(String publicKey) {
//通过PKCS#8编码的Key指令获得私钥对象
try {
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
//通过X509编码的Key指令获得公钥对象
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
return (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
还没有评论,来说两句吧...