Go provides crypto/aes packages in the standard library to support AES encryption and decryption. Here is a sample code for encryption and decryption using AES-128-CBC mode:
package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []byte("this is a 16 byte key") iv := []byte("this is a 16 byte iv") plaintext := []byte("hello world") // Encryption ciphertext, err := encrypt(plaintext, key, iv) if err != nil { panic(err) } ("Encryption result: %s\n", (ciphertext)) // Decrypt decrypted, err := decrypt(ciphertext, key, iv) if err != nil { panic(err) } ("Decryption result: %s\n", decrypted) } func encrypt(plaintext []byte, key []byte, iv []byte) ([]byte, error) { block, err := (key) if err != nil { return nil, err } ciphertext := make([]byte, len(plaintext)) mode := (block, iv) (ciphertext, plaintext) return ciphertext, nil } func decrypt(ciphertext []byte, key []byte, iv []byte) ([]byte, error) { block, err := (key) if err != nil { return nil, err } plaintext := make([]byte, len(ciphertext)) mode := (block, iv) (plaintext, ciphertext) return plaintext, nil }
The above code is encrypted and decrypted using AES-128-CBC mode, and can be replaced with other AES modes, such as AES-192-CBC or AES-256-CBC, etc., and you only need to change the key length. Note that the ciphertext is formatted using base64 encoding in this example code. If you need to use binary ciphertext directly, please ignore the base64 encoding part.
During the AES encryption and decryption process, some parameters need to be used. The following is an explanation of the functions of these parameters:
(Plain text): The original data that needs to be encrypted.
(ciphertext): encrypted data.
(Key): The key used for encryption and decryption, which can be 16, 24 or 32 bytes in length (i.e. 128 bits, 192 bits, or 256 bits).
(Initialization vector): A fixed-length random number used to enhance the strength of AES encryption. The length of the IV is usually 16 bytes (i.e. 128 bits), and it must be used with the key.
(block): The basic unit of AES encryption and decryption, its size is related to the length of the key. For example, when using a 128-bit key, the size of the block is 128-bit (i.e., 16 bytes).
(Mode): AES encryption can use multiple modes, such as ECB, CBC, CFB, OFB, etc. Each mode has its specific encryption rules and advantages and disadvantages.
(Fill): Since AES encrypted block size is usually 128 bits, and the length of plaintext may not be an integer multiple of the block size, fill is required. Common filling methods are PKCS#5 and PKCS#7, which can ensure that the length of the plaintext is an integer multiple of the block size.
The above are some important parameters and concepts in AES encryption and decryption. Understanding these parameters and concepts can help you better understand the process and implementation of AES encryption and decryption.
This is the end of this article about the example code of Go using AES encryption and decryption. For more related Go AES encryption and decryption content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!