php使用服务器端和客户端加密狗环境部署及使用记录(服务器端windows环境下部署、linux环境宝塔面板部署、客户端部署加密狗)

发布于:2024-05-07 ⋅ 阅读:(32) ⋅ 点赞:(0)

在PHP中使用加密狗(通常被称为USB加密设备或硬件安全模块HSM)涉及与加密狗的特定硬件和API接口进行交互。每个加密狗供应商都会提供其设备的驱动程序和API文档,遵循这些文档来在PHP中集成加密狗。

  1. 了解加密狗
    • 查阅加密狗的官方文档,了解其功能、API接口和任何特定的要求。
    • 确定加密狗是否支持PHP,或者是否有可用的库或插件可以与其一起使用。
  2. 安装驱动程序
    • 在服务器上安装加密狗的驱动程序。这通常涉及将驱动程序文件上传到服务器并运行安装程序。
    • 确保驱动程序与你的操作系统和PHP版本兼容。
  3. 配置PHP
    • 根据加密狗的文档,配置PHP以使用加密狗。这可能涉及在php.ini文件中添加特定的扩展或设置。
    • 重启Web服务器以使配置更改生效。
  4. 编写代码
    • 使用加密狗提供的API函数在PHP中编写代码。这些函数允许与加密狗进行通信,执行加密、解密、签名和验证等操作。
    • 通常,使用特定的函数来打开与加密狗的连接,执行所需的操作,然后关闭连接。
  5. 测试
    • 在开发环境中测试代码,确保它可以正确地与加密狗进行通信并执行所需的操作。
    • 检查错误消息和日志,以确保没有遗漏任何重要的配置或步骤。
  6. 部署
    • 将代码部署到生产环境中,并确保加密狗已正确连接到服务器。
    • 在生产环境中进行进一步的测试,以确保代码可以正常工作并满足你的需求。

ViKey加密狗环境部署

1.windows环境下部署

开发文档

网页.PHP

验证代码

$ViKeyInterface = new COM("ViKeyActiveX.ViKeyInterface.1");
$ViKeyInterfaceVersion = $ViKeyInterface->IGetVersion();

$ViKeyCount = $ViKeyInterface->IVikeyFind();
if ($ViKeyInterface->IViKeyGetError() == 0 && $ViKeyCount > 0) {
    $ViKeyIndex = 0;
    $ViKeyInterface->IVikeyUserLogin($ViKeyIndex, "11111111");
    if ($ViKeyInterface->IViKeyGetError() == 0) {
        echo "用户登陆加密狗成功! <br />";
    } else {
        echo "用户登陆加密狗失败<br /> ";
    }
} else {
    die("系统没有找到加密狗");
}

提示Fatal error: Class ‘COM’ not found in

1、在windows的“服务”里面检查COM 服务是否已经启动。如果未启动,请启动它
2、检查php目录ext文件夹下面php_com_dotnet.dll是否存在
3、在php.ini里面加入以下语句:

[PHP_COM_DOTNET]
extension=php_com_dotnet.dll
com.allow_dcom = true

PHP 5.4.5以前的版本,只需要在php.ini中把com.allow_dcom = true打开就可以了,但是5.4.5版本以后,PHP把com/dotnet 模块集成到了一个单独的扩展中,所以需要在php.ini中加一行扩展extension=php_com_dotnet.dll,是加一行,不是打开,默认配置文件中没有这一行的,然后重启IIS或Apache,再次运行就正常了。

2.linux环境下部署(宝塔面板)

开发文档

Linux.php

验证代码

$ViKeyCount = 0;
$ViKeyErrorCode = 0;
$ViKeyErrorCode = VikeyFind($ViKeyCount);
PrintKeyValueEx("ViKeyErrorCode", $ViKeyErrorCode, "ViKeyCount", $ViKeyCount);

提示Fatal error: Uncaught Error: Call to undefined function VikeyFind()

1.php版本必须>7.2;
2.在php.ini加入extension=vikey.so
3.将vikey.so拷贝到php扩展的ext目录里面
4.将libvikey.so拷贝到系统目录里面/usr/local/lib
5.php重载配置,即可。

3.网页端使用加密狗(客户端)

使用文档

网页.支持谷歌浏览器

安装ViKey网页控件

浏览网页前请先安装“ViKey网页控件”,才可以正常的访问ViKey加密狗。

登录逻辑

在这里插入图片描述

1.javascript验证逻辑
 var ViKeyInterface;
        var bHasInstallVikey = 0;

        function IsInstallVikey() {
            if (bHasInstallVikey == 0) {
                console.log("尚未安装插件,或插件尚未正常运行");
            } else {
                console.log("插件工作正常");
            }
        }

        //执行加载
        window.onload = function () {
            var strSocketResult;
            //创建UK类
            ViKeyInterface = new ViKeySocketInterface();

            //检测控件安装
            setTimeout(function () {
                ViKeyInterface.CheckInstall();
            }, 1000);


            ViKeyInterface.ViKeySocket.onmessage = function (msg) {
                var FM = window.document.ViKeyForm;
                var ReceiveJsonData = JSON.parse(msg.data);
                //console.log(ReceiveJsonData);

                if (ReceiveJsonData.FunctionType == "VikeyFind") {
                    if (ReceiveJsonData.ErrorCode == 0) {
                        console.log("已找到ViKey加密锁数量:" + ReceiveJsonData.Count);
                    } else {
                        console.log("查找失败 ERRORCODE:" + ReceiveJsonData.ErrorCode);
                    }
                } else if (ReceiveJsonData.FunctionType == "CheckInstall") {
                    //alert("CheckInstall");
                    if (ReceiveJsonData.ErrorCode == 0) {
                        bHasInstallVikey = 1;
                        IsInstallVikey();
                        setTimeout(function () {
                            IsInstallVikey();

                        }, 500);
                    }
                } else if (ReceiveJsonData.FunctionType == "VikeyGetHID") {
                    if (ReceiveJsonData.ErrorCode == 0) {
                        console.log("获取硬件序列号成功,硬件 " + ReceiveJsonData.HID);
                        FM.etdNTID.value = ReceiveJsonData.HID;
                    } else {
                        console.log("获取硬件序列号失败 ERRORCODE:" + ReceiveJsonData.ErrorCode);
                    }
                } else if (ReceiveJsonData.FunctionType == "VikeyUserLogin") {
                    if (ReceiveJsonData.ErrorCode == 0) {
                        console.log("用户登录成功" + FM.UserPin2.value);
                        //location.href = "http://tools.qiaodu.net/dog/";
                        /*登录逻辑,
                           * 1.使用ajax将将FM.UserPin2.value作为参数传至后台
                           * 2.后台加密FM.UserPin2.value
                           * 3.设置cookie,返回给前台
                           * 4.判断是否能正常登录
                        * */

                    } else {
                        console.log("用户权限登陆失败 ERRORCODE:" + ReceiveJsonData.ErrorCode);
                    }
                }
            };
        }

        //插件是否正常
        function CheckInstallEx() {
            //bHasInstallVikey = 0;
            ViKeyInterface.CheckInstall();
            setTimeout(function () {
                IsInstallVikey();
            }, 500);
        }

        //以用户权限登录
        function ViKeyUserLogon() {
            var FM = window.document.ViKeyForm;
            var ViKeyIndex = 0;
            ViKeyInterface.FindViKey();
            ViKeyInterface.VikeyGetHID(ViKeyIndex);//获取硬件ID
            ViKeyInterface.VikeyUserLogin(ViKeyIndex, FM.UserPin2.value);
        }
2.HTML容器
<form action="" name="ViKeyForm" id="ViKeyForm">
    <table width="960" border="1" align="center" bgcolor="#dfe7df">
        <tr>
            <td width="200">&nbsp;</td>
            <td>&nbsp;</td>
            <td><input name="btnInstall" type="button" class="ctbottom" id="btnInstall" value="检测控件"
                       onClick="CheckInstallEx();"></td>
        </tr>
        <tr>
            <td>
                <div align="right"><span class="ctbody3">硬件序列号:</span></div>
            </td>
            <td><input name="etdNTID" type="text" class="cttextboxt" id="etdNTID" size="90" maxlength="32"></td>
            <td></td>
        </tr>
        <tr>
            <td>
                <div align="right" class="ctbody3">用户密码:</div>
            </td>
            <td><input name="UserPin2" type="password" class="cttextboxt" id="UserPin2" autocomplete="off"></td>
            <td><input name="btnUserPin2" type="button" class="ctbottom" id="btnUserPin2" value="用户登录"
                       onClick="ViKeyUserLogon();"></td>
        </tr>
    </table>
</form>

@漏刻有时