我有公钥字符串:
----- BEGIN PUBLIC KEY ----- MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAvL + UegLeWJ1KLh6WEfbW KGCA + vj4G / k6iIz9OWCb9t3maokH +盎司/ bNB8PGlwmH2aCBmqXPv9p3hexcujUsNC jFahCAz84tT7M4rR2GGwVWyY9PJ98rZbbvD9PLG380cqZs + vlwYT16bv5t + braiJ WhrAgwKF0eZyID6DzRnDCG7HbKr6sL1h7QD1WHBljplV + TinIeWoyBIWPJdqEAUs js1wXWkFPIb5x0W7kAzWc0 + oH3gDD3kGv + T / p2DsbUuOYYSZgZB8Lx1AKiWVRERt 90tWH9uytulSxvwoONBriasH7VlSXyi5ve6AA4 + FG / HExiPNN5Lh34kNP6l5CTc + yaCDHewYQeqDdgP + GurgdV9ZCM4P7c6Ky7GZBs8sAAAXevch8ggf9XGjEsLl ++ C7 G5E4FO6kNmloL3nogRLJ8P8w / s53Y0g4ih2hdJFfqJ3l / fIolvldR17nA3oZBHTo OuOSR / JK1KWvkl / C9I1lHO70zrgvr7wtpW2YbdBHvvTu5aTK8oIMNoZ8lhWdKFYG aiQWvfsM / IPXbFKQbDQachn5ZHYLc4wXi1uBHYnPCjZM3Fq6wc1oSnRZ77CI8crf To7TJFfXO20VtSdwUq0SCrQ1wxttK8uH22c / YUXPld + K6fW13tspWz653gnw5FPd 8bFQQByVkrcihbyuXWjUSMkCAwEAAQ == ----- END PUBLIC KEY -----
并希望用它来加密邮件,但是当我运行下面的代码时,它会给我以下错误:
警告:openssl_public_encrypt():key参数不是有效的公钥i ...
<?php
$unformatted_pub= "-----BEGIN PUBLIC KEY----- MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAvL+UegLeWJ1KLh6WEfbW kGcA+vj4G/k6iIz9OWCb9t3maokH+Oz/bNB8PGlwmH2aCBmqXPv9p3hexcujUsNC jFahCAz84tT7M4rR2GGwVWyY9PJ98rZbbvD9PLG380cqZs+vlwYT16bv5t+braiJ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+8Npm/OI1hzfhlRkQvdOxN/oPo8w3G26Cq6nEdf0TnIE/f3 3jfMhqakzZsayom9rOb56veo7EYKEWm3hs//LRcSmIZ0I1EtPxBEnuZOcUaeQq1W mIBQKBy2kCdNkb/iCPrNNtK4sz2tOB/FzC//+oUgKE+dTgPGz1fOqWtodAlHrjWx /fkFAoIBADk3d8FxNXgpv8imDzUTg3ONN9sbzqHjb9/6HKAYpN4Ez535Kr9rIJ1U D+z4VC7X5Gd5GrikoqsikotNEMJmYR/5Z0ojbZPdcaLWsIWXiAQksQOCodloQ+6+ QkJ2U8Be9y41F2psiq4s0E326rufobj3NSyk+5LYizrjdB/S7CeuehQnq7nA99A6 7uvmD4Kmh+EfDggS7sKQoIS9nLzraGpdjokFk56vtKH0TbUlrgoMTdUuNUBRSD8R O0KT1VLB4wrlMAuVQ+wKs/dOFRwHIMNl2EaBoXCkYE4sjl9776r1kIUJ/ZDt7+FZ H6CKoxjdh97Zzxo1Up2PTOOpcXfE6GA= -----END PRIVATE KEY-----";
$data = 'plaintext data goes here';
// Encrypt the data to $encrypted using the public key
openssl_public_encrypt($data, $encrypted, $unformatted_pub);
// Decrypt the data using the private key and store the results in $decrypted
openssl_private_decrypt($encrypted, $decrypted, $unformatted_priv);
echo $decrypted;
//echo $pubKey;
?>
我不确定如何正确编码公钥,我们非常感谢所有的帮助。
答案 0 :(得分:3)
以下代码生成密钥,一切都按预期工作,但您的硬编码是一个公钥,它不会对生成的私钥起作用:
<?php
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create the private and public key
$res = openssl_pkey_new($config);
// Extract the private key from $res to $privKey
openssl_pkey_export($res, $privKey);
// Extract the public key from $res to $pubKey
$pubKey = openssl_pkey_get_details($res);
$pubKey = $pubKey["key"];
$data = 'plaintext data goes here';
// Encrypt the data to $encrypted using the public key
openssl_public_encrypt($data, $encrypted, $pubKey);
// Decrypt the data using the private key and store the results in $decrypted
openssl_private_decrypt($encrypted, $decrypted, $privKey);
echo $decrypted;
?>
相反,您应该执行以下操作 test ,生成密钥然后硬编码:
<?php
$pubKey = '
-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAvucXbUOp6VBweTvmJQeS
S//VlWbqDmoxLB9Ru5OWWqU+hDbYNcusc92W08rqrlJkaQRyhUn30vF7nqrQKBPf
hj9sX5s5AqPkGH7Ygvl7zjNTRY4PTGnaxgJs5PJgiGqusdmT6OMv6IwBUuk463ao
YzYIyLOmzmi/nKi0DkAw5B1NHIXtb9XMrb8vBskit+gbjKuXet2kXslKkNZncUUS
/AbchHZDaw192n0s6yhOJCe4nzvrPoO/1BnqLjaQbkMwoKXhwoAxByyiUVNpVtN9
cykZl9TM2mjYPFDXnNBkiV5HURJP64EVoXu69qPCORtOxvTQ3Jhz7Z2brtEQFtxg
vfQnm4FuiYnVXSwLte3IC+cEDbn3++x3CuHPIe4ioQwR/VxnzY7uvZASh4qPG4Re
hWbQgU+i5MIqwnTW/IA8XreD/kzgmYk+qXWHUxAZpTHfHxUGUGZZDhYGPpdhYi6L
Cw45FZXDjvLRB3bYOUw9M8s4V7uMEFSg5cZdr7smCzhsF3WNysumlCEK/FEHmszW
yDnYqZMwbgkEb4m6hxsxzEpo41LrxSnAVO3OWLsS9Llf3J63xN8IaW7e3vpif1+n
I4TU2efSyy+v9AHuHGIOyIOI7fQjWNYORtsQGNp5OZaekvStHyu5qDPrISiY90Ty
qihrmpm2PtYASEc1/MDlFY8CAwEAAQ==
-----END PUBLIC KEY-----';
$privKey = '-----BEGIN PRIVATE KEY-----
MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQC+5xdtQ6npUHB5
O+YlB5JL/9WVZuoOajEsH1G7k5ZapT6ENtg1y6xz3ZbTyuquUmRpBHKFSffS8Xue
qtAoE9+GP2xfmzkCo+QYftiC+XvOM1NFjg9MadrGAmzk8mCIaq6x2ZPo4y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BeLSiilSpAY3z2ZTWJfB6g5JOo/h5kIbS1qKRCf1VYCtCqSQf7M7+421M3VKYdt9vBa2/53vzgkCrKEw4kPiBUFzuAPNcwa+cc6j3IsyRzZ6ownabBbYkrPHxzAlNDKABPxbU8v+8yJXtE1kEwAbr62AmJVLXuvdaX9St8gTYfCMxnP9VAWZ8Y4K5dwuG/6wTz/fDFTK2MgRd6WErAuj5mdLjMa8Yc5/Wt0Yut0grtdarNq3m5Z0Quhir99L0xHAmuKtNztPJC1wnEbHFfZ4kRjxG53SE19baeBpLNC0erfoWF3aQt1aJHqw2xbLdV8CnKMaVAw1qjdRu4WFml/EVokgTHDqm4SGlgwdhXtWY/T+p1zhu7gOl2PuArPyPGpYNh57Ib7TlwjvaHOO73O5xQqm+YUElh7MjwvH45jTekbWZHjc2fyN7pXtdnus+WOjKhopL09LPy4M17DARUSedypSiZL3gTiIz6qqJ31XQIsEaKWOMosYOjY=';
// Decrypt the data using the private key and store the results in $decrypted
openssl_private_decrypt(base64_decode($encrypted), $decrypted, $privKey);
echo $decrypted;
?>
将输出plaintext data goes here
。