Answer the question
In order to leave comments, you need to log in
openssl_sign not accepting pem file?
Gives a constant error:
PHP Warning: openssl_sign(): supplied key param cannot be coerced into a private key in
class SomeClass {
public function verify() {
$key = openssl_get_privatekey('./private_key.pem', 'strong_password');
$signature = null;
if (openssl_sign(base64_decode($data), $signature, $key, OPENSSL_ALGO_SHA256) === false) {
throw new \Exception(openssl_error_string());
}
}
}
openssl genrsa -aes256 -out private_key.pem 2048
openssl rsa -pubout -in private_key.pem -out public_key.pem
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,61078E5A491AAFBA18BBABE978C78BDF
BqJ06kTyUlX4DDzNmwEkn3Ujy8iQUD+j5mxI8kIvVkqU+gGMHWhCVn66ZtGFreFw
yZ7EMUHx9uQmZQ0sSsVxhmGljejAfmnWqdK3FS4HMZQAxDveRq123zw6I3pjXk6f
WCsEakTL1hcsM3IzUYRDOK4BFGM5uJZcLW4UNiTz6uto0yquReuxJV8S6zNEgxuk
O6hYQvAAr+OvprehGb0IuH1FfOrIF6dhqPyyHtVDPEpnKtLXvGptclk4PgAgOfdt
oMic5z4xO9Nmdfk3L4zalNZkVhfmTb//EEbIB2V5TBVuRoh2RuV2Y71Fqf0LwctD
9Ys+P0VQ/4e27QYlkXsxAdm/uVQncnHA3couU5vaUpP2NlIL+DbrqncMeXxTGuu3
tFP8FtBMrPvdgqIqFiUthUmemhN7uOatIQrbxXWenPnUT79bp9/9sDL+ovgN61cR
p50Fu6oJAnv3edgC4LY5btkQjszP2QtLZ0KYbB6b+6nwJpuH31QnIGNPURNqS+1d
irPqHpL5Nt5OILtVjiuampSk2o9xngIYFel5rihawEszKVUAtAT6k3IPOaNNpoia
9Djna+CdedIKbnWCRk6uJzmKowr5D6+Eh+79/z6sne7e2FD2S0IzcByYv/EHcJLR
L8Rb8so/0fBwZ5b8bQt/HidL+e54xwSbZzkVehQv0c56dPeIeUruMleZoqHbNm7u
5ly20hrPcedvHVYRMgihNWoJEb/L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1NbDjabtozEPshSW9cSAXV3RtKVnYrOfiCIWV
naZfdYV6NfI3WI/hL3flUz6Dd81e4TwrHCIji5e5n8EHO27g+xsq2iNP2T2ZZQWt
dFvGgqAOona4KE9tAwls9eJk01lw204brH9HfjSCYUvVweKQUoQ1CZjMxrj9xs3/
sIZcw3mdxIJVqpiHu9q00c7L1lWpJG0qGNgg6sIhBXSMA5KtABQo2YfDiRpLtf3A
DQIDAQAB
-----END PUBLIC KEY-----
Answer the question
In order to leave comments, you need to log in
Decided so.
I generate key files with the commands:
openssl genrsa -des3 -out private_rsa.pem 2048
openssl rsa -in private_rsa.pem -outform PEM -pubout -out public_rsa.pem
class SomeClass
{
#region PROPERTIES
protected static $public_key;
protected static $private_key;
const KEY_PASSPHRASE = 'key_passphrase';
#endregion
#region PUBLIC METHODS
public function __construct()
{
$keys_dir = dirname(__FILE__) . '/../keys/encrypt/';
$public_key_path = realpath($keys_dir . 'public_rsa.pem');
$private_key_path = realpath($keys_dir . 'private_rsa.pem');
self::$public_key = file_get_contents($public_key_path);
self::$private_key = file_get_contents($private_key_path);
}
public function encode($data)
{
if (is_numeric($data) === false || is_string($data) === false) {
$data = JSON::encode($data);
}
$encrypted = null;
if (openssl_public_encrypt($data, $encrypted, self::$public_key)) {
$encrypted = base64_encode($encrypted);
} else {
throw new Exception('Не удалось зашифровать данные. ' . openssl_error_string());
}
return $encrypted;
}
public function decode($data)
{
$decrypted = null;
openssl_private_decrypt(base64_decode($data), $decrypted, openssl_pkey_get_private(self::$private_key, self::KEY_PASSPHRASE));
return $decrypted;
}
#endregion
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question