Skip to content

Instantly share code, notes, and snippets.

@awadhwana
Last active April 19, 2024 07:31
Show Gist options
  • Save awadhwana/9c95377beba61293390c5fd23a3bb1df to your computer and use it in GitHub Desktop.
Save awadhwana/9c95377beba61293390c5fd23a3bb1df to your computer and use it in GitHub Desktop.
Golang: aes-256-cbc ecrypt/decrypt examples (with iv, blockSize)
package main
// Working example: https://goplay.space/#Sa7qCLm6w65
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
)
func main() {
key := "12345678901234567890123456789012"
iv := "1234567890123456"
plaintext := "abcdefghijklmnopqrstuvwxyzABCDEF"
fmt.Println("Data to encode: ", plaintext)
cipherText := fmt.Sprintf("%v", Ase256Encode(plaintext, key, iv, aes.BlockSize))
fmt.Println("Encode Result:\t", cipherText)
fmt.Println("Decode Result:\t", Ase256Decode(cipherText, key, iv))
}
func Ase256Encode(plaintext string, key string, iv string, blockSize int) string {
bKey := []byte(key)
bIV := []byte(iv)
bPlaintext := PKCS5Padding([]byte(plaintext), blockSize, len(plaintext))
block, err := aes.NewCipher(bKey)
if err != nil {
panic(err)
}
ciphertext := make([]byte, len(bPlaintext))
mode := cipher.NewCBCEncrypter(block, bIV)
mode.CryptBlocks(ciphertext, bPlaintext)
return hex.EncodeToString(ciphertext)
}
func Ase256Decode(cipherText string, encKey string, iv string) (decryptedString string) {
bKey := []byte(encKey)
bIV := []byte(iv)
cipherTextDecoded, err := hex.DecodeString(cipherText)
if err != nil {
panic(err)
}
block, err := aes.NewCipher(bKey)
if err != nil {
panic(err)
}
mode := cipher.NewCBCDecrypter(block, bIV)
mode.CryptBlocks([]byte(cipherTextDecoded), []byte(cipherTextDecoded))
return string(cipherTextDecoded)
}
func PKCS5Padding(ciphertext []byte, blockSize int, after int) []byte {
padding := (blockSize - len(ciphertext)%blockSize)
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
@daamanshah
Copy link

Do not forget to use PKCS5UnPadding() while decoding the string !!!!!!!!!!!!!!!!

func Ase256(plaintext string, key string, iv string, blockSize int) (string, error) {
    bKey := []byte(key)
    bIV := []byte(iv)
    bPlaintext := PKCS5Padding([]byte(plaintext), blockSize, len(plaintext))
    block, err := aes.NewCipher(bKey)
    if err != nil {
        return "", err
    }
    ciphertext := make([]byte, len(bPlaintext))
    mode := cipher.NewCBCEncrypter(block, bIV)
    mode.CryptBlocks(ciphertext, bPlaintext)
    return hex.EncodeToString(ciphertext), nil
}
func PKCS5UnPadding(src []byte) []byte {
    length := len(src)
    unpadding := int(src[length-1])
    return src[:(length - unpadding)]
}
func PKCS5Padding(ciphertext []byte, blockSize int, after int) []byte {
    padding := (blockSize - len(ciphertext)%blockSize)
    padtext := bytes.Repeat([]byte{byte(padding)}, padding)
    return append(ciphertext, padtext...)
}
func Ase256Decode(cipherText string, encKey string, iv string) (decryptedString string) {
    bKey := []byte(encKey)
    bIV := []byte(iv)
    cipherTextDecoded, err := hex.DecodeString(cipherText)
    if err != nil {
        panic(err)
    }
    block, err := aes.NewCipher(bKey)
    if err != nil {
        panic(err)
    }
    mode := cipher.NewCBCDecrypter(block, bIV)
    mode.CryptBlocks([]byte(cipherTextDecoded), []byte(cipherTextDecoded))
    return string(PKCS5UnPadding(cipherTextDecoded))
}

@KC6745
Copy link

KC6745 commented Sep 23, 2022

Hi all
I wanted to encrypt and decrypt using a key of length 43 which I am getting from hashicorp vault

https://goplay.space/#C3TFPoaDmeb

how can I make this work? can you pls suggest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment