Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Compression And Encryption Re:(2)

From: civicminded4:ya:hoo at: 13-Sep-2000 12:50

--0-1681692777-968874604=:23812 Content-Type: text/plain; charset=us-ascii Sorry there was a minor typo in the last script, use this one instead! rebol[ title: "String Encryption/Decryption Function (cipher)" version: 0.1.0 author: "alan parman" email: [aparman--mail--com] comment: {This encryption is only as good as your passphrase. Your passphrase may be upto 246 characters. The longer the passphrase, stronger the encryption} ] cipher: func [{Candidate Function to encrypt/decrypt a string. Uses CipherSaber (copyright Arnold Reinhold), a version of ARC4} string [string!] "string to en-decrypt" /decrypt "decrypt the string" /pass "supply a passphrase without being prompted" supplied-pass [string!] "pre-supplied passphrase" /local passphrase initvector plaintextlength ][ ; this function both encrypts and decrypts, for the purposes of this function what goes in is plaintext what comes out is ciphertext ;regardless if you are encrypting or decrypting ;series swap, define a swapping function, needed for encryption engine******* s-swap: func ["swaps the elements of two series at the given index positions" a [series!] {1st series} index-a [integer!] {index position to swap in 1st series} b [series!] {2nd series} index-b [integer!] {index position to swap in 2nd series} /local holder ] [ holder: pick a index-a poke a index-a pick b index-b poke b index-b holder ] ;***** begin true works of function here either decrypt [ plaintext: decompress load string ] [ plaintext: string ] ;*****make or extract initvector, then set plaintext length initvector: make block! 10 either decrypt [ for n 1 10 1 [ append initvector to-char (pick plaintext n) ] remove/part plaintext 10 ] [ random/seed to-integer checksum/secure form now ; reduces chances of deciphering the date from initvector loop 10 [ append initvector to-char ((random 256) - 1) ;REBOL's random does 1 to N ] ] plaintextlength: length? plaintext ;*****get passphrase either pass [ passphrase: supplied-pass ][ passphrase: "" ] while [any [ (length? passphrase) > 246 passphrase = "" ] ][ if (length? passphrase) > 246 [print "Passphrase must be 246 characters or less"] passphrase: ask/hide "passphrase :>> " ] ;*****make key key: make block! [] foreach letter passphrase [ append key letter ] append key initvector keylength: length? key ;*****make state, a block from 0 to 255, in order state: make block! 256 for i 0 255 1 [append state i] ;*****mix state ;*******try a cs2 type addition, mixing 3 times for number 1 3 1 [ j: 0 ; remember do 0 to 255, not 1 to 256 ;i will index state i.e. each element is state(i) ;n is the index to cycle thru the key (i modulo keylength) ;j is the "randomly" choosen element of state for i 0 255 1 [; must add + 1 to index i as the arrays are accessed 1 2 3 ... 256 ;i.e. first element "0" is element 1 not element 0 n: i // keylength state-i: to-integer pick state (i + 1) key-n: to-integer pick key (n + 1) j: (j + state-i + key-n) // 256 s-swap state (i + 1) state (j + 1) ] ;end of for ] ;***** end of 'for' of cs2 addition ;***** produce the ciphertext i: j: n: 0 ciphertext: make block! [] ; contents of file going out if (not decrypt) [ append ciphertext initvector ] for count 0 (plaintextlength - 1) 1 [ i: ((count + 1) // 256) ; this "+1" is shown, but not clearly, on the instructions ; this is _not_ an indexing adjustment, it is an integral part of the ciphersaber process state-i: pick state (i + 1) j: ((j + state-i) // 256) s-swap state (i + 1) state (j + 1) n: (((pick state (i + 1)) + (pick state (j + 1))) // 256) append ciphertext (xor (to-char pick state (n + 1)) (to-char pick plaintext (count + 1))) ] ;end of for either decrypt [ ciphertext: to-string ciphertext ][ ciphertext: form compress to-string ciphertext ] unset [plaintext] return ciphertext ] ; end cipher --0-1681692777-968874604=:23812 Content-Type: text/html; charset=us-ascii <P> <BR>Sorry there was a minor typo in the last script, use this one instead! <P>&nbsp; <P>rebol[<BR>title: "String Encryption/Decryption Function (cipher)"<BR>version: 0.1.0<BR>author: "alan parman"<BR>email: <A href="mailto:[aparman--mail--com]">[aparman--mail--com]</A><BR>comment: {This encryption is only as good as your passphrase.&nbsp; Your passphrase may be upto 246 characters. The longer the passphrase, stronger the encryption}<BR>] <P>cipher: func [{Candidate Function to encrypt/decrypt a string.<BR>&nbsp;Uses CipherSaber (copyright Arnold Reinhold), a version of ARC4}<BR>string [string!] "string to en-decrypt"<BR>/decrypt "decrypt the string"<BR>/pass "supply a passphrase without being prompted" <BR>supplied-pass [string!]&nbsp; "pre-supplied passphrase"<BR>/local passphrase initvector plaintextlength<BR>][<BR>; this function both encrypts and decrypts, for the purposes of this function what goes in is plaintext what comes out is ciphertext<BR>;regardless if you are encrypting or decrypting <P>;series swap, define a swapping function, needed for encryption engine*******<BR>s-swap: func ["swaps the elements of two series at the given index positions"<BR>&nbsp;a [series!] {1st series}<BR>&nbsp;index-a [integer!] {index position to swap in 1st series}<BR>&nbsp;b [series!] {2nd series}<BR>&nbsp;index-b [integer!] {index position to swap in 2nd series}<BR>&nbsp;/local holder<BR>] [<BR>&nbsp;holder: pick a index-a<BR>&nbsp;poke a index-a pick b index-b<BR>&nbsp;poke b index-b holder<BR>] <P>;***** begin true works of function here <P>either decrypt [<BR>&nbsp;plaintext: decompress load string<BR>] [<BR>&nbsp;plaintext: string<BR>] <P>&nbsp;;*****make or extract initvector,&nbsp; then set plaintext length<BR>&nbsp;initvector: make block! 10<BR>&nbsp;either decrypt&nbsp; [<BR>&nbsp;&nbsp;for n 1 10 1 [<BR>&nbsp;&nbsp;&nbsp;append initvector to-char (pick plaintext n)<BR>&nbsp;&nbsp;]<BR>&nbsp;&nbsp;remove/part plaintext 10<BR>&nbsp;] [<BR>&nbsp;&nbsp;random/seed to-integer checksum/secure form now ; reduces chances of deciphering the date from initvector<BR>&nbsp;&nbsp;loop 10 [<BR>&nbsp;&nbsp;&nbsp;append initvector to-char ((random 256) - 1) ;REBOL's random does 1 to N<BR>&nbsp;&nbsp;]<BR>&nbsp;] <P>&nbsp;plaintextlength: length? plaintext <P>&nbsp;;*****get passphrase<BR>&nbsp;either pass [<BR>&nbsp;&nbsp;passphrase: supplied-pass<BR>&nbsp;][<BR>&nbsp;&nbsp;passphrase: ""<BR>&nbsp;] <P>&nbsp;while [any [<BR>&nbsp;&nbsp;(length? passphrase) > 246<BR>&nbsp;&nbsp;passphrase = ""<BR>&nbsp;&nbsp;]<BR>&nbsp;][<BR>&nbsp;&nbsp;if (length? passphrase) > 246 [print "Passphrase must be 246 characters or less"]<BR>&nbsp;&nbsp;passphrase: ask/hide "passphrase :>> "<BR>&nbsp;] <P>&nbsp;;*****make key<BR>&nbsp;key: make block! []<BR>&nbsp;foreach letter passphrase [<BR>&nbsp;&nbsp;append key letter<BR>&nbsp;]<BR>&nbsp;append key initvector<BR>&nbsp;keylength: length? key <P>&nbsp;;*****make state, a block from 0 to 255, in order<BR>&nbsp;state: make block! 256<BR>&nbsp;for i 0 255 1 [append state i] <P>&nbsp;;*****mix state<BR>&nbsp;;*******try a cs2 type addition, mixing 3 times<BR>&nbsp;for number 1 3 1 [<BR>&nbsp;&nbsp;j: 0 ; remember do 0 to 255, not 1 to 256<BR>&nbsp;&nbsp;;i will index state i.e. each element is state(i)<BR>&nbsp;&nbsp;;n is the index to cycle thru the key (i modulo keylength)<BR>&nbsp;&nbsp;;j is the "randomly" choosen element of state <P>&nbsp;&nbsp;for i 0 255 1 [; must add + 1 to index i as the arrays are accessed 1 2 3 ... 256 <BR>&nbsp;&nbsp;&nbsp;;i.e. first element "0" is element 1 not element 0<BR>&nbsp;&nbsp;&nbsp;n: i // keylength <P>&nbsp;&nbsp;&nbsp;state-i: to-integer pick state (i + 1)<BR>&nbsp;&nbsp;&nbsp;key-n: to-integer pick key (n + 1) <P>&nbsp;&nbsp;&nbsp;j: (j + state-i + key-n) // 256 <P>&nbsp;&nbsp;&nbsp;s-swap state (i + 1) state (j + 1) <P>&nbsp;&nbsp;] ;end of for<BR>&nbsp;] ;***** end of 'for' of cs2 addition <P>&nbsp;;***** produce the ciphertext<BR>&nbsp;i: j: n: 0 <P>&nbsp;ciphertext: make block! [] ; contents of file going out <P>&nbsp;if (not decrypt) [<BR>&nbsp;&nbsp;append ciphertext initvector<BR>&nbsp;] <P>&nbsp;for count 0 (plaintextlength - 1) 1 [<BR>&nbsp;&nbsp;i: ((count + 1) // 256) ; this "+1" is shown, but not clearly, on the instructions<BR>&nbsp;&nbsp;; this is _not_ an indexing adjustment, it is an integral part of the ciphersaber process <P>&nbsp;&nbsp;state-i: pick state (i + 1) <P>&nbsp;&nbsp;j: ((j + state-i) // 256) <P>&nbsp;&nbsp;s-swap state (i + 1) state (j + 1) <P>&nbsp;&nbsp;n: (((pick state (i + 1)) + (pick state (j + 1))) // 256) <P>&nbsp;&nbsp;append ciphertext&nbsp; (xor (to-char pick state (n + 1)) (to-char pick plaintext (count + 1))) <P>&nbsp;] ;end of for <P><BR>&nbsp;either decrypt [<BR>&nbsp;&nbsp;ciphertext:&nbsp; to-string ciphertext&nbsp;&nbsp;<BR>&nbsp;][<BR>&nbsp;&nbsp;ciphertext: form compress to-string ciphertext<BR>&nbsp;]<BR>unset [plaintext]<BR>return ciphertext <P>] ; end cipher</P> --0-1681692777-968874604=:23812--