aes-256-cbc

command
v0.0.0-...-a2a1f02 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2020 License: MIT Imports: 7 Imported by: 0

README

aes-256-cbc example

AES-256 CBC (Cipher Block Chaining) mode where a block of plaintext is XORed with the previous cipherText block before being encrypted.

I have the following AES-256 mode examples,

GitHub Webpage

RUN

run aes-cbc.go

You output should be,

Original Text:           This is AES-256 CBC (32 Bytes)!!

The 32-byte Key:         myverystrongpasswordo32bitlength
The Nonce:               dcde73a548a63e3d0073ad870fd21d7b

Encrypted Text:          8eddd047a775fe81ce5343edb4d5684c16465192ee85f3765bbfb35ddb219e50
Decrypted Text:          This is AES-256 CBC (32 Bytes)!!

HOW IT WORKS

  • plainText should be multiple of AES block size, else padding may be used
  • The IV value should be equal to AES block size
  • Encryption but not message integrity

For simplicity I did not include the nonce in the cipherText.

Encryption,

// GET CIPHER BLOCK USING KEY
block, err := aes.NewCipher(keyByte)

// GET CBC ENCRYPTER
cbc := cipher.NewCBCEncrypter(block, nonce)

// ENCRYPT DATA
cbc.CryptBlocks(cipherTextByte, plaintextByte)

// RETURN HEX
cipherText := hex.EncodeToString(cipherTextByte)

Decryption,

// GET CIPHER BLOCK USING KEY
block, err := aes.NewCipher(keyByte)
checkErr(err)

// GET CBC DECRYPTER
cbc := cipher.NewCBCDecrypter(block, nonce)

// DECRYPT DATA
cbc.CryptBlocks(plainTextByte, cipherTextByte)

// RETURN STRING
plainText := string(plainTextByte[:])

This illustration may help,

IMAGE - aes-cbc - IMAGE

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL