Vb.net 到 golang 的 AES

huangapple go评论146阅读模式
英文:

Vb.net to golang AES

问题

我正在尝试将一个VB函数加密程序转换为Golang。

我的Go函数返回我传入的任何值的十六进制长度,但是VB程序始终返回长度为32。

请问有人可以告诉我我做错了什么吗?

VB代码:

Function encryptString(ByVal source As String, ByVal key As Byte(), ByVal IV As Byte()) As Byte()
    Dim array As Byte() = Nothing
    Using aesManaged As System.Security.Cryptography.AesManaged = New System.Security.Cryptography.AesManaged()
        aesManaged.Key = key
        aesManaged.IV = IV
        Dim cryptoTransform As ICryptoTransform = aesManaged.CreateEncryptor(aesManaged.Key, aesManaged.IV)
        Using memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
            Using cryptoStream As System.Security.Cryptography.CryptoStream = New System.Security.Cryptography.CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write)
                Using streamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(cryptoStream)
                    streamWriter.Write(source)
                End Using
                array = memoryStream.ToArray()
            End Using
        End Using
    End Using
End Function

Go代码:

func Encrypt(key string, plaintext string) string {
    block, err := aes.NewCipher([]byte(key))

    if err != nil {
        panic(err)
    }
    str := []byte(plaintext)

    iv := []byte{178, 212, 4, 97, 181, 77, 129, 200, 198, 223, 84, 96, 81, 55, 3, 40}
    encrypter := cipher.NewCFBEncrypter(block, iv)
    encrypted := make([]byte, len(str))
    encrypter.XORKeyStream(encrypted, str)
    fmt.Printf("'%s' encrypted to %x\n", str, encrypted)
}

另外,IV和密钥两次都是相同的,但结果却不同...

英文:

I am trying to convert a function encryption routine from VB to Golang.

My Go route returns the hex length of my what ever value I pass into it but the VB routine always returns a length of 32.

Could some please advice me on what I am doing wrong?

VB:

Function encryptString(ByVal source As String, ByVal key As Byte(), ByVal IV As Byte()) As Byte()
    Dim array As Byte() = Nothing
    Using aesManaged As System.Security.Cryptography.AesManaged = New System.Security.Cryptography.AesManaged()
        aesManaged.Key = key
        aesManaged.IV = IV
        Dim cryptoTransform As ICryptoTransform = aesManaged.CreateEncryptor(aesManaged.Key, aesManaged.IV)
        Using memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
            Using cryptoStream As System.Security.Cryptography.CryptoStream = New System.Security.Cryptography.CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write)
                Using streamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(cryptoStream)
                    streamWriter.Write(source)
                End Using
                array = memoryStream.ToArray()
            End Using
        End Using
    End Using

Go

func Encrypt(key string, plaintext string) string {
	block, err := aes.NewCipher([]byte(key))

	if err != nil {
		panic(err)
	}
	str := []byte(plaintext)

	iv := []byte{178, 212, 4, 97, 181, 77, 129, 200, 198, 223, 84, 96, 81, 55, 3, 40}
	encrypter := cipher.NewCFBEncrypter(block, iv)
	encrypted := make([]byte, len(str))
	encrypter.XORKeyStream(encrypted, str)
	fmt.Printf("%s encrypted to %x\n", str, encrypted)
}

Also both times the IV and the key have been the same but I get different results...

答案1

得分: 1

默认的.Net操作模式可能是CBC(你应该显式设置),但你的Go代码使用的是CFB模式。它们不兼容。

CBC模式需要填充方案,但CFB模式不需要。.Net默认使用PKCS#7填充,但Go不提供任何帮助来添加或删除填充。你需要自己实现它。这个问题包含了PKCS#7填充的实现。

英文:

The default mode of operation in .Net is probably CBC (you should set it explicitly), but your Go code uses CFB mode. They are not compatible.

CBC mode requires a padding scheme, but CFB not. .Net uses PKCS#7 padding by default, but Go doesn't provide you with any help to add or remove padding. You would need to implement it yourself. This question contains an implementation of PKCS#7 padding.

huangapple
  • 本文由 发表于 2017年3月30日 20:14:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/43117572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定