Cgo中unsigned char*類型的傳遞

	ret := 0
	
	key := []byte("1234567890abcdef")
	iv := []byte("1234567890abcdef")
	orig := []byte("hello world!")
	
	ret = Sm4CbcEncrypt(orig,cipherText,key,iv)
	ret = Sm4CbcDecrypt(cipherText[0:ret],plainText,key,iv)
	fmt.Println(string(plainText[0:ret]))
func Sm4CbcEncrypt(source,cipherText,key,iv []byte)(int){
	
	clen := (C.int)(0)
	
	C.Sm4CbcEncrypt((*C.uchar)(unsafe.Pointer(&source[0])),(C.int)(len(source)),
	(*C.uchar)(unsafe.Pointer(&cipherText[0])),&clen,
	(*C.uchar)(unsafe.Pointer(&key[0])),
	(*C.uchar)(unsafe.Pointer(&iv[0])));

	return int(clen)
}

func Sm4CbcDecrypt(source,plainText,key,iv []byte)(int){
	
	clen := (C.int)(0)
	
	C.Sm4CbcDecrypt((*C.uchar)(unsafe.Pointer(&source[0])),(C.int)(len(source)),
	(*C.uchar)(unsafe.Pointer(&plainText[0])),&clen,
	(*C.uchar)(unsafe.Pointer(&key[0])),
	(*C.uchar)(unsafe.Pointer(&iv[0])));

	return int(clen)
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章