ITPub博客

首页 > > > C#调用golang编译的加密动态库闪一下就退出,调用普通的函数成功

C#调用golang编译的加密动态库闪一下就退出,调用普通的函数成功

作者:karspb 时间:2019-10-29 14:53:07 0 删除 编辑

项目上要调用一段3des的加密,因为用C#跟golang生成的密文匹配不上,所以无法解密,主要是golang跟C#的key的补位方式有点不一样,所以就想着直接将golang的加密函数编译成dll,用C#直接调用,我在golang里按照网上的例子写的函数,在C#里能成功调用,但是调用加密的函数时,C#里闪一下就退出了,求熟悉golang和C#的大神帮看一下,附上golang的加密函数

package main

import "fmt"

import "C"

import "bytes"

import "crypto/des"

import "encoding/hex"

import "errors"

func main() {

plainText, key := "", ""

res := DesECBEncryptForHex(plainText, key)

fmt.Println("密文")

fmt.Println(res)

}

//PrintHello :

//export PrintHello

func PrintHello() {

fmt.Println("Hello From Golang")

}

//Sum :

//export Sum

func Sum(a, b int) int {

return a + b

}

//DesECBEncryptForHex :

//export DesECBEncryptForHex

func DesECBEncryptForHex(plainText string, key string) string {

crypted := []byte(plainText)

res, _ := DesECBEncrypt(crypted, key)

out := hex.EncodeToString(res)

return out

}

func DesECBEncrypt(crypted []byte, key string) ([]byte, error) {

tk, err := hex.DecodeString(key)

if err != nil {

return nil, err

}

crypted = pkcs5Padding0(crypted, 8)

tkey := make([]byte, 24, 24)

copy(tkey, tk)

k1 := tkey[:8]

k2 := tkey[8:16]

k3 := k1

buf1, err := encrypt(crypted, k1)

if err != nil {

return nil, err

}

buf2, err := decrypt(buf1, k2)

if err != nil {

return nil, err

}

out, err := encrypt(buf2, k3)

if err != nil {

return nil, err

}

//out= bytes.Trim(out,string(0x0))returnout, nil

}

func pkcs5Padding0(ciphertext []byte, blockSize int) []byte {

padding := blockSize - len(ciphertext)%blockSize

padtext := bytes.Repeat([]byte{0x0}, padding)

return append(ciphertext, padtext...)

}

func encrypt(origData, key []byte) ([]byte, error) {

if len(origData) < 1 || len(key) < 1 {

return nil, errors.New("wrong data or key")

}

block, err := des.NewCipher(key)

if err != nil {

return nil, err

}

bs := block.BlockSize()

if len(origData)%bs != 0 {

return nil, errors.New("wrong padding")

}

out := make([]byte, len(origData))

dst := out

for len(origData) > 0 {

block.Encrypt(dst, origData[:bs])

origData = origData[bs:]

dst = dst[bs:]

}

return out, nil

}

func decrypt(crypted, key []byte) ([]byte, error) {

if len(crypted) < 1 || len(key) < 1 {

return nil, errors.New("wrong data or key")

}

block, err := des.NewCipher(key)

if err != nil {

return nil, err

}

out := make([]byte, len(crypted))

dst := out

bs := block.BlockSize()

if len(crypted)%bs != 0 {

return nil, errors.New("wrong crypted size")

}

forlen(crypted) >0{ block.Decrypt(dst, crypted[:bs]) crypted = crypted[bs:] dst = dst[bs:]}returnout,nil

}

下面是C#里的

[DllImport("test", EntryPoint = "DesECBEncryptForHex")]

static extern string DesECBEncryptForHex(string txt, string key);

[DllImport("test", EntryPoint ="PrintHello")]externstaticvoidPrintHello(); [DllImport("test", EntryPoint ="Sum")]externstaticintSum(inta,intb);

PrintHello跟Sum都能够正常调用,就是加密函数DesECBEncryptForHex调用时闪一下退出

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2001/viewspace-3143100/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 网页js脚本注入
请登录后发表评论 登录
全部评论

注册时间:2019-07-31

  • 博文量
    222
  • 访问量
    16821