/** * 加载 PKCS#1 编码的 pem 格式私钥 * * @param privateKeyStr 私钥 * @throws Exception */ public void loadPrivateKeyPEMPKCS1(String privateKeyStr) throws Exception { try { byte[] e = Base64.decode(privateKeyStr); // 读取 PKCS#1的私钥 RSAPrivateKeyStructure asn1PrivateKey = new RSAPrivateKeyStructure( (ASN1Sequence) ASN1Sequence.fromByteArray(e)); RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(asn1PrivateKey.getModulus(), asn1PrivateKey.getPrivateExponent()); // 实例化KeyFactory对象,并指定 RSA 算法 KeyFactory keyFactory = KeyFactory.getInstance(SIGN_ALGORITHMS); // 获得 PrivateKey 对象 this.privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec); } catch (NoSuchAlgorithmException var6) { throw new Exception("无此算法"); } catch (InvalidKeySpecException var7) { throw new Exception("私钥非法"); } catch (IOException var8) { throw new Exception("私钥数据内容读取错误"); } catch (NullPointerException var9) { throw new Exception("私钥数据为空"); } }