June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
30
Task/Vigen-re-cipher/Factor/vigen-re-cipher.factor
Normal file
30
Task/Vigen-re-cipher/Factor/vigen-re-cipher.factor
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
USING: arrays ascii formatting kernel math math.functions
|
||||
math.order sequences ;
|
||||
IN: rosetta-code.vigenere-cipher
|
||||
|
||||
: mult-pad ( key input -- x )
|
||||
[ length ] bi@ 2dup < [ swap ] when / ceiling ;
|
||||
|
||||
: lengthen-pad ( key input -- rep-key input )
|
||||
[ mult-pad ] 2keep [ <repetition> concat ] dip
|
||||
[ length ] keep [ head ] dip ;
|
||||
|
||||
: normalize ( str -- only-upper-letters )
|
||||
>upper [ LETTER? ] filter ;
|
||||
|
||||
: vigenere-encrypt ( key input -- ecrypted )
|
||||
[ normalize ] bi@ lengthen-pad
|
||||
[ [ CHAR: A - ] map ] bi@ [ + 26 mod CHAR: A + ] 2map ;
|
||||
|
||||
: vigenere-decrypt ( key input -- decrypted )
|
||||
[ normalize ] bi@ lengthen-pad [ [ CHAR: A - ] map ] bi@
|
||||
[ - 26 - abs 26 mod CHAR: A + ] 2map ;
|
||||
|
||||
: main ( -- )
|
||||
"Vigenere cipher" dup
|
||||
"Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
2dup "Key: %s\nInput: %s\n" printf
|
||||
vigenere-encrypt dup "Encrypted: %s\n" printf
|
||||
vigenere-decrypt "Decrypted: %s\n" printf ;
|
||||
|
||||
MAIN: main
|
||||
Loading…
Add table
Add a link
Reference in a new issue