user3358392
user3358392

Reputation: 17

Vb.net to golang AES

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...

Upvotes: 0

Views: 383

Answers (1)

Artjom B.
Artjom B.

Reputation: 61892

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.

Upvotes: 1

Related Questions