Posted to tcl by rhobart at Wed Aug 27 20:15:43 GMT 2025view raw

  1. #!/usr/bin/env tclsh
  2. # filename: fileEncryptTest.tcl
  3.  
  4. # aes::aes automatically pads input with NUL characters so length is even multiple of 16
  5. # aes default is cbc
  6.  
  7. package require aes
  8.  
  9.  
  10. set filename "./tcl.pdf"
  11. set f [open $filename rb]
  12. #$f configure -translation binary
  13. set data [read $f]
  14. close $f
  15.  
  16. #Test sucessfully creates identical file in bytes
  17. set outfilename "./tcl.pdf.test"
  18. set outf [open $outfilename w+b]
  19. #$outf configure -translation binary
  20. puts -nonewline $outf $data
  21. close $outf
  22.  
  23. #encrypt file
  24. #use data that was read
  25. #set 32_byte_key [string repeat "h" 32]
  26. set 32_byte_key "thisrandompopple491!@?y0ycxvbure"
  27. set cipher [aes::aes -dir encrypt -key $32_byte_key $data]
  28. #now save to file
  29. set cipherfilename "./tcl.pdf.cipher"
  30. set cipher_f [open $cipherfilename w+b]
  31. puts -nonewline $cipher_f $cipher
  32. close $cipher_f
  33.  
  34. #decrypt file using encrypted file that was created
  35. set cipher_f_2 [open $cipherfilename rb]
  36. set encrypted_data [read $cipher_f_2]
  37. set deciphered_data [aes::aes -dir decrypt -key $32_byte_key $encrypted_data]
  38. set close $cipher_f_2
  39. #make the decrypted file
  40. set decrypt_pdf_filename "./tcl.decrypted.pdf"
  41. set decrypt_f [open $decrypt_pdf_filename w+b]
  42. puts $decrypt_f [string trimright $deciphered_data]
  43. close $decrypt_f

Add a comment

Please note that this site uses the meta tags nofollow,noindex for all pages that contain comments.
Items are closed for new comments after 1 week