登录
首页 >  Golang >  Go问答

差异性比较:在 C# 中导入 Go Dll 后 CString 返回值的不同

来源:stackoverflow

时间:2024-03-02 12:06:25 434浏览 收藏

本篇文章向大家介绍《差异性比较:在 C# 中导入 Go Dll 后 CString 返回值的不同》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。

问题内容

我在go中编写了一个dll,用公钥/私钥编码来解密/加密数据。

我遇到的第一个问题是在 dll 和 c# 代码之间共享字符串。

我通过使用 go 中的 c 库解决了这个问题。

func myfunc( password *c.char, passwordlength c.int )  *c.char{
   result = c.gostringn(password, passwordlength)
   return c.cstring(result)
}

或多或少是这样的。

在 c# 中,对应的是

marshal.ptrtostringauto(value);

我现在的问题是 go 和 c# 中的字符串不匹配。一个函数返回一个加密值,当我用

记录它时
fmt.println(...)

它与我在 c# 中登录的返回值不匹配

console.writeln(...)

我尝试了不同的功能:

Marshal.PtrToStringAuto(...)
Marshal.PtrToStringAnsi(...)
Marshal.PtrToStringUTF8(...)
Marshal.PtrToStringUni(...)

使用 c 库后,我也遇到了 go 中字符串的问题。因此,我增加了长度并从 c.gostring 移至 c.gostringn,这有助于在 go 中调试代码。

我真的不知道我做错了什么。有人知道我可以尝试什么吗?

输出go 1

输出go 2

我解决了部分问题。加密的输出具有定义的大小。所以在 go 中我现在能够正确读取这些值。 我在 c# 中有哪些选项可以正确读取值? marshal.ptrtostringutf8 似乎在 marshal 函数中效果最好。但它不等于 go 的价值。


解决方案


这是一个在 go dll 中使用 aes 加密的示例;字符串与我匹配:

go 代码(使用 cgo_enabled=1 goarch=386 goos=windows go build -buildmode=c-shared -o test.dll aes.go 编译):

package main

import (
    "c"
    "crypto/aes"
    "crypto/cipher"
    "crypto/rand"
    "encoding/base64"
    "errors"
    "io"
)

var cipherkey = []byte("zq4t7w!z%c&f)j@n")

func main() {}

//export encrypt
func encrypt(cmessage *c.char) *c.char {
    message := c.gostring(cmessage)
    plaintext := []byte(message)

    block, err := aes.newcipher(cipherkey)
    if err != nil {
        panic(err)
    }

    ciphertext := make([]byte, aes.blocksize+len(plaintext))
    iv := ciphertext[:aes.blocksize]
    if _, err = io.readfull(rand.reader, iv); err != nil {
        panic(err)
    }

    stream := cipher.newcfbencrypter(block, iv)
    stream.xorkeystream(ciphertext[aes.blocksize:], plaintext)

    //returns to base64 encoded string
    encmess := base64.urlencoding.encodetostring(ciphertext)
    return c.cstring(encmess)
}

//export decrypt
func decrypt(cencryptedmessage *c.char) *c.char {
    encryptedmessage := c.gostring(cencryptedmessage)
    ciphertext, err := base64.urlencoding.decodestring(encryptedmessage)
    if err != nil {
        panic(err)
    }

    block, err := aes.newcipher(cipherkey)
    if err != nil {
        panic(err)
    }

    if len(ciphertext) < aes.blocksize {
        err = errors.new("ciphertext block size is too short!")
        panic(err)
    }

    iv := ciphertext[:aes.blocksize]
    ciphertext = ciphertext[aes.blocksize:]

    stream := cipher.newcfbdecrypter(block, iv)
    stream.xorkeystream(ciphertext, ciphertext)

    decodedmess := string(ciphertext)
    return c.cstring(decodedmess)
}

c# 代码:

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var encrypted = GoFunctions.Encrypt(System.Text.Encoding.UTF8.GetBytes("hello"));
            var decrypted = GoFunctions.Decrypt(System.Text.Encoding.UTF8.GetBytes(Marshal.PtrToStringAnsi(encrypted)));
            label1.Text = Marshal.PtrToStringAnsi(encrypted);
            label2.Text = Marshal.PtrToStringAnsi(decrypted);
        }

        static class GoFunctions
        {
            [DllImport(@"C:\Repos\Go_AES\test.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
            public static extern IntPtr Encrypt(byte[] message);

            [DllImport(@"C:\Repos\Go_AES\test.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
            public static extern IntPtr Decrypt(byte[] encrypted_message);
        }
    }
}

终于介绍完啦!小伙伴们,这篇关于《差异性比较:在 C# 中导入 Go Dll 后 CString 返回值的不同》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>