简介
资料列表
入门示例
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException,
InvalidKeyException, SignatureException, UnsupportedEncodingException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(2048, random);
KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
String privStr = Base64.getEncoder().encodeToString(priv.getEncoded());
String pubStr = Base64.getEncoder().encodeToString(pub.getEncoded());
PrivateKey privKey = getPrivateKey(privStr);
PublicKey pubKey = getPublicKey(pubStr);
String plaintext = "私钥签名测试";
Signature rsaPrivSig = Signature.getInstance("SHA1withRSA");
rsaPrivSig.initSign(privKey);
rsaPrivSig.update(plaintext.getBytes("UTF-8"));
byte[] privSign = rsaPrivSig.sign();
String privSignStr = Base64.getEncoder().encodeToString(privSign);
Signature rsaPubSig = Signature.getInstance("SHA1withRSA");
rsaPubSig.initVerify(pubKey);
rsaPubSig.update(plaintext.getBytes("UTF-8"));
boolean verifies = rsaPubSig.verify(Base64.getDecoder().decode(privSignStr));
System.out.println("signature verifies: " + verifies);
}
简单工具类
public class RsaUtil {
public static RsaPair generate() throws NoSuchAlgorithmException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(2048, random);
KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
String privStr = Base64.getEncoder().encodeToString(priv.getEncoded());
String pubStr = Base64.getEncoder().encodeToString(pub.getEncoded());
return new RsaPair(privStr, pubStr);
}
public static String sign(String privStr, String plaintext) throws InvalidKeySpecException,
NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, SignatureException {
PrivateKey privKey = getPrivateKey(privStr);
Signature rsaPrivSig = Signature.getInstance("SHA1withRSA");
rsaPrivSig.initSign(privKey);
rsaPrivSig.update(plaintext.getBytes("UTF-8"));
byte[] privSign = rsaPrivSig.sign();
return Base64.getEncoder().encodeToString(privSign);
}
public static boolean verify(String pubStr, String privSignStr, String plaintext) throws InvalidKeySpecException,
NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, SignatureException {
PublicKey pubKey = getPublicKey(pubStr);
Signature rsaPubSig = Signature.getInstance("SHA1withRSA");
rsaPubSig.initVerify(pubKey);
rsaPubSig.update(plaintext.getBytes("UTF-8"));
return rsaPubSig.verify(Base64.getDecoder().decode(privSignStr));
}
private static PrivateKey getPrivateKey(String key) throws NoSuchAlgorithmException, InvalidKeySpecException {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encKey = Base64.getDecoder().decode((key.getBytes()));
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encKey);
return keyFactory.generatePrivate(keySpec);
}
private static PublicKey getPublicKey(String key) throws NoSuchAlgorithmException, InvalidKeySpecException {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encKey = Base64.getDecoder().decode((key.getBytes()));
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encKey);
return keyFactory.generatePublic(keySpec);
}
}
public class RsaPair {
private String privSt;
private String pubStr;
public RsaPair(String privSt, String pubStr) {
this.privSt = privSt;
this.pubStr = pubStr;
}
}