Posted to tcl by rhobart at Wed Aug 27 20:15:43 GMT 2025view pretty
#!/usr/bin/env tclsh
# filename: fileEncryptTest.tcl
# aes::aes automatically pads input with NUL characters so length is even multiple of 16
# aes default is cbc
package require aes
set filename "./tcl.pdf"
set f [open $filename rb]
#$f configure -translation binary
set data [read $f]
close $f
#Test sucessfully creates identical file in bytes
set outfilename "./tcl.pdf.test"
set outf [open $outfilename w+b]
#$outf configure -translation binary
puts -nonewline $outf $data
close $outf
#encrypt file
#use data that was read
#set 32_byte_key [string repeat "h" 32]
set 32_byte_key "thisrandompopple491!@?y0ycxvbure"
set cipher [aes::aes -dir encrypt -key $32_byte_key $data]
#now save to file
set cipherfilename "./tcl.pdf.cipher"
set cipher_f [open $cipherfilename w+b]
puts -nonewline $cipher_f $cipher
close $cipher_f
#decrypt file using encrypted file that was created
set cipher_f_2 [open $cipherfilename rb]
set encrypted_data [read $cipher_f_2]
set deciphered_data [aes::aes -dir decrypt -key $32_byte_key $encrypted_data]
set close $cipher_f_2
#make the decrypted file
set decrypt_pdf_filename "./tcl.decrypted.pdf"
set decrypt_f [open $decrypt_pdf_filename w+b]
puts $decrypt_f [string trimright $deciphered_data]
close $decrypt_f