go语言版串口获取银商秘钥工具

简介: go语言版串口获取银商秘钥工具

体验ç和GO语言混合编程,去语言的魅力。互联网时代的“”,“C”,没错,就是要简洁高效


package main
/*
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define U08 unsigned char
#define U32 unsigned int
void myhello(int i) {
  printf("Hello C: %d\n", i);
}
static int GenerateRandomKey(U08 *psRandom, U08 *psRandomKey)
{
    int i = 0;
    U08 sBasicKey[16][16] = {
        {0x73, 0xF2, 0x97, 0x73, 0xAA, 0x24, 0x28, 0xE5, 0x93, 0xE5, 0x03, 0xDD, 0xAC, 0xD1, 0x54, 0xB8},
        {0x47, 0x9B, 0x35, 0x03, 0x59, 0x72, 0x7A, 0x1C, 0x5B, 0x8B, 0x10, 0x6F, 0x0C, 0xA7, 0xA1, 0xF1},
        {0x33, 0xEE, 0x40, 0xF3, 0xC9, 0xAF, 0xFB, 0xE8, 0x1A, 0x02, 0x39, 0x04, 0xCB, 0x91, 0x34, 0x83},
        {0xAA, 0x61, 0x7B, 0x3B, 0xC2, 0x7A, 0x66, 0x11, 0x3C, 0x1E, 0x95, 0xB6, 0x95, 0xCD, 0x4E, 0x33},
        {0x43, 0x76, 0xB5, 0x13, 0xFA, 0x9D, 0xC8, 0xA0, 0x9C, 0x09, 0x5A, 0xE8, 0x14, 0x60, 0x51, 0x64},
        {0xA3, 0x0B, 0xB8, 0x0F, 0xBF, 0x0B, 0x9D, 0x46, 0x9E, 0x02, 0x6D, 0x65, 0xAE, 0xBD, 0x3D, 0x12},
        {0x14, 0x55, 0xF9, 0x1F, 0xBC, 0xD3, 0x52, 0xDD, 0x81, 0x49, 0x1B, 0xCA, 0x3F, 0xD5, 0x20, 0xCA},
        {0x86, 0x39, 0x25, 0xBC, 0xC9, 0x2B, 0xCC, 0xEF, 0x5A, 0x8E, 0x06, 0x90, 0xE9, 0x1E, 0xE6, 0x3F},
        {0x60, 0x2D, 0x0C, 0x5B, 0xB3, 0x7E, 0x06, 0x98, 0xE7, 0x0D, 0x50, 0xDC, 0x17, 0x40, 0x5B, 0xBA},
        {0x4D, 0x4A, 0x79, 0xC7, 0x0A, 0xEC, 0xA9, 0xA3, 0x9A, 0x36, 0x67, 0x55, 0x35, 0xCE, 0x3D, 0xFE},
        {0x33, 0xE4, 0x4B, 0x53, 0xFB, 0x45, 0xFB, 0x31, 0x9C, 0xD2, 0x02, 0x93, 0x76, 0x87, 0x6F, 0x87},
        {0xB8, 0x78, 0x6B, 0x1B, 0xB5, 0x7A, 0x60, 0xF5, 0x88, 0x6B, 0x61, 0x6A, 0xBF, 0xB4, 0x95, 0x33},
        {0xB2, 0x20, 0x8D, 0xD7, 0x86, 0x0F, 0xCD, 0x16, 0x9E, 0x95, 0x0F, 0xDE, 0x39, 0x02, 0xFF, 0xC9},
        {0xE9, 0x83, 0x95, 0xE1, 0x90, 0xB0, 0xA5, 0x72, 0x7D, 0x6E, 0x20, 0x37, 0xE5, 0x62, 0x37, 0x22},
        {0xF9, 0xF4, 0x31, 0x6D, 0xB6, 0xF6, 0xD8, 0xF3, 0xF2, 0x82, 0x16, 0xF1, 0x94, 0x9D, 0x5A, 0xE5},
        {0x30, 0xD6, 0x90, 0x4E, 0x5D, 0xAA, 0x3D, 0xDE, 0xA8, 0xBB, 0x0B, 0x0C, 0x80, 0xE9, 0x6F, 0x51}};
    if ((psRandom == NULL) || (psRandomKey == NULL))
    {
        return 1;
    }
    for (i = 0; i < 8; i++)
    {
        psRandomKey[2 * i] = sBasicKey[2 * i][psRandom[i] >> 4 & 0x0F];
        psRandomKey[2 * i + 1] = sBasicKey[2 * i + 1][psRandom[i] & 0x0F];
    }
    return 0;
}
// 从长度字节开始输入
int CalcXOR(U08 *in, U08 * xor)
{
    U08 len,i;
    U08 out=0;
    len = in[0];
    if(len <1)
    {
        return 1;
    }
    out = in[0];
    for(i=1; i<len+1; i++)
    {
        out = out ^ in[i];
    }
    *xor = out;
    return 0;
}
U32 XOR_Calc( U08 *data, U32 size )
{
    U32 i,rcode;
    rcode = 0;
    for( i = 0; i < size; i++ )
    { rcode ^= data[i]; }
    return rcode;
}
void GetRand(U08 *send,U08* in)
{
    send[0] = 0x02;
    send[1] = 0x11;
    send[2] = 0x04;
    srand((unsigned)time(NULL));
    for (int i = 0; i < 8; i++)
    {
        in[i] = i;
    }
    int ra = rand() % (100000)+1000;
    memset(in,0,16);
    in[0] = ra << 24;
    in[1] = ra << 16;
    in[2] = ra << 8;
    in[3] = ra << 0;
    memcpy(&send[3], in, 16);
    CalcXOR(&send[1], &send[19]);
    send[20] = 0x03;
}
*/
import "C"
import (
  "flag"
  "fmt"
  "github.com/larspensjo/config"
  "github.com/tarm/goserial"
  //"hex"
  "bytes"
  //"github.com/djimenez/iconv-go"
  "encoding/hex"
  "log"
  "os"
  "strconv"
  "testgo/desutil"
  "unsafe"
)
func bytesToHexStr(data []byte, lenth int) string {
  buf := data[0:lenth]
  hexStr := fmt.Sprintf("%x", buf)
  //fmt.Println(hexStr)
  return hexStr
}
// bytes to hex string
func bytesToHexString(b []byte) string {
  var buf bytes.Buffer
  for _, v := range b {
    t := strconv.FormatInt(int64(v), 16)
    if len(t) > 1 {
      buf.WriteString(t)
    } else {
      buf.WriteString("0" + t)
    }
  }
  return buf.String()
}
// hex string to bytes
func hexStringToBytes(s string) []byte {
  bs := make([]byte, 0)
  for i := 0; i < len(s); i = i + 2 {
    b, _ := strconv.ParseInt(s[i:i+2], 16, 16)
    bs = append(bs, byte(b))
  }
  return bs
}
var (
  conFile = flag.String("configfile", "/config.ini", "config file")
)
func memcpy(dst, src []byte, size int) {
  for i := 0; i < size; i++ {
    dst[i] = src[i]
  }
  return
}
func main() {
  C.myhello(C.int(12))
  fmt.Println("Hello Go")
  fmt.Println("DES test...")
  data := []byte("1234567812345678")
  key := []byte("12345678")
  result, err := desutil.DesEncrypt(data, key)
  if err != nil {
    fmt.Println(err)
  }
  a := hex.EncodeToString(result)
  fmt.Println(a)
  out, _ := hex.DecodeString(a)
  result, err = desutil.DesDecrypt(out, key)
  if err != nil {
    fmt.Println(err)
  }
  a = hex.EncodeToString(result)
  fmt.Println(a)
  //=============================================
  var name string
  //获取当前路径
  file, _ := os.Getwd()
  cfg, err := config.ReadDefault(file + *conFile)
  //获取配置文件中的配置项
  id, err := cfg.String("COM", "COMID")
  //设置串口编号
  c := &serial.Config{Name: id, Baud: 115200}
  //打开串口
  s, err := serial.OpenPort(c)
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println("open serial port ok! ")
  fmt.Println("Please press any key to continue: ")
  fmt.Scanln(&name)
  //command := "00A400023F00"
  // 写入串口命令
  bufsend := make([]byte, 21, 128)
  bufrand := make([]byte, 16)
  C.GetRand((*C.uchar)(unsafe.Pointer(&bufsend[0])), (*C.uchar)(unsafe.Pointer(&bufrand[0])))
  //n, err := s.Write([]byte(command))
  log.Printf("->send:%s", bytesToHexStr(bufsend, len(bufsend)))
  n, err := s.Write(bufsend)
  if err != nil {
    log.Fatal(err)
  }
  go func() {
    buf := make([]byte, 256)
    for {
      n, err = s.Read(buf)
      log.Printf("收到串口信息,len=%d\n", n)
      if err != nil {
        log.Fatal(err)
      }
      log.Printf("%s\n", bytesToHexStr(buf, n))
      //out := make([]byte, n)
      //iconv.Convert(buf, out, "GB2312", "utf-8")
      //log.Printf("%s\n", bytesToHexString(out))
    }
  }()
  randkey := make([]byte, 16)
  databuf := make([]byte, 80)
  var i int
  for i = 0; i < 80; i++ {
    databuf[i] = 0x20
  }
  C.GenerateRandomKey((*C.uchar)(unsafe.Pointer(&bufrand[0])), (*C.uchar)(unsafe.Pointer(&randkey[0])))
  log.Printf("randkey:%s\n", bytesToHexStr(randkey, 16))
  MID := "AAAA"
  SN := "AAAA18030710025"
  bufsend[0] = 0x02
  bufsend[1] = 73
  memcpy(bufsend[2:], bufrand, 8)
  bufsend[10] = 0x01
  memcpy(databuf, []byte(MID), len(MID))
  memcpy(databuf[20:], []byte(SN), len(SN))
  log.Printf("databuf:%s\n", bytesToHexStr(databuf, 77))
  data = make([]byte, 8)
  bufsend = bufsend[:77]
  for i = 0; i < 8; i++ {
    memcpy(data, databuf[(i*8):], 8)
    out, _ := desutil.Des3Encrypt(data, randkey)
    memcpy(bufsend[(i*8+11):], out, 8)
  }
  bufsend[75] = 0x03
  bufsend[76] = byte(C.XOR_Calc((*C.uchar)(unsafe.Pointer(&bufsend[0])), 76))
  log.Printf("sendrequest:\n")
  //log.Printf("bufsend:%s\n", bytesToHexStr(bufsend, 77))
  log.Printf("->send:%s", bytesToHexStr(bufsend, len(bufsend)))
  n, err = s.Write(bufsend)
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println("Please press any key to continue: ")
  fmt.Scanln(&name)
}
相关文章
|
1月前
|
网络协议 Linux Go
分享一个go开发的工具-SNMP Server
分享一个go开发的工具-SNMP Server
54 0
|
1月前
|
测试技术 API 开发者
【Docker项目实战】在Docker环境下部署go-file文件分享工具
【2月更文挑战第15天】在Docker环境下部署go-file文件分享工具
80 1
|
8月前
|
关系型数据库 MySQL Go
Go语言微服务框架 - 8.Gormer迭代-定制专属的ORM代码生成工具
我们对比一下GORM库提供的`gorm.Model`,它在新增、修改时,会自动修改对应的时间,这个可以帮我们减少很多重复性的代码编写。这里,我就针对现有的gormer工具做一个示例性的迭代。
60 0
|
17天前
|
编译器 Go 索引
浅谈go语言中的符文字符处理工具
【5月更文挑战第20天】本文简述了Go 1.20之后的rune符文处理工具和函数,`unsafe`包新增了SliceData、String和StringData函数,支持直接将slice转换为array,明确了数组和结构体比较顺序。
33 1
浅谈go语言中的符文字符处理工具
|
21天前
|
数据可视化 算法 Java
了解go语言运行时工具的作用
【5月更文挑战第16天】本文简介`runtime`库提供系统调用包装、执行跟踪、内存分配统计、运行时指标和剖析支持。`internal/syscall`封装系统调用,保证uintptr参数有效。`trace`用于执行跟踪,捕获各种事件,如goroutine活动、系统调用和GC事件。`ReadMemStats`提供内存分配器统计。`metrics`接口访问运行时定义的度量,包括CPU使用、GC和内存信息。`coverage`支持代码覆盖率分析,`cgo`处理C语言交互,`pprof`提供性能剖析工具集成。这些功能帮助优化和理解Go程序的运行行为。
37 6
|
1月前
|
搜索推荐 Linux Go
分享一个go开发的端口转发工具-port-forward
分享一个go开发的端口转发工具-port-forward
39 0
|
1月前
|
Go
推荐一个go写的RTSP转直播工具
推荐一个go写的RTSP转直播工具
23 0
|
1月前
|
大数据 Java Go
Go语言在大数据处理中的核心技术与工具
【2月更文挑战第22天】本文深入探讨了Go语言在大数据处理领域的核心技术与工具。通过分析Go语言的并发编程模型、内存管理、标准库以及第三方工具库等方面,展现了其在大数据处理中的优势和实际应用。同时,本文也讨论了如何使用这些技术与工具构建高效、稳定的大数据处理系统,为开发者提供了有价值的参考。
|
1月前
|
设计模式 测试技术 Go
Go 项目必备:Wire 依赖注入工具的深度解析与实战应用
在现代软件开发中,依赖注入(Dependency Injection,简称 DI)已经成为一种广泛采用的设计模式。它的核心思想是通过外部定义的方式,将组件之间的依赖关系解耦,从而提高代码的可维护性、可扩展性和可测试性。然而,随着项目规模的增长,手动管理复杂的依赖关系变得日益困难。这时,依赖注入代码生成工具就显得尤为重要。在众多工具中,Wire 以其简洁、强大和易用性脱颖而出,成为 Go 语言项目中的宠儿。本文将带你深入了解 Wire 的安装、基本使用、核心概念以及高级用法,并通过一个实际的 web 博客项目示例,展示如何利用 Wire 简化依赖注入的实现。准备好了吗?让我们开始这场代码解耦的奇
|
11月前
|
分布式计算 监控 安全
GO语言安全工具开发方向探索
GO语言安全工具开发方向探索
160 0