philanc / plc

Pure Lua Crypto

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Chacha20 problem

janicetr opened this issue · comments

The following snippet reads a file, encrypts and writes it back with .cha20 extension. Given a file for encryption, and the result back again for decryption, the original file content is truncated.

chacha20 = require "chacha20"
sha3 = require "sha3"

f = io.open(arg[1], "r")
plaintext = f:read("*all")
f:close()

key = sha3.hash256("Super Password 123 XXX")
nonce = "\x00\x00\x00\x00\x00\x00\x00\x4a\x00\x00\x00\x00"
assert(#key == chacha20.key_size)
assert(#nonce == chacha20.nonce_size)

counter = 1
ciphertext = chacha20.encrypt(key, counter, nonce, plaintext)

f = io.open(arg[1] .. ".cha20", "w")
f:write(ciphertext)
f:close()

LICENSE - 1070 bytes
LICENSE.cha20 - 1071 bytes
LICENSE.cha20.cha20 - 234 bytes

Solved. On Windows, binary files must be opened with the special flag.