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

2020-08-04 10:11:19 浏览数 (1)

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

代码语言:javascript复制
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: %dn", 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=%dn", n)
			if err != nil {
				log.Fatal(err)
			}
			log.Printf("%sn", bytesToHexStr(buf, n))
			//out := make([]byte, n)
			//iconv.Convert(buf, out, "GB2312", "utf-8")
			//log.Printf("%sn", 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:%sn", 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:%sn", 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:%sn", 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)
}

0 人点赞