vue与usbkey交互

1.淘宝购买usbkey,本文使用的是UKEY3000D

购买链接(店家看到请自行打赏 ^_^):Ukey3000D 国密SM2算法Usb key Usbkey证书Ukey二次开发

2.废话不多说,请看图:

 

 

代码片段:

<template>

  <div class="demo-input-suffix">
    <el-button type="primary" @click="login_onclick">点我有惊喜</el-button>

    <el-row style="line-height:20px;font-size:14px;margin-top:10px;margin-bottom:20px;">
      <el-col>
        <el-button type="primary" @click="getChipID">获取UKEY唯一编号</el-button>
        <el-input style="width: 300px;" v-model="ukeyId" disabled></el-input>
      </el-col>
    </el-row>

    <el-row style="line-height:20px;font-size:14px;margin-top:10px;margin-bottom:20px;">
      <el-col>
        <el-button type="primary" @click="setCal_2">设置增强算法密钥</el-button>
        <el-input style="width: 300px;" v-model="secretKey"></el-input>
      </el-col>
    </el-row>

    <el-row style="line-height:20px;font-size:14px;margin-top:10px;margin-bottom:20px;">
      <el-col>
        <el-input style="width: 300px;" v-model="text"></el-input>
        <el-button type="primary" @click="encode">加密</el-button>
        <el-input style="width: 300px;" v-model="secretText" disabled></el-input>
      </el-col>
    </el-row>

    <el-row style="line-height:20px;font-size:14px;margin-top:10px;margin-bottom:20px;">
      <el-col>
        <el-input style="width: 300px;" v-model="secretText"></el-input>
        <el-button type="primary" @click="decode">解密</el-button>
        <el-input style="width: 300px;" v-model="decodeText" disabled></el-input>
      </el-col>
    </el-row>

    <el-row style="line-height:20px;font-size:14px;margin-top:10px;margin-bottom:20px;">
      <el-col>
        <hr/>
        <h1>SM2</h1>
        <el-button type="primary" @click="genKeyPair">生成密钥对</el-button>
        <el-button type="primary" @click="genPubKeyY">GenPubKeyY</el-button>
        <el-button type="primary" @click="genPubKeyX">GenPubKeyX</el-button>
        <el-button type="primary" @click="genPriKey">获取私钥</el-button>
        <el-button type="primary" @click="setSM2KeyPair">设置密钥进锁中</el-button>
      </el-col>
    </el-row>

    <el-row style="line-height:20px;font-size:14px;margin-top:10px;margin-bottom:20px;">
      <el-col>
        <el-button type="primary" @click="reSetUkey">初始化UKEY</el-button>
        <el-button type="primary" @click="ytSetPin">设置PIN</el-button>
      </el-col>
    </el-row>

    <el-row style="line-height:20px;font-size:14px;margin-top:10px;margin-bottom:20px;">
      <el-col>
        <el-input style="width: 300px;" v-model="text"></el-input>
        <el-button type="primary" @click="sm2EncString">加密</el-button>
        <el-input style="width: 300px;" v-model="secretText" disabled></el-input>
      </el-col>
    </el-row>
    <el-row style="line-height:20px;font-size:14px;margin-top:10px;margin-bottom:20px;">
      <el-col>
        <el-input style="width: 300px;" v-model="secretText"></el-input>
        <el-button type="primary" @click="sm2DecString">解密</el-button>
        <el-input style="width: 300px;" v-model="decodeText" disabled></el-input>
      </el-col>
    </el-row>


  </div>

</template>

<script>
import SoftKey3W from "../components/sm/Syunew3";

export default {
  name: 'UKEY',

  data() {
    return {
      myUserName: "",
      myPassword: "",
      ukeyId: "",
      mSoftKey3W: null,
      devicePath: "",
      bConnect: 0,
      text: "",//待加密串
      secretText: "",//已加密串
      decodeText: "",//解密串
      secretKey: "",
      PriKey: "",
      PubKeyX : "",
      PubKeyY: "",
      CompName: "Levi"
    }
  },
  beforeMount() {

  },

  mounted() {
    this.intSoftKey3A();
  },
  methods: {

    setCal_2: function () {
      let ms = this.mSoftKey3W;
      let _this = this;

      ms.SetCal_2(_this.secretKey, _this.devicePath); //重置ukey

      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("setCal_2 onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("设置增强密钥时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }

        console.log("成功设置增强密钥 : " + UK_Data.return_value);
      }
    },

    reSetUkey: function () {
      let ms = this.mSoftKey3W;
      let _this = this;

      ms.ReSet(_this.devicePath); //重置ukey

      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("sm2 reSetPin onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("初始化UKEY时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }

        // _this.secretText = UK_Data.return_value;
        console.log("成功初始化UKEY : " + UK_Data.return_value);
      }
    },

    ytSetPin: function () {
      let ms = this.mSoftKey3W;
      let _this = this;

      ms.ReSet(_this.devicePath); //重置ukey
      ms.ResetOrder();//重置order
      ms.YtSetPin("111", "123", _this.devicePath)//加密

      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("sm2 ytSetPin onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("设置Pin码时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }

        // _this.secretText = UK_Data.return_value;
        console.log("成功设置了设置Pin码为 111 : " + UK_Data.return_value);
      }
    },
    sm2EncString: function () {
      console.log("this.text = ", this.text)

      let ms = this.mSoftKey3W;
      let _this = this;

      ms.ResetOrder();//重置order
      ms.SM2_EncString(_this.text, _this.devicePath)//加密

      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("sm2 encode onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("sm2进行加密运算时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }

        _this.secretText = UK_Data.return_value;
        console.log("sm2加密成功:" + UK_Data.return_value);
      }
    },
    sm2DecString: function () {
      console.log("this.text = ", this.secretText)

      let ms = this.mSoftKey3W;
      let _this = this;

      ms.ResetOrder();//重置order
      ms.SM2_DecString(_this.secretText, "123", _this.devicePath)

      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("sm2 decode onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("sm2进行解密运算时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }

        _this.decodeText = UK_Data.return_value;
        console.log("sm2解密成功:" + UK_Data.return_value);
      }
    },

    /**
     * 生成密钥
     */
    genKeyPair : function () {
      let ms = this.mSoftKey3W;
      let _this = this;

      ms.ResetOrder();//重置order
      ms.StarGenKeyPair(_this.devicePath);
      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("生成密钥对时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }

        // _this.secretText = UK_Data.return_value;
        console.log("生成密钥对成功:" + UK_Data.return_value);

      }
    },

    genPubKeyY : function () {
      let ms = this.mSoftKey3W;
      let _this = this;

      ms.ResetOrder();//重置order
      ms.GenPubKeyY();
      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("获取GenPubKeyY时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }

        _this.PubKeyY = UK_Data.return_value;
        console.log("获取GenPubKeyY成功:" + UK_Data.return_value);

      }
    },

    genPubKeyX : function () {
      let ms = this.mSoftKey3W;
      let _this = this;

      ms.ResetOrder();//重置order
      ms.GenPubKeyX();
      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("获取GenPubKeyX时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }

        _this.PubKeyX = UK_Data.return_value;
        console.log("获取GenPubKeyX成功:" + UK_Data.return_value);

      }
    },
    genPriKey : function () {
      let ms = this.mSoftKey3W;
      let _this = this;

      ms.ResetOrder();//重置order
      ms.GenPriKey();
      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("获取GenPriKey时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }

        _this.PriKey = UK_Data.return_value;
        console.log("获取GenPriKey成功:" + UK_Data.return_value);

      }
    },

    // 设置密钥对到锁中
    setSM2KeyPair: function () {
      let ms = this.mSoftKey3W;
      let _this = this;

      ms.ResetOrder();//重置order
      ms.Set_SM2_KeyPair(_this.PriKey, _this.PubKeyX, _this.PubKeyY, _this.CompName, _this.devicePath);

      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("setSM2KeyPair onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("设置密钥对到锁中时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }

        // _this.secretText = UK_Data.return_value;
        console.log("设置密钥对到锁中成功:" + UK_Data.return_value);

      }
    },

    encode: function () {
      console.log("this.text = ", this.text)

      let ms = this.mSoftKey3W;
      let _this = this;

      ms.ResetOrder();//重置order
      ms.EncString(_this.text, _this.devicePath)//加密

      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("进行加密运算时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }

        _this.secretText = UK_Data.return_value;
        console.log("加密成功:" + UK_Data.return_value);

      }

    },
    decode: function () {
      console.log("this.secretText = ", this.secretText)

      let ms = this.mSoftKey3W;
      let _this = this;

      ms.ResetOrder();//重置order
      ms.DecString(_this.secretText, _this.secretKey)//加密
      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("进行加密运算时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }
        _this.decodeText = UK_Data.return_value;
        console.log("解密成功:" + UK_Data.return_value);
      }

    },
    login_onclick: function () {
      myLogin_onclick(this.bConnect)
    },
    getChipID: function () {
      let ms = this.mSoftKey3W;
      let _this = this;

      // ms.ResetOrder();//重置order
      ms.GetChipID(_this.devicePath)//获取唯一ID
      ms.Socket_UK.onmessage = function gotPacket(Msg) {
        console.log("onmessage Msg:" + Msg.data);
        var UK_Data = JSON.parse(Msg.data);
        if (UK_Data.type != "Process") return;

        if (UK_Data.LastError != 0) {
          window.alert("返回芯片唯一ID时出现错误,错误码为:" + UK_Data.LastError.toString());
          ms.Socket_UK.close();
          return false;
        }
        _this.ukeyId = UK_Data.return_value;
        console.log("已成功返回芯片唯一ID:" + UK_Data.return_value);
      }
    },
    /**
     * 初始化
     * @returns {boolean}
     */
    intSoftKey3A: function () {
      console.log("this实例指向: ", this)
      let _this = this;
      //如果是IE10及以下浏览器,则跳过不处理
      if (navigator.userAgent.indexOf("MSIE") > 0 && !navigator.userAgent.indexOf("opera") > -1) return;
      try {
        let mSoftKey3W = new SoftKey3W();
        _this.mSoftKey3W = mSoftKey3W;
        mSoftKey3W.Socket_UK.onopen = function () {
          _this.bConnect = 1;//代表已经连接,用于判断是否安装了客户端服务
          console.log("客户端连接")
          mSoftKey3W.FindPort(0)
        }

        //在使用事件插拨时,注意,一定不要关掉Sockey,否则无法监测事件插拨
        mSoftKey3W.Socket_UK.onmessage = function got_packet(Msg) {
          console.log("load onmessage Msg = ", Msg)
          var PnpData = JSON.parse(Msg.data);
          if (PnpData.type == "PnpEvent")//如果是插拨事件处理消息
          {
            if (PnpData.IsIn) {
              alert("UKEY已被插入,被插入的锁的路径是:" + PnpData.DevicePath);
            } else {
              alert("UKEY已被拨出,被拨出的锁的路径是:" + PnpData.DevicePath);
            }
          }

          if (PnpData.type == "Process") {
            switch (PnpData.order) {
              case 0: {
                if (PnpData.LastError != 0) {
                  window.alert("未发现加密锁,请插入加密锁");
                  ms.Socket_UK.close();
                  return false;
                }
                _this.devicePath = PnpData.return_value;//获得返回的UK的路径
                console.log("获得返回的UK的路径:" + PnpData.return_value);
              }
            }
          }
        }

        mSoftKey3W.Socket_UK.onclose = function () {

        }
      } catch (e) {
        alert(e.name + ": " + e.message);
        return false;
      }
    },

    toHex: (n) => {
      var result = ''
      var start = true;

      for (var i = 32; i > 0;) {
        i -= 4;
        var digit = (n >> i) & 0xf;

        if (!start || digit != 0) {
          start = false;
          result += digitArray[digit];
        }
      }

      return (result == '' ? '0' : result);
    }

  }
}

var digitArray = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');

function myLogin_onclick(bConnect) {

  console.log("bConnect = ", bConnect)
  //如果是IE10及以下浏览器,则使用AVCTIVEX控件的方式
  if (navigator.userAgent.indexOf("MSIE") > 0 && !navigator.userAgent.indexOf("opera") > -1) return Handle_IE10();

  //判断是否安装了服务程序,如果没有安装提示用户安装
  if (bConnect == 0) {
    window.alert("未能连接服务程序,请确定服务程序是否安装。");
    return false;
  }
  try {
    var DevicePath, mylen, ret, keyid, username, mykey, outstring, address, mydata, i, InString, versionex,
      version;
    var PubKeyX, PubKeyY, PriKey, UserName, Pin;

    //由于是使用事件消息的方式与服务程序进行通讯,
    //好处是不用安装插件,不分系统及版本,控件也不会被拦截,同时安装服务程序后,可以立即使用,不用重启浏览器
    //不好的地方,就是但写代码会复杂一些
    var s_simnew1 = new SoftKey3W(); //创建UK类

    s_simnew1.Socket_UK.onopen = function () {
      s_simnew1.ResetOrder();//这里调用ResetOrder将计数清零,这样,消息处理处就会收到0序号的消息,通过计数及序号的方式,从而生产流程
    }


    //写代码时一定要注意,每调用我们的一个UKEY函数,就会生产一个计数,即增加一个序号,较好的逻辑是一个序号的消息处理中,只调用我们一个UKEY的函数
    s_simnew1.Socket_UK.onmessage = function got_packet(Msg) {
      var UK_Data = JSON.parse(Msg.data);
      if (UK_Data.type != "Process") return;//如果不是流程处理消息,则跳过
      //alert(Msg.data);
      switch (UK_Data.order) {
        case 0: {
          s_simnew1.FindPort(0);//查找加密锁
        }
          break;//!!!!!重要提示,如果在调试中,发现代码不对,一定要注意,是不是少了break,这个少了是很常见的错误
        case 1: {
          if (UK_Data.LastError != 0) {
            window.alert("未发现加密锁,请插入加密锁");
            s_simnew1.Socket_UK.close();
            return false;
          }
          DevicePath = UK_Data.return_value;//获得返回的UK的路径
          s_simnew1.GetChipID(DevicePath);//返回芯片唯一ID
        }
          break;
        case 2: {
          if (UK_Data.LastError != 0) {
            window.alert("返回芯片唯一ID时出现错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }
          outstring = UK_Data.return_value;
          window.alert("已成功返回芯片唯一ID:" + outstring);
          s_simnew1.YtSetPin("123", "123", DevicePath); //设置Pin码,使用默认的PIN码
        }
          break;
        case 3: {
          if (UK_Data.LastError != 0) {
            window.alert("设置Pin码时出现错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }
          window.alert("已成功设置了设置Pin码.");

          //对数据进行签名
          Pin = "123" //使用默认的PIN码
          s_simnew1.YtSign("加密锁", Pin, DevicePath);
        }
          break;
        case 4: {
          if (UK_Data.LastError != 0) {
            window.alert("对数据进行签名时出现错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }
          outstring = UK_Data.return_value;
          window.alert("已成功对数据进行签名,签名后的数据是:" + outstring);

          //对数据进行加密
          s_simnew1.SM2_EncString("加密锁", DevicePath);
        }
          break;
        case 5: {
          if (UK_Data.LastError != 0) {
            window.alert("对数据进行加密时出现错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }
          outstring = UK_Data.return_value;
          window.alert("已成功对数据进行加密,加密后的数据是:" + outstring);

          //对数据进行解密,使用默认的PIN码
          s_simnew1.SM2_DecString(outstring, Pin, DevicePath);
        }
          break;
        case 6: {
          if (UK_Data.LastError != 0) {
            window.alert("对数据进行解密时出现错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }
          outstring = UK_Data.return_value;
          window.alert("已成功对数据进行解密,解密后的数据是:" + outstring);

          //产生密钥对
          s_simnew1.StarGenKeyPair(DevicePath);
        }
          break;
        case 7: {
          if (UK_Data.LastError != 0) {
            window.alert("产生密钥对出现错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }

          //获取生成的私钥
          s_simnew1.GenPriKey();

        }
          break;
        case 8: {
          if (UK_Data.LastError != 0) {
            window.alert("获取生成的私钥时错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }
          PriKey = UK_Data.return_value;

          //获取生成的公钥X
          s_simnew1.GenPubKeyX();
        }
          break;
        case 9: {
          if (UK_Data.LastError != 0) {
            window.alert("获取生成的公钥X时错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }
          PubKeyX = UK_Data.return_value;

          //获取生成的公钥Y
          s_simnew1.GenPubKeyY();

        }
          break;
        case 10: {
          if (UK_Data.LastError != 0) {
            window.alert("获取生成的公钥Y时错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }
          PubKeyY = UK_Data.return_value;

          //设置密钥对及用户身份到锁中
          UserName = "xxx公司";
          s_simnew1.Set_SM2_KeyPair(PriKey, PubKeyX, PubKeyY, UserName, DevicePath);
        }
          break;
        case 11: {
          if (UK_Data.LastError != 0) {
            window.alert("设置密钥对及用户身份时出现错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }
          window.alert("已成功设置密钥对及用户身份.");

          //返回设置在锁中的公钥X
          s_simnew1.GetPubKeyX(DevicePath);
        }
          break;
        case 12: {
          if (UK_Data.LastError != 0) {
            window.alert("返回设置在锁中的公钥X时出现错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }
          outstring = UK_Data.return_value;
          window.alert("已成功返回设置在锁中的公钥X:" + outstring);

          //返回设置在锁中的公钥Y
          s_simnew1.GetPubKeyY(DevicePath);
        }
          break;
        case 13: {
          if (UK_Data.LastError != 0) {
            window.alert("返回设置在锁中的公钥Y时出现错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }
          outstring = UK_Data.return_value;
          window.alert("已成功返回设置在锁中的公钥Y:" + outstring);

          //返回设置在锁中的身份
          s_simnew1.GetSm2UserName(DevicePath);
        }
          break;
        case 14: {
          if (UK_Data.LastError != 0) {
            window.alert("返回设置在锁中的用户身份时出现错误,错误码为:" + UK_Data.LastError.toString());
            s_simnew1.Socket_UK.close();
            return false;
          }
          outstring = UK_Data.return_value;
          window.alert("已成功返回设置在锁中的用户身份:" + outstring);


          //所有工作处理完成后,关掉Socket
          s_simnew1.Socket_UK.close();
        }
          break;
      }
    }

    s_simnew1.Socket_UK.onclose = function () {

    }
    return true;
  } catch (e) {
    alert(e.name + ": " + e.message);
    return false;
  }

}

function Handle_IE10() {

  try {
    var DevicePath, mylen, ret, keyid, username, mykey, outstring, address, mydata, i, InString, versionex,
      version;
    var PubKeyX, PubKeyY, PriKey, UserName, Pin;

    //创建控件
    var s_simnew1 = new ActiveXObject("Syunew3A.s_simnew3");

    DevicePath = s_simnew1.FindPort(0);//'查找加密锁
    if (s_simnew1.LastError != 0) {
      window.alert("未发现加密锁,请插入加密锁");
      return false;
    } else {
      //返回芯片唯一ID
      {
        outstring = s_simnew1.GetChipID(DevicePath);
        if (s_simnew1.LastError != 0) {
          window.alert("返回芯片唯一ID时出现错误");
          return;
        }
        window.alert("已成功返回芯片唯一ID:" + outstring);
      }
      //设置Pin码
      {
        ret = s_simnew1.YtSetPin("123", "123", DevicePath);
        if (ret != 0) {
          window.alert("设置Pin码时出现错误");
          return;
        }
        window.alert("已成功设置了设置Pin码.");
      }
      //使用默认的PIN码
      Pin = "123"
      //对数据进行签名
      {
        outstring = s_simnew1.YtSign("加密锁", Pin, DevicePath);
        if (s_simnew1.LastError != 0) {
          window.alert("对数据进行签名时出现错误");
          return;
        }
        window.alert("已成功对数据进行签名:" + outstring);
      }
      //对数据进行加密
      {
        outstring = s_simnew1.SM2_EncString("加密锁", DevicePath);
        if (s_simnew1.LastError != 0) {
          window.alert("对数据进行加密时出现错误");
          return;
        }
        window.alert("已成功对数据进行加密:" + outstring);
      }
      //对数据进行解密,使用默认的PIN码
      {
        outstring = s_simnew1.SM2_DecString(outstring, Pin, DevicePath);
        if (s_simnew1.LastError != 0) {
          window.alert("对数据进行解时出现错误");
          return;
        }
        window.alert("已成功对数据进行解密:" + outstring);
      }

      //产生密钥对
      {
        ret = s_simnew1.StarGenKeyPair(DevicePath);
        if (ret != 0) {
          window.alert("产生密钥对出现错误");
          return;
        }

        PriKey = s_simnew1.GenPriKey;
        window.alert("已成功返回生成的私钥:" + PriKey);

        PubKeyX = s_simnew1.GenPubKeyX;
        window.alert("已成功返回生成的公钥X:" + PubKeyX);

        PubKeyY = s_simnew1.GenPubKeyY;
        window.alert("已成功返回生成的公钥Y:" + PubKeyY);
      }

      //对设置密钥对及用户身份
      UserName = "xxx公司";
      {
        ret = s_simnew1.Set_SM2_KeyPair(PriKey, PubKeyX, PubKeyY, UserName, DevicePath);
        if (s_simnew1.LastError != 0) {
          window.alert("设置密钥对及用户身份时出现错误");
          return;
        }
        window.alert("已成功设置密钥对及用户身份.");
      }
      //返回设置在锁中的公钥X
      {
        outstring = s_simnew1.GetPubKeyX(DevicePath);
        if (s_simnew1.LastError != 0) {
          window.alert("返回设置在锁中的公钥X时出现错误");
          return;
        }
        window.alert("已成功返回设置在锁中的公钥X:" + outstring);
      }
      //返回设置在锁中的公钥Y
      {
        outstring = s_simnew1.GetPubKeyY(DevicePath);
        if (s_simnew1.LastError != 0) {
          window.alert("返回设置在锁中的公钥Y时出现错误");
          return;
        }
        window.alert("已成功返回设置在锁中的公钥Y:" + outstring);
      }
      //返回设置在锁中的身份
      {
        outstring = s_simnew1.GetSm2UserName(DevicePath);
        if (s_simnew1.LastError != 0) {
          window.alert("返回设置在锁中的身份时出现错误");
          return;
        }
        window.alert("已成功返回设置在锁中的身份:" + outstring);
      }

    }
    return true;
  } catch (e) {
    alert(e.name + ": " + e.message);
    return false;
  }
}


</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}

.input-class {
  width: 200px;
  position: relative;
  padding-left: 15px;
}
</style>

 

 

源码:https://download.csdn.net/download/shn_207/87593876

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值