Skip to content

Instantly share code, notes, and snippets.

@buzzer-re
Last active December 21, 2020 20:41
Show Gist options
  • Select an option

  • Save buzzer-re/df95922332ebe2c3ea3c444c9cf67c07 to your computer and use it in GitHub Desktop.

Select an option

Save buzzer-re/df95922332ebe2c3ea3c444c9cf67c07 to your computer and use it in GitHub Desktop.
Simple AES CBC encryptor and decryptor in Golang
package main
import (
"os"
"flag"
"fmt"
"syscall"
"io/ioutil"
"crypto/rand"
"crypto/cipher"
"crypto/aes"
"encoding/hex"
)
var encrypt, decrypt, key, iv string
func init() {
flag.StringVar(&encrypt, "e", "", "Encrypt the file")
flag.StringVar(&decrypt, "d", "", "Decrypt the file")
flag.StringVar(&key, "k", "", "Key used to encrypt")
flag.StringVar(&iv, "i", "", "IV used to CBC")
flag.Parse()
}
func readFileBytes(filePath string) ([]byte) {
data, err := ioutil.ReadFile(filePath)
checkErr(err)
return data
}
func writeFileBytes(filePath string, data []byte) {
ioutil.WriteFile(filePath, data, 0644)
}
func encryptAes(input []byte , key []byte , iv []byte ) ([]byte) {
block, err := aes.NewCipher(key)
checkErr(err)
for i := len(input); len(input) % aes.BlockSize != 0; i++ {
input = append(input, 0)
}
cbcEncryptor := cipher.NewCBCEncrypter(block, iv)
ciphered := make([]byte, aes.BlockSize + len(input))
cbcEncryptor.CryptBlocks(ciphered, input)
return ciphered
}
func decryptAes(input []byte, key []byte, iv []byte) ([]byte) {
block, err := aes.NewCipher(key)
checkErr(err)
cbcDecryptor := cipher.NewCBCDecrypter(block, iv)
cbcDecryptor.CryptBlocks(input, input)
return input;
}
func genRandomValue(size uint32) ([]byte) {
v := make([]byte, size)
rand.Read(v)
return v
}
func main() {
if len(os.Args) < 2 {
flag.Usage()
os.Exit(1)
}
var filePath string
var fileBytes []byte
if encrypt != "" {
filePath = encrypt
checkAccess(filePath)
var randomKey []byte = genRandomValue(16)
var iv []byte = genRandomValue(16)
fileBytes = readFileBytes(filePath)
encryptedInput := encryptAes(fileBytes, randomKey, iv)
writeFileBytes(filePath + ".aes", encryptedInput)
fmt.Printf("File encrypted\nKey: %x\nIV: %x\n", randomKey, iv)
} else if decrypt != "" && iv != "" && key != "" {
filePath = decrypt
checkAccess(filePath)
encodedKey, err := hex.DecodeString(key)
checkErr(err)
encodedIv, err := hex.DecodeString(iv)
checkErr(err)
fmt.Printf("Attempt to decrypt %s with key %x and iv %x...\n", filePath, encodedKey, encodedIv)
fileBytes = readFileBytes(filePath)
decryptedFile := decryptAes(fileBytes, encodedKey, encodedIv)
writeFileBytes(filePath + ".dec", decryptedFile)
} else {
flag.Usage()
}
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func checkAccess(filePath string) {
if err := syscall.Access(filePath, syscall.O_RDONLY); err != nil {
fmt.Fprintf(os.Stderr, "Unable to open file %s", filePath)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment