Posted to tcl by patthoyts at Mon Feb 26 23:43:52 GMT 2007view raw

  1. # Requires some reliable and secret method to exchange the secret.
  2. package require blowfish
  3. package require base64
  4. package require md5
  5.  
  6. proc CreateKey {secret} {
  7. return [blowfish::Init cbc $secret [string repeat \0 8]]
  8. }
  9.  
  10. proc Enc {key data} {
  11. # shoud be the best random data you can get.
  12. set iv [string range [md5::md5 [clock clicks]:[pid]:[expr {rand()}]] 0 7]
  13. blowfish::Reset $key $iv
  14. set c [blowfish::Encrypt $key $data]
  15. return [base64::encode $iv$c]
  16. }
  17.  
  18. proc Dec {key pkt} {
  19. set data [base64::decode $pkt]
  20. set iv [string range $data 0 7]
  21. set data [string range $data 8 end]
  22. blowfish::Reset $key $iv
  23. return [blowfish::Decrypt $key $data]
  24. }