Search code examples
goencryptionaes

Is Go only encrypt text in a 16-byte message length?


I try to encrypt a message using AES in Golang.

func main() {
    key := "mysupersecretkey32bytecharacters"
    plainText := "thisismyplaintextingolang"

    fmt.Println("My Encryption")
    byteCipherText := encrypt([]byte(key), []byte(plainText))
    fmt.Println(byteCipherText)
}

func encrypt(key, plaintext []byte) []byte {
    cphr, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    ciphertext := make([]byte, len(plaintext))
    cphr.Encrypt(ciphertext, plaintext)
    return ciphertext
}

That function returns : [23 96 11 10 70 223 95 118 157 250 80 92 77 26 137 224 0 0 0 0 0 0 0 0 0]

In that result, there are only 16 non-zero byte values. This means that AES encryption in Go only encrypts 16 characters. Is it possible to encrypts more than 16 characters in Go AES without using any mode in AES (like GCM, CBC, CFB, ..etc), just pure AES?


Solution

  • aes.NewCipher returns an instance of cipher.Block, which encrypts in blocks of 16 bytes (this is how pure AES works).

    mode of operation literally determines how messages longer than 16 bytes are encrypted. The simplest one is ECB (a "no-op" mode), which simply repeats the encryption in blocks of 16 bytes using the same key. You can do the same using a simple for-loop, though keep in mind that ECB is not very secure.