我从客户那里得到了一些文档,其中有一个RSA专用密钥。
我无法找到一种方法来加密带有该密钥的文本,而无需输入另一个密码?
它们包括Java中的“演示示例”,以说明其完成方式。
publicstaticStringsign(StringstringA, StringprivateKey, Stringinput_charset)
{
try {
PKCS8EncodedKeySpecpriPKCS8 =
newPKCS8EncodedKeySpec(Base64.decode(privateKey));
KeyFactorykeyf = KeyFactory.getInstance("RSA");
PrivateKeypriKey = keyf.generatePrivate(priPKCS8);
java.security.Signaturesignature =
java.security.Signature.getInstance("SHA1WithRSA");
signature.initSign(priKey);
signature.update(stringA.getBytes("UTF-8"));
byte[] signed = signature.sign();
returnBase64.encode(signed);
} catch (Exceptione) {
e.printStackTrace();
}
returnnull;
}
当我尝试使用PHP时,它告诉我密钥是错误的。所以我的答案是,如何使用从文档中获得的密钥来加密文本?没有密码作为“私钥”?
function encryptData($raw)
{
global $privateKey;
$privateKey = openssl_pkey_get_private($privateKey);
if (!$privateKey) {
throw new RuntimeException('Invalid private key or passphrase');
}
// Encrypt digest using the key
$encrypted = "";
openssl_private_encrypt($raw, $encrypted, $privateKey, OPENSSL_PKCS1_PADDING);
return $encrypted;
}
encryptData("Hello world!");
我的私钥是;
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEA
AoGBAIpUpl1YsfN5V2iSLDdih1ymU4W0zC05oUYftsT4CE
FFEgPJdqxTqFoaza01+OLbAzewzRvcTjmyFscahx6X3K4oh
1aJ14jrTXCxqc3E4sfPI/QBxioLVusm+jznnBP5h0iiX1gtdkG
Kj1VmrkkX7brMUDH7y6jqod/TqnHMPVAFAgMBAAECg
YAfnmG4jIGAaYf1OxQoRLk0EECfG0hgPgADC1S3iQo6AR
OxEeeyVBzzG5VPtjqBgBYoMJ1xdNXckjxqnkGsWIH0Zrjal
N7NApDNiq76lINk/xXCRZWLjy7BSa/z6rc8mtYbYf57yxbm
VwznO8f2VlWkjQ8fJ8c5+xxqBlvm9hyoAQJBAMvrncsVSt13
a6IsPk4+2IGO73lgXgtBq3mImIZfK63QniAatrDxTe4i6R4l5
DEPUZ5ETObTqeRajjQ1e/boC4UCQQCtqMF3Kh04DNdF
AXgpf79pA81ymDmzwZIcBpD9LTJcNbSORGUq/aWmWSz
naPtRcF+zSAvKjgLzB3QHsdwO1BqBAkAi2vkzt0X+qOTq6
WkU8sULECV6tsdbeHQY5/I6NZYsdLxtd84S2aop6r1Dk
D86cNnrAEBC3jkoVsdvvuYJAXXXkBMlDJcPjIMWX1EbNV
0V2UtBgQ/GGm5nw6zVSHyw0+F4hjw87A5OQknRgPLOD
w7OXa6NHcep2kdipsdVzbwgqX4rilqqFTeJhy/tDiFJKhi4xOm
DQRIdVhhhgZs81