PHP RSA encryption and decryption

Time:2021-9-17
<?php
/**
*App RSA encryption and decryption auxiliary function
**/
function app_pri_encrypt($data){
    $privateKey = openssl_pkey_get_private(file_get_contents("app-private.key"));
    $encrypted = null;
    openssl_private_encrypt($data,$encrypted,$privateKey);
    return base64_encode($encrypted);
}
function app_pri_decrypt($data){
    $privateKey = openssl_pkey_get_private(file_get_contents("app-private.key"));
    $decrypted = null;
    openssl_private_decrypt(base64_decode($data),$decrypted,$privateKey);
    return $decrypted;
}
function app_pub_encrypt($data){
    $publicKey = openssl_pkey_get_public(file_get_contents("app-public.key"));
    $encrypted = null;
    openssl_public_encrypt($data,$encrypted,$publicKey);
    return base64_encode($encrypted);
}
function app_pub_decrypt($data){
    $publicKey = openssl_pkey_get_public(file_get_contents("app-public.key"));
    $decrypted = null;
    openssl_public_decrypt(base64_decode($data),$decrypted,$publicKey);
    return $decrypted;
}

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]