最近不知道在忙什麼,也不知道寫點什麼,直接丟個工具類吧~~~
/**
* 獲取X509證書的祕鑰算法和祕鑰長度
*
* @param certificate
* @return
*/
public static CertInfo getKeyAlgAndLength(X509Certificate certificate) throws Exception {
CertInfo certInfo = new CertInfo();
//可以得到EC RSA
String keyAlgName = certificate.getPublicKey().getAlgorithm();
//不區分大小寫比較
if ("EC".equalsIgnoreCase(keyAlgName)) {
keyAlgName = "SM2";
certInfo.setKeyLength(256);
certInfo.setKeyAlg(keyAlgName);
return certInfo;
} else if ("RSA".equalsIgnoreCase(keyAlgName)) {
RSAPublicKey rsaPublicKey = (RSAPublicKey) certificate.getPublicKey();
Integer length = rsaPublicKey.getModulus().bitLength();
certInfo.setKeyAlg(keyAlgName);
certInfo.setKeyLength(length);
} else {
throw new Exception();
}
return certInfo;
}
/**
* 接收證書祕鑰算法和祕鑰長度
*
* @author wly
*/
public class CertInfo {
private String keyAlg;
private int keyLength;
public CertInfo() {
}
//get、set方法
}
順便區分一下簽名算法和祕鑰算法值, 僅針對自己平時SM2和RSA的證書:
//獲取祕鑰算法 可以得到 EC RSA
String keyAlgName = cert.getPublicKey().getAlgorithm();
//獲取證書籤名算法 值爲 SM2WithSM3、SHA1WithRSA、SHA256WithRSA等
String signAlgName = cert.getSigAlgName()
預祝大家國慶節Happy!本人已經打算回家找媽媽啦~