Vb.net 到 golang 的 AES

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

Vb.net to golang AES

问题

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

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

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

VB代码:

  1. Function encryptString(ByVal source As String, ByVal key As Byte(), ByVal IV As Byte()) As Byte()
  2. Dim array As Byte() = Nothing
  3. Using aesManaged As System.Security.Cryptography.AesManaged = New System.Security.Cryptography.AesManaged()
  4. aesManaged.Key = key
  5. aesManaged.IV = IV
  6. Dim cryptoTransform As ICryptoTransform = aesManaged.CreateEncryptor(aesManaged.Key, aesManaged.IV)
  7. Using memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
  8. Using cryptoStream As System.Security.Cryptography.CryptoStream = New System.Security.Cryptography.CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write)
  9. Using streamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(cryptoStream)
  10. streamWriter.Write(source)
  11. End Using
  12. array = memoryStream.ToArray()
  13. End Using
  14. End Using
  15. End Using
  16. End Function

Go代码:

  1. func Encrypt(key string, plaintext string) string {
  2. block, err := aes.NewCipher([]byte(key))
  3. if err != nil {
  4. panic(err)
  5. }
  6. str := []byte(plaintext)
  7. iv := []byte{178, 212, 4, 97, 181, 77, 129, 200, 198, 223, 84, 96, 81, 55, 3, 40}
  8. encrypter := cipher.NewCFBEncrypter(block, iv)
  9. encrypted := make([]byte, len(str))
  10. encrypter.XORKeyStream(encrypted, str)
  11. fmt.Printf("'%s' encrypted to %x\n", str, encrypted)
  12. }

另外,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:

  1. Function encryptString(ByVal source As String, ByVal key As Byte(), ByVal IV As Byte()) As Byte()
  2. Dim array As Byte() = Nothing
  3. Using aesManaged As System.Security.Cryptography.AesManaged = New System.Security.Cryptography.AesManaged()
  4. aesManaged.Key = key
  5. aesManaged.IV = IV
  6. Dim cryptoTransform As ICryptoTransform = aesManaged.CreateEncryptor(aesManaged.Key, aesManaged.IV)
  7. Using memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
  8. Using cryptoStream As System.Security.Cryptography.CryptoStream = New System.Security.Cryptography.CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write)
  9. Using streamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(cryptoStream)
  10. streamWriter.Write(source)
  11. End Using
  12. array = memoryStream.ToArray()
  13. End Using
  14. End Using
  15. End Using

Go

  1. func Encrypt(key string, plaintext string) string {
  2. block, err := aes.NewCipher([]byte(key))
  3. if err != nil {
  4. panic(err)
  5. }
  6. str := []byte(plaintext)
  7. iv := []byte{178, 212, 4, 97, 181, 77, 129, 200, 198, 223, 84, 96, 81, 55, 3, 40}
  8. encrypter := cipher.NewCFBEncrypter(block, iv)
  9. encrypted := make([]byte, len(str))
  10. encrypter.XORKeyStream(encrypted, str)
  11. fmt.Printf("%s encrypted to %x\n", str, encrypted)
  12. }

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:

确定