How to encrypt data using RSA public key in IOS - php

I am using phpseclib lib for rsa encryption
require_once = "./Crypt/RSA.php"
$rsa = new Crypt_RSA();
$rsa->setHash('sha1');
$rsa->setMGFHash('sha1');
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);
$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
// Code to Generate the public key and private key
$keys = $rsa->createKey(1024);
extract($keys);
// Base 64 encode Public and Private key
$publicKeyBase = base64_encode( $publickey );
$privateKeyBase = base64_encode( $privatekey );
// Private key
var_dump( $privateKeyBase );
echo "---------------------------------------------------------</br>";
// Public Key
var_dump($publicKeyBase);
Generates public key like follows in bae64_encode format:
LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0NCk1JSUNJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBZzhBTUlJQ0NnS0NBZ0VBbndac2s5QXpSUndEWXN5dksxLzYNCk11TTRVMlpkL3dIV1RaY01uVDR2ODdyaGVuMTZZM2szeDZ0OFJIWTBUY3hRZHJrQjIvVTZpQUdyRFMwaWppTFENClZLWmJtU1cvZ05xdk5UR0hUUlR3T0tlaVhadHNOQU01U1V2YWtVcytrMzBhd0t5U2NIV2xjL0tvMWJpZlBpYmUNCnZyM2xBTmhyMlFhT3kzY2dvbWszR040M1dpN01MTXFTMVY3M1Q4RUxYeGtDYkErRDA0WWgrZjFURVNRQkhwakoNCkIwYklBS0JKMkFYTE9Ieis1UUdFaXlGVGpMZDZsT3NpaW9LTThqbXk1R0JIdlladld0akt6RG9uVGNSQ3FpWisNCm1HYTNka1JyUkVtRDgvOXNRN0EzZldqeStBWWl1czdsRTRFZURCMWZzTFp2TVhKTzdFMGdrbk1kVGpsT3M0MU4NClhCNVVLWUZiZlJ4UWZLZlV0Y1NlTHd1ekxQMXdYWEVBRkdHcGJEYjVmZnUzR0hycUFOQWI1OUh3ZzgweT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Y4N1pKbWRmUDEyU2E3aVN1MHJndTFsNEsvejlaWGN0S3BWdi8vZ1BiYnVWdm1RZUFiSlVsOGllDQpuSkdrZ282VG5HbGdoN1NoNlRYSTlyN1QyV2NPMFFwTXFrNUJlZjlmdGZDM1Jad2lCU2VJZ1hQa1JsbVVMNUNsDQpEREFoQW9JQkFRQ3hkN2dSL2srZGJIN2ZkLzlFdVdLa0kvQ3V2dlJwVGlXaXVkcXp1N2ZKbTN0bkpzUDBkMFU0DQoxSzc4MU1tLzdlT0JqamxwZGxVWXkzOXhPdzlhc3h6R1Z5cURNSWVOUzNpcHlrczRmV0FOMGZpUFZUUmlFMVhVDQpBSFBwQkorSE1XaUIxdEVGQ1lPYXpRMEE4ZHlxNm5VWFVlMGJNOWhxWWt1cnUvblBIRzdndklIeVlrT3hrRzViDQprOFZHd2oxbHpQZVlxSXowdS80dGlBcy80bGc3MEtxSG9MMmwwUFVpZlZEY0JERmVPSEpYTms5ZWI2eGozcnhnDQpYc3B2L0N5S2dTc0pqVWpmRDl1WWFuZTdjL0dqWkM4cjI1UHozQjBzY3llWGlQYnNnajNhNkE4eGp5NGNCWG9hDQpZTzdhK1RqZGlSVVBXa2gwcWRnN0FQZGVCRDc3RVB5SQ0KLS0tLS1FTkQgUFJJVkFURSBLRVktLS0tLQ==

Related

How to convert PEM format RSA key into XML format using PHP

I'm trying to convert key pair from PEM format :
-----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----
Into XML format :
<RSAKeyValue>
<Exponent> </Exponent>
<Modulus> </Modulus>
</RSAKeyValue>
Is it possible using only openssl as I generate the keys through it ?
nb : my keys are stored into $privKey and $pubKey variable for test purpose, so I want to be able to $echo the XML format key and not store it into a file for the moment.
nb' : I have tried using phpseclib with an exemple found here but it gives me this error "Uncaught Error: Class "BaseController" not found in ..."
Thanks for your help
Here is the PHP code :
<?php
$config = array
(
'config' => 'C:\xampp\htdocs\crypto\openssl.cnf',
'default_md' => 'sha512',
'private_key_bits' => 4096,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
);
$keypair = openssl_pkey_new($config);
openssl_pkey_export($keypair, $privKey, null, $config);
$publickey = openssl_pkey_get_details($keypair);
$pubKey = $publickey['key'];
use phpseclib3\Crypt\RSA;
echo $pubKey->toString("XML");
echo "$privKey";
?>
The conversion of a PEM encoded key in X.509/SPKI format to XML format can be done with phpseclib as follows:
use phpseclib3\Crypt\PublicKeyLoader;
$x509pem = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAunF5aDa6HCfLMMI/MZLT
5hDk304CU+ypFMFiBjowQdUMQKYHZ+fklB7GpLxCatxYJ/hZ7rjfHH3Klq20/Y1E
bYDRopyTSfkrTzPzwsX4Ur/l25CtdQldhHCTMgwf/Ev/buBNobfzdZE+Dhdv5lQw
KtjI43lDKvAi5kEet2TFwfJcJrBiRJeEcLfVgWTXGRQn7gngWKykUu5rS83eAU1x
H9FLojQfyia89/EykiOO7/3UWwd+MATZ9HLjSx2/Lf3g2jr81eifEmYDlri/OZp4
OhZu+0Bo1LXloCTe+vmIQ2YCX7EatUOuyQMt2Vwx4uV+d/A3DP6PtMGBKpF8St4i
GwIDAQAB
-----END PUBLIC KEY-----';
$publicKey = PublicKeyLoader::load($x509pem); // import public PEM key
$xmlFormattedKey = $publicKey->toString("XML"); // export public XML key
print($xmlFormattedKey);
The output is:
<RSAKeyValue>
<Modulus>unF5aDa6HCfLMMI/MZLT5hDk304CU+ypFMFiBjowQdUMQKYHZ+fklB7GpLxCatxYJ/hZ7rjfHH3Klq20/Y1EbYDRopyTSfkrTzPzwsX4Ur/l25CtdQldhHCTMgwf/Ev/buBNobfzdZE+Dhdv5lQwKtjI43lDKvAi5kEet2TFwfJcJrBiRJeEcLfVgWTXGRQn7gngWKykUu5rS83eAU1xH9FLojQfyia89/EykiOO7/3UWwd+MATZ9HLjSx2/Lf3g2jr81eifEmYDlri/OZp4OhZu+0Bo1LXloCTe+vmIQ2YCX7EatUOuyQMt2Vwx4uV+d/A3DP6PtMGBKpF8St4iGw==</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
For key generation OpenSSL can be used as in your code. However, the exported PEM key must be imported in the phpseclib part as shown in the code above (this import is missing in your code):
// Key generation with OpenSSL
$config = array(
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$res = openssl_pkey_new($config); // create key resource using $config
//openssl_pkey_export($res, $privKey); // export private key (PEM encoded, PKCS#8 format); not required for this example
$pubKeyDetails = openssl_pkey_get_details($res);
$x509pem = $pubKeyDetails["key"]; // export public key (PEM encoded, X.509 format)
// Key conversion with phpseclib
$publicKey = PublicKeyLoader::load($x509pem); // import public PEM key generated with OpenSSL
$xmlFormattedKey = $publicKey->toString("XML"); // export public XML key
print($xmlFormattedKey);
Alternatively, also key generation can be done with phpseclib:
use phpseclib3\Crypt\RSA;
$privateKey = RSA::createKey(2048); // generate private key
$xmlFormattedKey = $privateKey->getPublicKey()->toString("XML"); // export public XML key
print($xmlFormattedKey);

EC Public Key Encryption with OpenSSL

Is there a possibility in PHP's OpenSSL implementation to encrypt via public key/decrypt via private key using PHP OpenSSL?
As per the OpenSSL documentation, openssl_seal() does not support an EC public key.
The public key must be RSA because it is the only OpenSSL public key
algorithm that supports key transport.
openssl_public_encrypt() also throws an error when using it with an EC key.
My key creation works as follows:
// Configuration settings for the key
$config = array(
"private_key_type" => OPENSSL_KEYTYPE_EC,
"curve_name" => "secp521r1"
);
// Create the private and public key
$res = openssl_pkey_new($config);
// Extract the private key into $private_key
openssl_pkey_export($res, $private_key);
// Extract the public key into $public_key
$public_key = openssl_pkey_get_details($res);
$public_key = $public_key["key"];
Is there any way to do ec public key encryption with OpenSSL?
Why is the failure of openssl() and openssl_public_encrypt() for EC not documented at php.net?

How to Encrypt password by using Public Key (.pem) file in php

I have a Public Key(.pem) file and password to encrypt to use.
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1aMZtnww+noqWmOfobCE
ivRr5Iih9im26pyk1P2M/ofqke+4ddPDtD4HcfhOuDc0q6KNmuTHwFf+uIjYyIzk
SMnTc+P2jkYfw1zydexTX3b2fKyF6a6y3irJhtKx+PN5y/LOmfTch+A0ycfpCb4E
XvG8hlfWpMpSIJXq/ToPKY+AxFHbIfpRAhOQognLQiogSNe1gZgtDwOVyNjg+c/p
2p8e3SAOpOOr7NRA6Yw4AXn5W4/0mwAlFr5+tDlQqGSyi9VzEuGU1K+0tdj37bnc
/iXe59TH+9uMnZ0utrDmRzMLWBhJiqRYSskqScPlE3nc0tNblXI/n2pSysjt2YDl
eQIDAQAB
-----END PUBLIC KEY-----
But I am getting value [] for encrypted password. It doesn't get any error but return byte[] values is seem to wrong.
I have code below if it wrong plz help me..
<?php
$key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1aMZtnww+noqWmOfobCE
SMnTc+P2jkYfw1zydexTX3b2fKyF6a6y3irJhtKx+PN5y/LOmfTch+A0ycfpCb4E
XvG8hlfWpMpSIJXq/ToPKY+AxFHbIfpRAhOQognLQiogSNe1gZgtDwOVyNjg+c/p
2p8e3SAOpOOr7NRA6Yw4AXn5W4/0mwAlFr5+tDlQqGSyi9VzEuGU1K+0tdj37bnc
eQIDAQAB';
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->loadKey(array(
'e' => new Math_BigInteger(65537),
'n' => new Math_BigInteger(substr($key, 0, -4), -256)
));
$ciphertext = $rsa->encrypt('password(testing8)');
echo bin2hex($ciphertext);

openssl_pkey_get_private return null PHP Zend

I have private key here:
-----BEGIN ENCRYPTED PRIVATE KEY-----
...<< private_key_bits' => 2048 >>...
-----END ENCRYPTED PRIVATE KEY-----
i want to encrypt string, this is my code:
$rsa = Rsa::factory(array(
'public_key' => 'public_key_'.$username.'.pub',
'private_key' => 'private_key_'.$username.'.pem',
'pass_phrase' => $username.' '.date("Y/m/d h:i:s").'',
'binary_output' => false
));
$encrypt = $rsa->encrypt($content);
it return an error:
Unable to load private key; openssl error:0906D06C
I saw in zend/crypt then try to print_r parameters, it right. name of key private, pass phrase.. was true, then i print_r openssl_pkey_get_private result:
public function __construct($pemString, $passPhrase = null)
{
$result = openssl_pkey_get_private($pemString, $passPhrase);
print_r($result);
die();
if (false === $result) {
throw new Exception\RuntimeException(
'Unable to load private key; openssl ' . openssl_error_string()
);
}
but openssl_pkey_get_private return null. i don't know whats wrong with my private key.

RSA decryption doesn't work

I trying to encrypt and decrypt a simple string with PHPseclib, but when I try to decrypt it, the result is null or empty. Could any one help me please?
here are my functions
public function myEncrypt($conteudoArquivo, $private_key) {
$rsa = new Crypt_RSA();
$rsa->loadKey($private_key);
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$cipherText = base64_encode($rsa->encrypt($conteudoArquivo));
return $cipherText;
}
public function myDecrypt($cipherText, $public_key) {
$rsa = new Crypt_RSA();
$rsa->loadKey($public_key);
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$plainText = base64_decode($rsa->decrypt($cipherText));
return $plainText;
}
and the keys values are:
$private_key = "-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCRwpD+gV0skm+9SHPWFXAHkWV3r37I3N4fom45Z0imih3fDk9E
Q6PuFb56Vfo8IWiNUoP6Gco/eDHcInzO1SSEn07reh86Aosnnj7m/RMg1N5k7A5C
NH26YlATqgJe4DX8SdS/oKLit7xTo3aR+Wg3kZOQQmE5MyRq6TVDywhNyQIDAQAB
AoGAVFk4mN75sUJogSu9RMURGIAOLM2U293ceIgBqxxW0XEZyiu4uTM/WRaiLJ82
eLeIjkeS8hccj9AZYl9exD5Zq7oGTH8HDQAcmZ6p0h3faI1/sR3gnqEB28GZJyd7
MsFRlRiOEdpJAki33m/6UsJ7hRHC6X/w2K4wofzmmHopOIECQQDCAMW7gc4Gmyb1
scP8OwA75QZWgeLo36d7ynUzjqoN682ckSbIxbT6z4eYhEKE/It0gt07WTNmrL0s
vv4DH15xAkEAwFcTC1/FufpDvxt+EKnANmuH5ekri1zcsY3UMJJnjH9e7szfVTH6
JfhTRRSfi/oOe1aRinfcp/zMJhkf36VA2QJAO8K5JlWJ/Yb1rWGhGaWjINAf7637
E/kxQnTPPZ6Iy9kDcWNVKyub4FblUhoL06Nn4fAd7hZAOzSi4ZHD9XpIQQJAc1MC
QTygcp1jB3A1i0oszLR23FyNVldMoE044BK4cZ5hTm+arRt1MFUPoIj4DNbW3g8O
3uZ1cGf8BA/mc5NDKQJAexm+LcJ3DOTaenQwHw77bfXhbvvCtcvAlYOIawkyDlTx
AkBweqy8BEJbsVRSiBv7k6Hh+T+u1ZWaKDm/ZZCMkA==
-----END RSA PRIVATE KEY----- ";
$public_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCU+1bLfPmcY7qrF/dTbAtuJlv4R/FVc1WEH9HK
U0jQjX/n/db9vz/x0i3te/bKLNEcwUhBu+PWPnOt/qVURG9BUT6RsCRFUn0CyGiUKoy45o9K/mJA
HmbrNtrUB6ckrYLF75Y50nUNsBVHUDw8yQymmiOBT1gc/KM5s1xTz44LMwIDAQAB
-----END PUBLIC KEY-----";
You need to encrypt with the public key and decrypt with the private key. That is how asymmetric encryption works. In RSA "encrypting" with the private key is actually signing and "decrypting" with the public key is verification of the signature.
The other thing is that you don't reverse the Base64 encoding properly. So you need to first decode and then decrypt:
$plainText = $rsa->decrypt(base64_decode($cipherText));
The keys you presented cannot be used with this code, because they have a different modulus. You can generate the keys this way:
$rsa = new Crypt_RSA();
$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
$keys = $rsa->createKey(2048);
$private_key = $keys["privatekey"];
$public_key = $keys["publickey"];

Categories