SoFunction
Updated on 2025-03-02

Go implements AES encryption and writes a command line application

What is AES

For more knowledge about AES, please make up for it yourself. Advanced Encryption Standard (AES), also known as Rijndael encryption method, is a block encryption standard that is often used.

Go implements AES encryption

Golang's standard library aes can implement AES encryption. The official standard library aes document link: /crypto/aes

Small case requirements

This article shares the actual needs in actual work. The requirements are very simple, which is to implement a command line application that can encrypt incoming plain text strings and decrypt incoming ciphertext. The command line application is called passctl and has a help function. There are many powerful third-party libraries to implement command line applications. Because the requirements are too simple, this article just uses the Os in the standard library.

Actual combat

Encryption code

package main

import (
 "bytes"
 "crypto/aes"
 "crypto/cipher"
 "encoding/base64"
 "fmt"
)

var EncKey = []byte("QAZWSXEDCRFVTGBY") // 16-bit password string
func pkcs7Padding(data []byte, blockSize int) []byte {
 padding := blockSize - len(data)%blockSize
 padText := ([]byte{byte(padding)}, padding)
 return append(data, padText...)
}

func AesEncrypt(data []byte, key []byte) ([]byte, error) {
 block, err := (key)
 if err != nil {
  return nil, err
 }
 blockSize := ()
 encryptBytes := pkcs7Padding(data, blockSize)
 crypted := make([]byte, len(encryptBytes))
 blockMode := (block, key[:blockSize])
 (crypted, encryptBytes)
 return crypted, nil
}

func EncryptByAes(data []byte) (string, error) {
 res, err := AesEncrypt(data, EncKey)
 if err != nil {
  return "", err
 }
 return (res), nil
}

func main() {
 plaintext := "2wsx$RFV!Qaz" // Assume this is a plain text password p := []byte(plaintext)
 newp, _ := EncryptByAes(p) // Start encryption (newp)
}

Decrypt the code

Decrypt the password based on the above encrypted password.

package main

import (
 "crypto/aes"
 "crypto/cipher"
 "encoding/base64"
 "errors"
 "fmt"
)

var EncKey = []byte("QAZWSXEDCRFVTGBY") // 16-bit password string
func pkcs7UnPadding(data []byte) ([]byte, error) {
 length := len(data)
 if length == 0 {
  return nil, ("Sorry, the encryption string is wrong.")
 }
 unPadding := int(data[length-1])
 return data[:(length - unPadding)], nil
}

func AesDecrypt(data []byte, key []byte) ([]byte, error) {
 block, err := (key)
 if err != nil {
  return nil, err
 }
 blockSize := ()
 blockMode := (block, key[:blockSize])
 crypted := make([]byte, len(data))
 (crypted, data)
 crypted, err = pkcs7UnPadding(crypted)
 if err != nil {
  return nil, err
 }
 return crypted, nil
}

func DecryptByAes(data string) ([]byte, error) {
 dataByte, err := (data)
 if err != nil {
  return nil, err
 }
 return AesDecrypt(dataByte, EncKey)
}

func main() {
 ciphertext := "+LxjKS8N+Kpy/HNxsSJMIw==" // ciphertext pwd, _ := DecryptByAes(ciphertext)       // Start decryption (string(pwd))
}

Implement the passctl command line application

Code

package main

import (
 "bytes"
 "crypto/aes"
 "crypto/cipher"
 "encoding/base64"
 "errors"
 "fmt"
 "os"
)

var EncKey = []byte("QAZWSXEDCRFVTGBY") // 16-bit password string
func pkcs7Padding(data []byte, blockSize int) []byte {
 padding := blockSize - len(data)%blockSize
 padText := ([]byte{byte(padding)}, padding)
 return append(data, padText...)
}

func pkcs7UnPadding(data []byte) ([]byte, error) {
 length := len(data)
 if length == 0 {
  return nil, ("Sorry, the encryption string is wrong.")
 }
 unPadding := int(data[length-1])
 return data[:(length - unPadding)], nil
}

func AesEncrypt(data []byte, key []byte) ([]byte, error) {
 block, err := (key)
 if err != nil {
  return nil, err
 }
 blockSize := ()
 encryptBytes := pkcs7Padding(data, blockSize)
 crypted := make([]byte, len(encryptBytes))
 blockMode := (block, key[:blockSize])
 (crypted, encryptBytes)
 return crypted, nil
}

func AesDecrypt(data []byte, key []byte) ([]byte, error) {
 block, err := (key)
 if err != nil {
  return nil, err
 }
 blockSize := ()
 blockMode := (block, key[:blockSize])
 crypted := make([]byte, len(data))
 (crypted, data)
 crypted, err = pkcs7UnPadding(crypted)
 if err != nil {
  return nil, err
 }
 return crypted, nil
}

func EncryptByAes(data []byte) (string, error) {
 res, err := AesEncrypt(data, EncKey)
 if err != nil {
  return "", err
 }
 return (res), nil
}

func DecryptByAes(data string) ([]byte, error) {
 dataByte, err := (data)
 if err != nil {
  return nil, err
 }
 return AesDecrypt(dataByte, EncKey)
}

const help = `
Help description of encryption and decryption command line application

-h --help [Display help]
-e --encryption Plaintext string encryption
-d --decrypt Ciphertext string decryption

Example:

1. encryption example:
passctl -e "your plaintext password"

2. decryption example:
passctl -d "Your ciphertext string"

`

func main() {
 args := [1]
 if args == "-h" || args == "--help" {
  (help)
 } else if args == "-e" || args == "--encryption" {
  plaintext := [2]
  p := []byte(plaintext)
  newp, _ := EncryptByAes(p)
  (newp)
 } else if args == "-d" || args == "--decrypt" {
  ciphertext := [2]
  a, _ := DecryptByAes(ciphertext)
  (string(a))
 } else {
  ("Invalid option")
 }
}

Use after compiling into binary

# Compilation[root@devhost encryptionDecryption]# go build -o passctl 

# View Help[root@devhost encryptionDecryption]# ./passctl -h

Help description of encryption and decryption command line application

-h --help [Display help]
-e --encryption Plaintext string encryption
-d --decrypt Ciphertext string decryption

Example:

1. encryption example:
passctl -e "your plaintext password"

2. decryption example:
passctl -d "Your ciphertext string"

# Encryption[root@devhost encryptionDecryption]# ./passctl -e abc123456
nGi3ls+2yghdv7o8Ly2Z+A==

# Decryption[root@devhost encryptionDecryption]# ./passctl -d nGi3ls+2yghdv7o8Ly2Z+A==
abc123456
[root@devhost encryptionDecryption]# 

This is the article about Go implementing AES encryption and writing a command line application. For more related Go language AES encryption content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!