Skip to content

Instantly share code, notes, and snippets.

@byt3bl33d3r
Last active December 18, 2023 19:10
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save byt3bl33d3r/57fe809946b897110e3f5a8eb4a44fd2 to your computer and use it in GitHub Desktop.
Save byt3bl33d3r/57fe809946b897110e3f5a8eb4a44fd2 to your computer and use it in GitHub Desktop.
Nim AES256 Encryption/Decryption
#[
Author: Marcello Salvati, Twitter: @byt3bl33d3r
License: BSD 3-Clause
AES256-CTR Encryption/Decryption
]#
import nimcrypto
import nimcrypto/sysrand
import base64
func toByteSeq*(str: string): seq[byte] {.inline.} =
## Converts a string to the corresponding byte sequence.
@(str.toOpenArrayByte(0, str.high))
var
data: seq[byte] = toByteSeq(decode("SGVsbG8gV29ybGQ="))
envkey: string = "TARGETDOMAIN"
ectx, dctx: CTR[aes256]
key: array[aes256.sizeKey, byte]
iv: array[aes256.sizeBlock, byte]
plaintext = newSeq[byte](len(data))
enctext = newSeq[byte](len(data))
dectext = newSeq[byte](len(data))
# Create Random IV
discard randomBytes(addr iv[0], 16)
copyMem(addr plaintext[0], addr data[0], len(data))
# Expand key to 32 bytes using SHA256 as the KDF
var expandedkey = sha256.digest(envkey)
copyMem(addr key[0], addr expandedkey.data[0], len(expandedkey.data))
ectx.init(key, iv)
ectx.encrypt(plaintext, enctext)
ectx.clear()
dctx.init(key, iv)
dctx.decrypt(enctext, dectext)
dctx.clear()
echo "IV: ", toHex(iv)
echo "PLAINTEXT: ", toHex(plaintext)
echo "ENCRYPTED TEXT: ", toHex(enctext)
echo "DECRYPTED TEXT: ", toHex(dectext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment