Monday 20 January 2020

Encrypt decrypt aes ecb openssl

This class provide a way of Encryption & decrypt GST PHP code helps illustrate how to encryption & decript, to protect sensitive data. Using openssl_encrypt and openssl_decrypt cryptographic functions.

you don't need to intantiate the class object because its funciton are static. you can directly access the methods.certificate_file_path is your cert file path it can be sandbox or production cert files ,you can either generate it or get it from providers.

You can also use below class to generate token for any web application , $filepath will be the path of security certificate (.cer) on your local machine or your server path where you kept the .cer file. If you don't have a .cer file you can just google for it to "How To Generate .Cer File". AES known as Advanced Encryption Standard is a symmetric-key encryption algorithm.

class GSTAPIENC {
    static function test(){
        return 1;
    }
    static function generateappKey ($appkey,$filepath){
        openssl_public_encrypt($appkey, $encrypted, file_get_contents($filepath));
        return base64_encode($encrypted);   
    }
    static function encryptOTP($otp_code,$appkey) {
       return base64_encode(openssl_encrypt($otp_code, "AES-256-ECB", $appkey, OPENSSL_RAW_DATA));
    }
    static function encryptData($data, $key) {
        return base64_encode(openssl_encrypt($data, "AES-256-ECB", $key, OPENSSL_RAW_DATA));
    }
    static function mac256($ent, $key) {
        $res = hash_hmac('sha256', $ent, $key, true); 
        return $res;
    }
    static function decryptData($data, $key) {
        return openssl_decrypt(base64_decode($data), "AES-256-ECB", $key, OPENSSL_RAW_DATA);
    }
    static function decodeJsonResponse($out, $rek, $ek) {
        $apiEK = GSTAPIENC::decryptData($rek, $ek);
        return base64_decode(GSTAPIENC::decryptData($out, $apiEK));
    }
    static function keygen($length = 10) {
        $key = '';
        list($usec, $sec) = explode(' ', microtime());
        mt_srand((float) $sec + ((float) $usec * 100000));
        $inputs = array_merge(range('z', 'a'), range(0, 9), range('A', 'Z'));
        for ($i = 0; $i < $length; $i++) {
            $key .= $inputs{mt_rand(0, 61)};
        }
        return base64_encode($key);
    } 
}

You can use it like below to generate API KEY

$certificate_file_path = "PATH OF YOUR SANDBOX OR PRODUCTION cert file.";
$generated_key = GSTAPIENC::keygen(32);
$appKey = GSTAPIENC::generateappKey(base64_decode($generated_key), $certificate_file_path);

You can also view download source

https://github.com/boy108zon/Encryption-Decription-GST