php下ssl加密解密[转]

function ssl_publickey_encrypt($data,$crt) {
    $key_content = file_get_contents($crt);
    $publickeyid = openssl_get_publickey($key_content);
    if (@openssl_public_encrypt($data,$encrypted,$publickeyid)) {
        return base64_encode("".$encrypted);
    } else {
        return false;
    }
}
function ssl_publickey_decrypt($data,$crt) {
    $key_content = file_get_contents($crt);
    openssl_get_publickey($key_content);
    if (openssl_public_decrypt($data,$decrypted,$key_content)) {
        return $decrypted;
    } else {
        return false;
    }
}
function ssl_privatekey_decrypt($data,$pem,$password='') {
    $key_content = file_get_contents($pem);
    $privatekeyid = @openssl_get_privatekey($key_content);
    $data = base64_decode($data);
    if (@openssl_private_decrypt($data,$decrypted,$privatekeyid,OPENSSL_PKCS1_PADDING)) {
        return "".$decrypted;
    } else {
        return false;
    }
}
function ssl_privatekey_encrypt($data,$pem,$password='') {
    $key_content = file_get_contents($pem);
    $privatekeyid = @openssl_pkey_get_private($key_content,$password);
    if (@openssl_private_encrypt($data,$encrypted,$privatekeyid)) {
        return "".$encrypted;
    } else {
        return false;
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注