RSA Algorithm in PHP

Hello,
two days ago i need to implement RSA encryption public/private key management and i was stuck where should i begin what should i need and so on so i wrote this tutorial for it in php


first php support it by default using openssl


first you need to generate keys private/public key per user



        // generate a 2048 bit rsa private key, returns a php resource, save to file
        $privateKey = openssl_pkey_new(array(
            'private_key_bits' => 2048,
            'private_key_type' => OPENSSL_KEYTYPE_RSA,
        ));
        openssl_pkey_export_to_file($privateKey, 'private_key.pem');

        // get the public key $keyDetails['key'] from the private key;
        $keyDetails = openssl_pkey_get_details($privateKey);
        file_put_contents('public_key.pem', $keyDetails['key']);

after this you should have private_key.pem , public_key.pem files you can use them for encryption and decryption key length used in key generation was 2048 awesome it's hard for bruteforce attack and/or cryptanalysis applications



    /**
     * @param $plainText
     * @return string
     */
    public function encryption($plainText)
    {
        $public_key = openssl_pkey_get_public(file_get_contents('public_key.pem'));
        $public_key_details = openssl_pkey_get_details($public_key);
        // there are 11 bytes overhead for PKCS1 padding
        $encrypt_chunk_size = ceil($public_key_details['bits'] / 8) - 11;
        $output = '';
        // loop through the long plain text, and divide by chunks
        while ($plainText) {
            $chunk = substr($plainText, 0, $encrypt_chunk_size);
            $plainText = substr($plainText, $encrypt_chunk_size);
            $encrypted = '';
            if (!openssl_public_encrypt($chunk, $encrypted, $public_key))
                die('Failed to encrypt data');
            $output .= $encrypted;
        }
        openssl_free_key($public_key);
        return base64_encode($output);
    }





/**
     * @param $cipherText
     * @return string
     */
    public function decryption($cipherText)
    {
        // decode the text to bytes
        $encrypted = base64_decode($cipherText);
        // read the private key
        $private_key = openssl_pkey_get_private(file_get_contents('private_key.pem'));
        $private_key_details = openssl_pkey_get_details($private_key);
        // there is no need to minus the overhead
        $decrypt_chunk_size = ceil($private_key_details['bits'] / 8);
        $output = '';
        // decrypt it back chunk-by-chunk
        while ($encrypted) {
            $chunk = substr($encrypted, 0, $decrypt_chunk_size);
            $encrypted = substr($encrypted, $decrypt_chunk_size);
            $decrypted = '';
            if (!openssl_private_decrypt($chunk, $decrypted, $private_key))
                die('Failed to decrypt data');
            $output .= $decrypted;
        }
        openssl_free_key($private_key);
        return $output;

    }


well that's it

GoodBye , Happy Hacking !

Comments

Popular Posts