up

Cipher

create new MCRYPT object

command:ns_mcrypt new algorithm_name mode_name
parameters:algorithm_name - name of cipher algorithm: des, blowfish, rijndael-256, ...
mode_name - name of cipher mode: cbc, ncfb, stream, ...
result:MCRYPT object using in other commands
example:set mcryptid [ns_mcrypt new "rijndael-256" "cbc"]

destroy MCRYPT object

command:ns_mcrypt destroy mcrypt_id
parameters:mcrypt_id - id of MCRYPT object generated by new command (above)
result:destroy MCRYPT object - close loaded module, freeing IV
example:ns_mcrypt destroy $mcryptid

initialize MCRYPT object

command:ns_mcrypt init mcrypt_id keyword
parameters: mcrypt_id - id of MCRYPT object generated by new command (above)
keyword - keyword/password using in cipher process
result:initialize MCRYPT object with keyword
example:ns_mcrypt init $mcryptid "foo"

flushing/freeing MCRYPT object

command:ns_mcrypt flush mcrypt_id
parameters: mcrypt_id - id of MCRYPT object generated by new command (above)
result:freeing internal buffer of MCRYPT object without changing of IV. MCRYPT object can be reusing in next process. IV is not change.
example:ns_mcrypt flush $mcryptid

crypt plaintext

command:ns_mcrypt crypt -in raw|hex|base64 -out raw|str|hex|base64 mcrypt_id plain_text
parameters: -in raw|hex|base64 - type of input format for plaintext :: hexadecimal string: -hex; raw string (binary, plain) (TclByteArray): -raw; binary base64 encoded: -base64.
-out raw|str|hex|base64 - type of output format for encrypted ciphertext :: hexadecimal string: -hex; raw string (binary) (TclByteArray): -raw; string (TclString): -str; binary base64 encoded: -base64.
mcrypt_id - id of MCRYPT object generated by new command (above)
plain_text - plaintext you wish to encrypt.
result:return ciphet text of plain_text
example:set ctxt [ns_mcrypt crypt -in raw -out hex $mcryptid "foofoo"]

decrypt ciphertext

command:ns_mcrypt decrypt -in raw|hex|base64 -out raw|str|hex|base64 mcrypt_id cipher_text
parameters: -in raw|hex|base64 - type of input format for ciphertext :: hexadecimal string: -hex; raw string (binary, plain) (TclByteArray): -raw; binary base64 encoded: -base64.
-out raw|str|hex|base64 - type of output format for decrypted plaintext :: hexadecimal string: -hex; raw string (binary) (TclByteArray): -raw; string (TclString): -str; binary base64 encoded: -base64.
mcrypt_id - id of MCRYPT object generated by new command (above)
cipher_text - ciphertext you wish to decrypt.
result:return ciphet text of plain_text
example:set ptxt [ns_mcrypt decrypt -in hex -out str $mcryptid $ctxt]

detach MCRYPT object

command:ns_mcrypt detach mcrypt_id
parameters:mcrypt_id - id of MCRYPT object generated by new command (above)
result:detach MCRYPT object from Tcl interpreter. You can use with nsv_* for share MCRYPT object.
example: set detachid [ns_mcrypt detach $mcryptid]
nsv_set test test $detachid

attach MCRYPT object

command:ns_mcrypt attach detach_hash_id
parameters:detach_hash_id - detached id of MCRYPT object generated by detach command (above)
result:attach MCRYPT object to Tcl interpreter for next using. You can use with nsv_* for share MCRYPT object.
example: set detachid [nsv_get test test]
set mcryptid [ns_mcrypt attach $detachid]

Full samples:

crypt plain text with base64 output cipher text and IV set as base64 string

set td [ns_mcrypt new "rijndael-256" "cbc"]
ns_mcrypt initvector set -base64 $td "MDEyMzQ1="
ns_mcrypt init $td "foo"
set ctxt [ns_mcrypt crypt -in raw -out base64 $td "foofoo"]
ns_mcrypt flush $td 
ns_mcrypt destroy $td
ns_adp_puts $ctxt
result: <% set td [ns_mcrypt new "rijndael-256" "cbc"] ns_mcrypt initvector set -base64 $td "MDEyMzQ1=" ns_mcrypt init $td "foo" set ctxt [ns_mcrypt crypt -in raw -out base64 $td "foofoo"] ns_mcrypt flush $td ns_mcrypt destroy $td ns_adp_puts $ctxt %>

crypt/decrypt plain text with and IV set randomly

set td [ns_mcrypt new "rijndael-256" "cbc"]
ns_mcrypt initvector set -random $td 
ns_mcrypt init $td "foo"
set ctxt [ns_mcrypt crypt -in raw -out raw $td "foofoo"]
ns_mcrypt flush $td 
ns_mcrypt init $td "foo"
set ptxt [ns_mcrypt decrypt -in raw -out str $td $ctxt]
ns_mcrypt destroy $td
ns_adp_puts $ptxt
result: <% set td [ns_mcrypt new "rijndael-256" "cbc"] ns_mcrypt initvector set -random $td ns_mcrypt init $td "foo" set ctxt [ns_mcrypt crypt -in raw -out raw $td "foofoo"] ns_mcrypt flush $td ns_mcrypt init $td "foo" set ptxt [ns_mcrypt decrypt -in raw -out str $td $ctxt] ns_mcrypt destroy $td ns_adp_puts $ptxt %>

crypt/decrypt plain text with and IV set as hexadecimal string and attach/detach MCRYPT object

set td [ns_mcrypt new "rijndael-256" "ncfb"]
ns_mcrypt initvector set -hex $td "ab325897ffde"
ns_mcrypt init $td "foo"
set ctxt [ns_mcrypt crypt -in raw -out hex $td "foofoo"]
ns_mcrypt flush $td 
ns_adp_puts "cipher text (hex string): $ctxt"
set detachid [ns_mcrypt detach $td]
nsv_set test test $detachid
set id [nsv_get test test]
set td [ns_mcrypt attach $id]
ns_mcrypt init $td "foo"
set ptxt [ns_mcrypt decrypt -in hex -out raw $td $ctxt]
ns_mcrypt destroy $td
ns_adp_puts plain decrypted text: $ptxt
result:
<% set td [ns_mcrypt new "rijndael-256" "ncfb"] ns_mcrypt initvector set -hex $td "ab325897ffde" ns_mcrypt init $td "foo" set ctxt [ns_mcrypt crypt -in raw -out hex $td "foofoo"] ns_mcrypt flush $td ns_adp_puts "cipher text (hex string): $ctxt
" set detachid [ns_mcrypt detach $td] nsv_set test test $detachid set id [nsv_get test test] set td [ns_mcrypt attach $id] ns_mcrypt init $td "foo" set ptxt [ns_mcrypt decrypt -in hex -out raw $td $ctxt] ns_mcrypt destroy $td ns_adp_puts "plain decrypted text: $ptxt" %>

crypt/decrypt plain text with and IV set randomly

set td [ns_mcrypt new "rijndael-256" "cfb"]
ns_mcrypt initvector set -random $td 
ns_mcrypt init $td "foo"
set ctxt ""
set buf [ns_mcrypt crypt -in raw -out hex $td "foo1"]
append ctxt $buf
set buf [ns_mcrypt crypt -in raw -out hex $td "foo2"]
append ctxt $buf
set buf [ns_mcrypt crypt -in raw -out hex $td "foo3"]
append ctxt $buf
set buf [ns_mcrypt crypt -in raw -out hex $td "foo4"]
append ctxt $buf
ns_adp_puts "cipher text (hex string): $ctxt"
ns_mcrypt flush $td 
ns_mcrypt init $td "foo"
set ptxt [ns_mcrypt decrypt -in hex -out str $td $ctxt]
ns_mcrypt destroy $td
ns_adp_puts "plain decrypted text: $ptxt"
result: <% set td [ns_mcrypt new "rijndael-256" "cfb"] ns_mcrypt initvector set -random $td ns_mcrypt init $td "foo" set ctxt "" set buf [ns_mcrypt crypt -in raw -out hex $td "foo1"] append ctxt $buf set buf [ns_mcrypt crypt -in raw -out hex $td "foo2"] append ctxt $buf set buf [ns_mcrypt crypt -in raw -out hex $td "foo3"] append ctxt $buf set buf [ns_mcrypt crypt -in raw -out hex $td "foo4"] append ctxt $buf ns_adp_puts "cipher text (hex string): $ctxt
" ns_mcrypt flush $td ns_mcrypt init $td "foo" set ptxt [ns_mcrypt decrypt -in hex -out str $td $ctxt] ns_mcrypt destroy $td ns_adp_puts "plain decrypted text: $ptxt" %>