[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>
<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. 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> 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!] "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> a [series!]
{1st series}<BR> index-a [integer!] {index position to swap in 1st series}<BR> b
[series!] {2nd series}<BR> index-b [integer!] {index position to swap in 2nd series}<BR> /local
holder<BR>] [<BR> holder: pick a index-a<BR> poke a index-a pick b index-b<BR> poke
b index-b holder<BR>]
<P>;***** begin true works of function here
<P>either decrypt [<BR> plaintext: decompress load string<BR>] [<BR> plaintext:
string<BR>]
<P> ;*****make or extract initvector, then set plaintext length<BR> initvector:
make block! 10<BR> either decrypt [<BR> for n 1 10 1 [<BR> append
initvector to-char (pick plaintext n)<BR> ]<BR> remove/part plaintext
10<BR> ] [<BR> random/seed to-integer checksum/secure form now ; reduces
chances of deciphering the date from initvector<BR> loop 10 [<BR> append
initvector to-char ((random 256) - 1) ;REBOL's random does 1 to N<BR> ]<BR> ]
<P> plaintextlength: length? plaintext
<P> ;*****get passphrase<BR> either pass [<BR> passphrase: supplied-pass<BR> ][<BR> passphrase:
""<BR> ]
<P> while [any [<BR> (length? passphrase) > 246<BR> passphrase
= ""<BR> ]<BR> ][<BR> if (length? passphrase) > 246 [print
"Passphrase must be 246 characters or less"]<BR> passphrase: ask/hide "passphrase
:>> "<BR> ]
<P> ;*****make key<BR> key: make block! []<BR> foreach letter passphrase
[<BR> append key letter<BR> ]<BR> append key initvector<BR> keylength:
length? key
<P> ;*****make state, a block from 0 to 255, in order<BR> state: make block!
256<BR> for i 0 255 1 [append state i]
<P> ;*****mix state<BR> ;*******try a cs2 type addition, mixing 3 times<BR> for
number 1 3 1 [<BR> j: 0 ; remember do 0 to 255, not 1 to 256<BR> ;i
will index state i.e. each element is state(i)<BR> ;n is the index to cycle
thru the key (i modulo keylength)<BR> ;j is the "randomly" choosen element
of state
<P> for i 0 255 1 [; must add + 1 to index i as the arrays are accessed 1
2 3 ... 256 <BR> ;i.e. first element "0" is element 1 not element 0<BR> n:
i // keylength
<P> state-i: to-integer pick state (i + 1)<BR> key-n:
to-integer pick key (n + 1)
<P> j: (j + state-i + key-n) // 256
<P> s-swap state (i + 1) state (j + 1)
<P> ] ;end of for<BR> ] ;***** end of 'for' of cs2 addition
<P> ;***** produce the ciphertext<BR> i: j: n: 0
<P> ciphertext: make block! [] ; contents of file going out
<P> if (not decrypt) [<BR> append ciphertext initvector<BR> ]
<P> for count 0 (plaintextlength - 1) 1 [<BR> i: ((count + 1) // 256)
; this "+1" is shown, but not clearly, on the instructions<BR> ; this is _not_
an indexing adjustment, it is an integral part of the ciphersaber process
<P> state-i: pick state (i + 1)
<P> j: ((j + state-i) // 256)
<P> s-swap state (i + 1) state (j + 1)
<P> n: (((pick state (i + 1)) + (pick state (j + 1))) // 256)
<P> append ciphertext (xor (to-char pick state (n + 1)) (to-char pick
plaintext (count + 1)))
<P> ] ;end of for
<P><BR> either decrypt [<BR> ciphertext: to-string ciphertext <BR> ][<BR> ciphertext:
form compress to-string ciphertext<BR> ]<BR>unset [plaintext]<BR>return ciphertext
<P>] ; end cipher</P>
--0-1681692777-968874604=:23812--