$pemPrivateKey = '-----BEGIN PRIVATE KEY-----' . PHP_EOL . chunk_split($key, 64, PHP_EOL) . '-----END PRIVATE KEY-----' . PHP_EOL;
验证:dd(openssl_pkey_get_private($pemPrivateKey);
打印结果为:OpenSSL key resource @128 (此字符串为rsa秘钥)
打印结果为:falese (代表此字符串不是rsa秘钥)
java语言rsa解码示例:
decoded为加密字符串 RSAPrivateKey为私钥
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, priKey); outStr = new String(cipher.doFinal(inputByte));
PHP语言rsa解码示例:
$base为加密字符串 $pemPrivateKey为私钥 $decryptedData为解密结果 openssl_private_decrypt($base, $decryptedData, $pemPrivateKey)