aes-256-ctr

command
Version: 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-ctr example

AES-256 CTR (Counter) mode, which has similar characteristics to OFB, but also allows a random access property during decryption.

I have the following AES-256 mode examples,

GitHub Webpage

RUN

run aes-ctr.go

You output should be,

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

The 32-byte Key:         myverystrongpasswordo32bitlength
The Nonce:               25ce7cd9388daf451864bb2212b9fbc7

Encrypted Text:          17b697b077d086692632adaae6fc935b245060ed3758fd3d936e4aaf81f31161
Decrypted Text:          This is AES-256 CTR (32 Bytes)!!

HOW IT WORKS

  • The Counter (CTR) mode has similar characteristics to OFB
  • Allows a random access property during decryption
  • Well suited to operate on a multi-processor machine where blocks can be encrypted in parallel
  • plaintext should be multiple of AES block size, hence padding would have to be added when encrypting (Not in this example)
  • Because of the symmetry of the XOR operation, encryption and decryption are exactly the same.

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

Encryption,

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

// GET CTR
ctr := cipher.NewCTR(block, nonce)

// ENCRYPT DATA
ctr.XORKeyStream(cipherTextByte, plainTextByte)

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

Decryption,

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

// GET CTR
ctr := cipher.NewCTR(block, nonce)

// DECRYPT DATA
ctr.XORKeyStream(plainTextByte, cipherTextByte)

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

This illustration may help,

IMAGE - aes-ctr - IMAGE

Documentation

The Go Gopher

There is no documentation for this package.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL