by Anish
Posted on Wednesday December 12 , 2018
Asymmetric cryptography is a branch of cryptography where a secret key can be divided into two parts, a public key and a private key. The public key can be given to anyone, trusted or not, while the private key must be kept secret (just like the key in symmetric cryptography).
Encryption with asymmetric cryptography works in a slightly different way from symmetric encryption. Someone with the public key is able to encrypt a message, providing confidentiality, and then only the person in possession of the private key is able to decrypt it.
Some Key Algorithms for Asymmetric cryptography
The length of the cryptographic key is perhaps the most important security parameter that can be set at the discretion of the security professional
As of 2016, the NSA's Commercial National Security Algorithm Suite includes
Cryptosystem | Usage |
---|---|
RSA 3072-bit or larger | Key Establishment, Digital Signature |
Diffie-Hellman (DH) 3072-bit or larger | Key Establishment |
ECDH with NIST P-384 | Key Establishment |
ECDSA with NIST P-384 | Digital Signature |
SHA-384 | Integrity |
AES-256 | Confidentiality |
For More information around key length requirement visit keylength.com
openssl_pkey_new: Generates a new private key and public key pair.
The Syntax
openssl_pkey_new ([ array $configargs ] )
// Configuration for 4096 RSA key Pair with Digest Algo 512
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 1024,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
private_key_type can be
Examples
<?php
/**
* Created by https://8gwifi.org
* User: Anish Nath
* Date: 2018-12-17 * Time: 10:05 */
// Configuration for 4096 RSA key Pair with Digest Algo 512
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create the keypair
$res=openssl_pkey_new($config);
// Get private key
openssl_pkey_export($res, $privkey );
// Get public key
$pubkey=openssl_pkey_get_details($res);
$pubkey=$pubkey["key"];
echo "====PKCS1 RSA Key in Non Encrypted Format ====\n";
var_dump($privkey);
echo "====PKCS1 RSA Key in Encrypted Format====\n ";
// Get private key in Encrypted Format
openssl_pkey_export($res, $privkey,"myverystrongpassword" );
// Get public key
$pubkey=openssl_pkey_get_details($res);
$pubkey=$pubkey["key"];
var_dump($privkey);
echo "RSA Public Key \n ";
var_dump($pubkey);
The above example will output something similar to:
$ /usr/bin/php pkey_new_rsa.php
====PKCS1 RSA Key in Non Encrypted Format ====
string(916) "-----BEGIN PRIVATE KEY-----
MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAN5Grau1PqtgQNt3
SpFkPVlxkMfoZg2Z1/mdgD3cWm6YinCpfdeoiVGqVH+MitBQjFHFhTF467tQuH76
43rJI9CZNaWF+E8iwOhRc8Fh+lGVjFBJ1uYQbT1nQ2dPEGAVgLSuatUYi4bQo2Vi
x04B352+QmTYWjLdVRRlR43xdrNvAgMBAAECgYEA0YYMVYyey4sK+BXF27GCxxpv
IJHoCTAYzzpxVV6ufAwci8CpWC2Na8bEqgsAGweijb5EJHNYupSHpWK4JdkOgOPy
3/wpPL/fE0VveWmLUTpyVz7A4peEms8PX+qggsjFzP7TAZbEJZoSIdWwZGCkleu6
AVDFOothmVOjMfslzYkCQQD/gE9FJDxlXkq7SIi1nwrWKDRBaT7LzUEoDEjWQhHS
ZI/OvP4GBUUIbvtRPoZwjCZfxC3x9bqhppLsVox9//UjAkEA3rXDl0MAq/keXqhf
gHtOSENPv7XBp605TyogvRaA+obFy/ug3uAhD4TT7AW+bQ729JuB/TG8/84pmWlB
iixrRQJAMAjOXmBWnnQgtp8HXKXe4q5CTL3dmXEoO8A7fVSEhzpystpjI3ygItrC
kt6fZc9jc4OtY5zgDn4Dhz0qQpH5YwJBAL0po9zpszte2FXMK3nkweQNEEKkbb0v
G9tKzA3vfFPOf1i3/LbxpgMZbist4cWeDVJMd4eIZKDeCmVciqeC/QUCQQDcxTgQ
5apClv2+gDxa3WNac0uq3ARn964dtiX4T793MXPXXQAdcNEdzoGe7QD9GIioxLpN
v1L22DBC8CVy3oru
-----END PRIVATE KEY-----
"
====PKCS1 RSA Key in Encrypted Format====
string(1041) "-----BEGIN ENCRYPTED PRIVATE KEY-----
MIICxjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIG7dKRedVC1wCAggA
MBQGCCqG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mTWl
hfhPIsDoUXPBYfpRlYxQSdbmEG09Z0NnTxBgFYC0rmrVGIuG0KNlYsdOAd+dvkJk
2Foy3VUUZUeN8XazbwIDAQAB
-----END PUBLIC KEY-----
"
Note: that output format is PKCS#1 Type.
<?php
/**
* Created by https://8gwifi.org
* User: Anish Nath
* Date: 2018-12-17 * Time: 10:05 */
// Configuration for 2048 DSA key Pair with Digest Algo 512
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_DSA,
);
// Create the keypair
$res=openssl_pkey_new($config);
// Get private key
openssl_pkey_export($res, $privkey, "mystrongpassword" );
// Get public key
$pubkey=openssl_pkey_get_details($res);
$pubkey=$pubkey["key"];
var_dump($privkey);
var_dump($pubkey);
Elliptic curve based algorithms use significantly smaller key sizes than their non elliptic curve equivalents.
The approximate equivalence in security strength for symmetric algorithms compared to standard asymmetric algorithms and elliptic curve algorithms is shown in the table below.
AES Symmetric Keysize (Bits) | RSA and DSA Keysize (Bits) | ECC Keysize (Bits) |
---|---|---|
80 | 1024 | 160 |
112 | 2048 | 224 |
128 | 3072 | 256 |
192 | 7680 | 384 |
256 | 15360 | 512 |
Required PHP Version: (PHP 7 >= 7.1.0)
<?php
/**
* Created by https://8gwifi.org
* User: Anish Nath
* Date: 2018-12-17 * Time: 10:05 */
// Configuration for 2048 DSA key Pair with Digest Algo 512
$config = array(
"digest_alg" => "sha512",
"curve_name" => prime256v1,
"private_key_type" => OPENSSL_KEYTYPE_EC,
);
// Create the keypair
$res=openssl_pkey_new($config);
// Get private key
openssl_pkey_export($res, $privkey, "mystrongpassword" );
// Get public key
$pubkey=openssl_pkey_get_details($res);
$pubkey=$pubkey["key"];
var_dump($privkey);
var_dump($pubkey);
The above example will output something similar to:
$ /bin/php-7.1.19 pkey_new_ec.php
string(299) "-----BEGIN EC PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,606FAE9BA918BDEC
xo8McQWsbm/5A16e9DLneus2yfWEdSFkiG9DJ3Y9tjN1khWjQ0kKb+L40csSmrel
LvcxyO1pHmKXd5lW68T7sV5tUJwjXEkLslbCiSbqcCp+LT7+fw2Jdz5dEduElZGb
oK/e53CFZl0fL7PM7wDmj6G06F+I19iRL9wf3T8w+rs=
-----END EC PRIVATE KEY-----
"
string(178) "-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWcxIutWEkMXuakdcHhJW3hpOECBo
/7/aqoS7M2ruspT+AVRFN6RPwPkRNnJE4a2I5Sun/Ny0Bg9c+1c0BUFmYw==
-----END PUBLIC KEY-----
"
Continue Reading Part 2 (php Asymmetric Key Encryption/Decryption/Sign Verify The message)
Thanku for reading !!! Give a Share for Support
Instead of directly asking for donations, I'm thrilled to offer you all nine of my books for just $9 on leanpub By grabbing this bundle you not only help cover my coffee, beer, and Amazon bills but also play a crucial role in advancing and refining this project. Your contribution is indispensable, and I'm genuinely grateful for your involvement in this journey!
Any private key value that you enter or we generate is not stored on this site, this tool is provided via an HTTPS URL to ensure that private keys cannot be stolen, for extra security run this software on your network, no cloud dependency