RSA非对称加密提示

it2024-08-18  49

使用私钥解密时,报错:algid parse error, not a sequence 提示:私钥需要先转PKCS8格式

/** * * @param str 需解密的字符串 * @param privateKey 私钥 * @return * @throws Exception */ public static String decrypt(String str, String privateKey) throws Exception{ //64位解码加密后的字符串 byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8")); //base64编码的私钥 byte[] decoded = Base64.decodeBase64(privateKey); RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); //RSA解密 Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, priKey); String outStr = new String(cipher.doFinal(inputByte)); return outStr; }
最新回复(0)