首页 新闻 赞助 找找看

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

0
悬赏园豆:20 [待解决问题]

项目上要调用一段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))
return out, 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")
}

for len(crypted) > 0 {
    block.Decrypt(dst, crypted[:bs])
    crypted = crypted[bs:]
    dst = dst[bs:]
}

return out, nil

}

下面是C#里的
[DllImport("test", EntryPoint = "DesECBEncryptForHex")]
static extern string DesECBEncryptForHex(string txt, string key);

    [DllImport("test", EntryPoint = "PrintHello")]
    extern static void PrintHello();

    [DllImport("test", EntryPoint = "Sum")]
    extern static int Sum(int a, int b);

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

传说中的十三月的主页 传说中的十三月 | 初学一级 | 园豆:102
提问于:2019-10-28 11:34
< >
分享
所有回答(1)
0

不懂go,但是可以给出点建议,既然别的函数可以调用,那就可能是go写的DesECBEncryptForHex方法内部可能有问题,建议在DesECBEncryptForHex方法里加入一些日志或打印输出,看看代码执行到了哪一步出了错

会长 | 园豆:12401 (专家六级) | 2019-10-28 11:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册