all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
4
Task/Vigen-re-cipher/0DESCRIPTION
Normal file
4
Task/Vigen-re-cipher/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
Implement a [[wp:Vigen%C3%A8re_cipher|Vigenère cypher]], both encryption and decryption. The program should handle keys and text of unequal length, and should capitalize everything and discard non-alphabetic characters. (If your program handles non-alphabetic characters in another way, make a note of it.)
|
||||
|
||||
See also:
|
||||
* [[Vigenère Cipher/Cryptanalysis]]
|
||||
2
Task/Vigen-re-cipher/1META.yaml
Normal file
2
Task/Vigen-re-cipher/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Encryption
|
||||
62
Task/Vigen-re-cipher/ALGOL-68/vigen-re-cipher.alg
Normal file
62
Task/Vigen-re-cipher/ALGOL-68/vigen-re-cipher.alg
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
STRING key := "";
|
||||
|
||||
PROC vigenere cipher = (REF STRING key)VOID:
|
||||
(
|
||||
FOR i FROM LWB key TO UPB key DO
|
||||
IF key[i] >= "A" AND key[i] <= "Z" THEN
|
||||
key +:= key[i] FI;
|
||||
IF key[i] >= "a" AND key[i] <= "z" THEN
|
||||
key +:= REPR(ABS key[i] + ABS"A" - ABS"a") FI
|
||||
OD
|
||||
);
|
||||
|
||||
PROC encrypt = (STRING text)STRING:
|
||||
(
|
||||
STRING out := "";
|
||||
|
||||
INT j := LWB text;
|
||||
FOR i FROM LWB text TO UPB text DO
|
||||
CHAR c := text[i];
|
||||
|
||||
IF c >= "a" AND c <= "z" THEN
|
||||
c := REPR(ABS c + (ABS"A" - ABS"a")) FI;
|
||||
IF c >= "A" AND c <= "Z" THEN
|
||||
out +:= REPR((ABS c + ABS key[j] - 2*ABS"A") MOD 26 + ABS"A");
|
||||
j := j MOD UPB key + 1
|
||||
FI
|
||||
OD;
|
||||
|
||||
out
|
||||
);
|
||||
|
||||
PROC decrypt = (STRING text)STRING:
|
||||
(
|
||||
STRING out;
|
||||
|
||||
INT j := LWB text;
|
||||
FOR i FROM LWB text TO UPB text DO
|
||||
CHAR c := text[i];
|
||||
|
||||
IF c >= "a" AND c <= "z" THEN
|
||||
c := REPR(ABS c + (ABS"A" - ABS"a")) FI;
|
||||
IF c >= "A" AND c <= "Z" THEN
|
||||
out +:= REPR((ABS c - ABS key[j] + 26) MOD 26 + ABS"A");
|
||||
j := j MOD UPB key + 1
|
||||
FI
|
||||
OD;
|
||||
|
||||
out
|
||||
);
|
||||
|
||||
main:
|
||||
(
|
||||
vigenere cipher(key:="VIGENERECIPHER");
|
||||
|
||||
STRING original := "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
STRING encrypted := encrypt(original);
|
||||
STRING decrypted := decrypt(encrypted);
|
||||
|
||||
print((original, new line));
|
||||
print(("Encrypted: ", encrypted, new line));
|
||||
print(("Decrypted: ", decrypted, new line))
|
||||
)
|
||||
63
Task/Vigen-re-cipher/Ada/vigen-re-cipher.ada
Normal file
63
Task/Vigen-re-cipher/Ada/vigen-re-cipher.ada
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Vignere_Cipher is
|
||||
|
||||
subtype Letter is Character range 'A' .. 'Z';
|
||||
subtype Lowercase is Character range 'a' .. 'z';
|
||||
|
||||
function "+"(X, Y: Letter) return Letter is
|
||||
begin
|
||||
return Character'Val( ( (Character'Pos(X)-Character'Pos('A'))
|
||||
+ (Character'Pos(Y)-Character'Pos('A')) ) mod 26
|
||||
+ Character'Pos('A'));
|
||||
end;
|
||||
|
||||
function Normalize(S: String) return String is
|
||||
-- removes all characters except for uppercase and lowercase letters
|
||||
-- replaces lowercase by uppercase letters
|
||||
Offset: Integer := Character'Pos('A') - Character'Pos('a');
|
||||
begin
|
||||
if S="" then
|
||||
return "";
|
||||
elsif S(S'First) in Letter then
|
||||
return S(S'First) & Normalize(S(S'First+1 .. S'Last));
|
||||
elsif S(S'First) in Lowercase then
|
||||
return (Character'Val(Character'Pos(S(S'First)) + Offset)
|
||||
& Normalize(S(S'First+1 .. S'Last)));
|
||||
else
|
||||
return Normalize(S(S'First+1 .. S'Last));
|
||||
end if;
|
||||
end Normalize;
|
||||
|
||||
function Encrypt(Key: String; Text: String) return String is
|
||||
Ciphertext: String(Text'Range);
|
||||
begin
|
||||
for I in Text'Range loop
|
||||
Ciphertext(I) := Text(I)
|
||||
+ Key(Key'First + ((I-Text'First) mod Key'Length));
|
||||
end loop;
|
||||
return Ciphertext;
|
||||
end Encrypt;
|
||||
|
||||
function Invert(Key: String) return String is
|
||||
Result: String(Key'Range);
|
||||
begin
|
||||
for I in Key'Range loop
|
||||
Result(I)
|
||||
:= Character'Val( 26 - (Character'Pos(Key(I))-Character'Pos('A'))
|
||||
+ Character'Pos('A') );
|
||||
end loop;
|
||||
return Result;
|
||||
end Invert;
|
||||
|
||||
use Ada.Text_IO;
|
||||
Input: String := Get_Line;
|
||||
Key: String := Normalize(Get_Line);
|
||||
Ciph: String := Encrypt(Key => Key, Text => Normalize(Input));
|
||||
|
||||
begin
|
||||
Put_Line("Input =" & Input);
|
||||
Put_Line("Key =" & Key);
|
||||
Put_Line("Ciphertext =" & Ciph);
|
||||
Put_Line("Decryption =" & Encrypt(Key => Invert(Key), Text => Ciph));
|
||||
end Vignere_Cipher;
|
||||
23
Task/Vigen-re-cipher/AutoHotkey/vigen-re-cipher.ahk
Normal file
23
Task/Vigen-re-cipher/AutoHotkey/vigen-re-cipher.ahk
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
Key = VIGENERECIPHER
|
||||
Text= Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
|
||||
|
||||
out := "Input =" text "`nkey =" key "`nCiphertext =" (c := VigenereCipher(Text, Key)) "`nDecrypted =" VigenereDecipher(c, key)
|
||||
MsgBox % clipboard := out
|
||||
|
||||
VigenereCipher(Text, Key){
|
||||
StringUpper, Text, Text
|
||||
Text := RegExReplace(Text, "[^A-Z]")
|
||||
Loop Parse, Text
|
||||
{
|
||||
a := Asc(A_LoopField) - Asc("A")
|
||||
b := Asc(SubStr(Key, 1+Mod(A_Index-1, StrLen(Key)), 1)) - Asc("A")
|
||||
out .= Chr(Mod(a+b,26)+Asc("A"))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
VigenereDecipher(Text, key){
|
||||
Loop Parse, key
|
||||
decoderKey .= Chr(26-(Asc(A_LoopField)-65)+65)
|
||||
return VigenereCipher(Text, decoderKey)
|
||||
}
|
||||
40
Task/Vigen-re-cipher/BBC-BASIC/vigen-re-cipher.bbc
Normal file
40
Task/Vigen-re-cipher/BBC-BASIC/vigen-re-cipher.bbc
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
key$ = "LEMON"
|
||||
plaintext$ = "ATTACK AT DAWN"
|
||||
ciphertext$ = FNencrypt(plaintext$, key$)
|
||||
PRINT "Key = """ key$ """"
|
||||
PRINT "Plaintext = """ plaintext$ """"
|
||||
PRINT "Ciphertext = """ ciphertext$ """"
|
||||
PRINT "Decrypted = """ FNdecrypt(ciphertext$, key$) """"
|
||||
END
|
||||
|
||||
DEF FNencrypt(plain$, key$)
|
||||
LOCAL i%, k%, n%, o$
|
||||
plain$ = FNupper(plain$)
|
||||
key$ = FNupper(key$)
|
||||
FOR i% = 1 TO LEN(plain$)
|
||||
n% = ASCMID$(plain$, i%)
|
||||
IF n% >= 65 IF n% <= 90 THEN
|
||||
o$ += CHR$(65 + (n% + ASCMID$(key$, k%+1)) MOD 26)
|
||||
k% = (k% + 1) MOD LEN(key$)
|
||||
ENDIF
|
||||
NEXT
|
||||
= o$
|
||||
|
||||
DEF FNdecrypt(cipher$, key$)
|
||||
LOCAL i%, k%, n%, o$
|
||||
cipher$ = FNupper(cipher$)
|
||||
key$ = FNupper(key$)
|
||||
FOR i% = 1 TO LEN(cipher$)
|
||||
n% = ASCMID$(cipher$, i%)
|
||||
o$ += CHR$(65 + (n% + 26 - ASCMID$(key$, k%+1)) MOD 26)
|
||||
k% = (k% + 1) MOD LEN(key$)
|
||||
NEXT
|
||||
= o$
|
||||
|
||||
DEF FNupper(A$)
|
||||
LOCAL A%,C%
|
||||
FOR A% = 1 TO LEN(A$)
|
||||
C% = ASCMID$(A$,A%)
|
||||
IF C% >= 97 IF C% <= 122 MID$(A$,A%,1) = CHR$(C%-32)
|
||||
NEXT
|
||||
= A$
|
||||
73
Task/Vigen-re-cipher/C++/vigen-re-cipher.cpp
Normal file
73
Task/Vigen-re-cipher/C++/vigen-re-cipher.cpp
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
class Vigenere
|
||||
{
|
||||
public:
|
||||
string key;
|
||||
|
||||
Vigenere(string key)
|
||||
{
|
||||
for(int i = 0; i < key.size(); ++i)
|
||||
{
|
||||
if(key[i] >= 'A' && key[i] <= 'Z')
|
||||
this->key += key[i];
|
||||
else if(key[i] >= 'a' && key[i] <= 'z')
|
||||
this->key += key[i] + 'A' - 'a';
|
||||
}
|
||||
}
|
||||
|
||||
string encrypt(string text)
|
||||
{
|
||||
string out;
|
||||
|
||||
for(int i = 0, j = 0; i < text.length(); ++i)
|
||||
{
|
||||
char c = text[i];
|
||||
|
||||
if(c >= 'a' && c <= 'z')
|
||||
c += 'A' - 'a';
|
||||
else if(c < 'A' || c > 'Z')
|
||||
continue;
|
||||
|
||||
out += (c + key[j] - 2*'A') % 26 + 'A';
|
||||
j = (j + 1) % key.length();
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
string decrypt(string text)
|
||||
{
|
||||
string out;
|
||||
|
||||
for(int i = 0, j = 0; i < text.length(); ++i)
|
||||
{
|
||||
char c = text[i];
|
||||
|
||||
if(c >= 'a' && c <= 'z')
|
||||
c += 'A' - 'a';
|
||||
else if(c < 'A' || c > 'Z')
|
||||
continue;
|
||||
|
||||
out += (c - key[j] + 26) % 26 + 'A';
|
||||
j = (j + 1) % key.length();
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Vigenere cipher("VIGENERECIPHER");
|
||||
|
||||
string original = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
string encrypted = cipher.encrypt(original);
|
||||
string decrypted = cipher.decrypt(encrypted);
|
||||
|
||||
cout << original << endl;
|
||||
cout << "Encrypted: " << encrypted << endl;
|
||||
cout << "Decrypted: " << decrypted << endl;
|
||||
}
|
||||
56
Task/Vigen-re-cipher/C/vigen-re-cipher-1.c
Normal file
56
Task/Vigen-re-cipher/C/vigen-re-cipher-1.c
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
void upper_case(char *src)
|
||||
{
|
||||
while (*src != '\0') {
|
||||
if (islower(*src)) *src &= ~0x20;
|
||||
src++;
|
||||
}
|
||||
}
|
||||
|
||||
char* encipher(const char *src, char *key, int is_encode)
|
||||
{
|
||||
int i, klen, slen;
|
||||
char *dest;
|
||||
|
||||
dest = strdup(src);
|
||||
upper_case(dest);
|
||||
upper_case(key);
|
||||
|
||||
/* strip out non-letters */
|
||||
for (i = 0, slen = 0; dest[slen] != '\0'; slen++)
|
||||
if (isupper(dest[slen]))
|
||||
dest[i++] = dest[slen];
|
||||
|
||||
dest[slen = i] = '\0'; /* null pad it, make it safe to use */
|
||||
|
||||
klen = strlen(key);
|
||||
for (i = 0; i < slen; i++) {
|
||||
if (!isupper(dest[i])) continue;
|
||||
dest[i] = 'A' + (is_encode
|
||||
? dest[i] - 'A' + key[i % klen] - 'A'
|
||||
: dest[i] - key[i % klen] + 26) % 26;
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const char *str = "Beware the Jabberwock, my son! The jaws that bite, "
|
||||
"the claws that catch!";
|
||||
const char *cod, *dec;
|
||||
char key[] = "VIGENERECIPHER";
|
||||
|
||||
printf("Text: %s\n", str);
|
||||
printf("key: %s\n", key);
|
||||
|
||||
cod = encipher(str, key, 1); printf("Code: %s\n", cod);
|
||||
dec = encipher(cod, key, 0); printf("Back: %s\n", dec);
|
||||
|
||||
/* free(dec); free(cod); */ /* nah */
|
||||
return 0;
|
||||
}
|
||||
4
Task/Vigen-re-cipher/C/vigen-re-cipher-2.c
Normal file
4
Task/Vigen-re-cipher/C/vigen-re-cipher-2.c
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
Text: Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
|
||||
key: VIGENERECIPHER
|
||||
Code: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
|
||||
Back: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
|
||||
29
Task/Vigen-re-cipher/Clojure/vigen-re-cipher-1.clj
Normal file
29
Task/Vigen-re-cipher/Clojure/vigen-re-cipher-1.clj
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
(ns org.rosettacode.clojure.vigenere
|
||||
(:require [clojure.string :as string]))
|
||||
|
||||
; convert letter to offset from \A
|
||||
(defn to-num [char] (- (int char) (int \A)))
|
||||
|
||||
; convert number to letter, treating it as modulo 26 offset from \A
|
||||
(defn from-num [num] (char (+ (mod num 26) (int \A))))
|
||||
|
||||
; Convert a string to a sequence of just the letters as uppercase chars
|
||||
(defn to-normalized-seq [str]
|
||||
(map #'first (re-seq #"[A-Z]" (string/upper-case str))))
|
||||
|
||||
; add (op=+) or subtract (op=-) the numerical value of the key letter from the
|
||||
; text letter.
|
||||
(defn crypt1 [op text key]
|
||||
(from-num (apply op (list (to-num text) (to-num key)))))
|
||||
|
||||
(defn crypt [op text key]
|
||||
(let [xcrypt1 (partial #'crypt1 op)]
|
||||
(apply #'str
|
||||
(map xcrypt1 (to-normalized-seq text)
|
||||
(cycle (to-normalized-seq key))))))
|
||||
|
||||
; encipher a text
|
||||
(defn encrypt [plaintext key] (crypt #'+ plaintext key))
|
||||
|
||||
; decipher a text
|
||||
(defn decrypt [ciphertext key] (crypt #'- ciphertext key))
|
||||
11
Task/Vigen-re-cipher/Clojure/vigen-re-cipher-2.clj
Normal file
11
Task/Vigen-re-cipher/Clojure/vigen-re-cipher-2.clj
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(ns org.rosettacode.clojure.test-vigenere
|
||||
(:require [org.rosettacode.clojure.vigenere :as vigenere]))
|
||||
|
||||
(let
|
||||
[ plaintext "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
key "Vigenere cipher"
|
||||
ciphertext (vigenere/encrypt plaintext key)
|
||||
recovered (vigenere/decrypt ciphertext key) ]
|
||||
|
||||
(doall (map (fn [[k v]] (printf "%9s: %s\n" k v))
|
||||
[ ["Original" plaintext] ["Key" key] ["Encrypted" ciphertext] ["Decrypted" recovered] ])))
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
# Simple helper since charCodeAt is quite long to write.
|
||||
code = (char) -> char.charCodeAt()
|
||||
|
||||
encrypt = (text, key) ->
|
||||
res = []
|
||||
j = 0
|
||||
|
||||
for c in text.toUpperCase()
|
||||
continue if c < 'A' or c > 'Z'
|
||||
|
||||
res.push ((code c) + (code key[j]) - 130) % 26 + 65
|
||||
j = ++j % key.length
|
||||
|
||||
String.fromCharCode res...
|
||||
|
||||
decrypt = (text, key) ->
|
||||
res = []
|
||||
j = 0
|
||||
|
||||
for c in text.toUpperCase()
|
||||
continue if c < 'A' or c > 'Z'
|
||||
|
||||
res.push ((code c) - (code key[j]) + 26) % 26 + 65
|
||||
j = ++j % key.length
|
||||
|
||||
String.fromCharCode res...
|
||||
|
||||
# Trying it out
|
||||
key = "VIGENERECIPHER"
|
||||
original = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
encrypted = encrypt original, key
|
||||
|
||||
console.log "Original : #{original}"
|
||||
console.log "Encrypted : #{encrypted}"
|
||||
console.log "Decrypted : #{decrypt encrypted, key}"
|
||||
26
Task/Vigen-re-cipher/Common-Lisp/vigen-re-cipher-1.lisp
Normal file
26
Task/Vigen-re-cipher/Common-Lisp/vigen-re-cipher-1.lisp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
(defun strip (s)
|
||||
(remove-if-not
|
||||
(lambda (c) (char<= #\A c #\Z))
|
||||
(string-upcase s)))
|
||||
|
||||
(defun vigenère (s key &key decipher
|
||||
&aux (A (char-code #\A))
|
||||
(op (if decipher #'- #'+)))
|
||||
(labels
|
||||
((to-char (c) (code-char (+ c A)))
|
||||
(to-code (c) (- (char-code c) A)))
|
||||
(let ((k (map 'list #'to-code (strip key))))
|
||||
(setf (cdr (last k)) k)
|
||||
(map 'string
|
||||
(lambda (c)
|
||||
(prog1
|
||||
(to-char
|
||||
(mod (funcall op (to-code c) (car k)) 26))
|
||||
(setf k (cdr k))))
|
||||
(strip s)))))
|
||||
|
||||
(let* ((msg "Beware the Jabberwock... The jaws that... the claws that catch!")
|
||||
(key "vigenere cipher")
|
||||
(enc (vigenère msg key))
|
||||
(dec (vigenère enc key :decipher t)))
|
||||
(format t "msg: ~a~%enc: ~a~%dec: ~a~%" msg enc dec))
|
||||
3
Task/Vigen-re-cipher/Common-Lisp/vigen-re-cipher-2.lisp
Normal file
3
Task/Vigen-re-cipher/Common-Lisp/vigen-re-cipher-2.lisp
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
msg: Beware the Jabberwock... The jaws that... the claws that catch!
|
||||
enc: WMCEEIKLGRPIFVMEUGXXYILILZXYVBZLRGCEYAIOEKXIZGU
|
||||
dec: BEWARETHEJABBERWOCKTHEJAWSTHATTHECLAWSTHATCATCH
|
||||
29
Task/Vigen-re-cipher/D/vigen-re-cipher.d
Normal file
29
Task/Vigen-re-cipher/D/vigen-re-cipher.d
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import std.stdio, std.string;
|
||||
|
||||
string encrypt(in string txt, in string key) pure
|
||||
in {
|
||||
assert(key.removechars("^A-Z") == key);
|
||||
} body {
|
||||
string res;
|
||||
foreach (immutable i, immutable c; toUpper(txt).removechars("^A-Z"))
|
||||
res ~= (c + key[i % $] - 2 * 'A') % 26 + 'A';
|
||||
return res;
|
||||
}
|
||||
|
||||
string decrypt(in string txt, in string key) pure
|
||||
in {
|
||||
assert(key.removechars("^A-Z") == key);
|
||||
} body {
|
||||
string res;
|
||||
foreach (immutable i, immutable c; toUpper(txt).removechars("^A-Z"))
|
||||
res ~= (c - key[i % $] + 26) % 26 + 'A';
|
||||
return res;
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable key = "VIGENERECIPHER";
|
||||
immutable original = "Beware the Jabberwock, my son!" ~
|
||||
" The jaws that bite, the claws that catch!";
|
||||
immutable encoded = original.encrypt(key);
|
||||
writeln(encoded, "\n", encoded.decrypt(key));
|
||||
}
|
||||
47
Task/Vigen-re-cipher/Erlang/vigen-re-cipher-1.erl
Normal file
47
Task/Vigen-re-cipher/Erlang/vigen-re-cipher-1.erl
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
% Erlang implementation of Vigenère cipher
|
||||
-module(vigenere).
|
||||
-export([encrypt/2, decrypt/2]).
|
||||
-import(lists, [append/2, filter/2, map/2, zipwith/3]).
|
||||
|
||||
% Utility functions for character tests and conversions
|
||||
isupper([C|_]) -> isupper(C);
|
||||
isupper(C) -> (C >= $A) and (C =< $Z).
|
||||
|
||||
islower([C|_]) -> islower(C);
|
||||
islower(C) -> (C >= $a) and (C =< $z).
|
||||
|
||||
isalpha([C|_]) -> isalpha(C);
|
||||
isalpha(C) -> isupper(C) or islower(C).
|
||||
|
||||
toupper(S) when is_list(S) -> lists:map(fun toupper/1, S);
|
||||
toupper(C) when (C >= $a) and (C =< $z) -> C - $a + $A;
|
||||
toupper(C) -> C.
|
||||
|
||||
% modulo function that normalizes into positive range for positive divisor
|
||||
mod(X,Y) -> (X rem Y + Y) rem Y.
|
||||
|
||||
% convert letter to position in alphabet (A=0,B=1,...,Y=24,Z=25).
|
||||
to_pos(L) when L >= $A, L =< $Z -> L - $A.
|
||||
|
||||
% convert position in alphabet back to letter
|
||||
from_pos(N) -> mod(N, 26) + $A.
|
||||
|
||||
% encode the given letter given the single-letter key
|
||||
encipher(P, K) -> from_pos(to_pos(P) + to_pos(K)).
|
||||
|
||||
% decode the given letter given the single-letter key
|
||||
decipher(C, K) -> from_pos(to_pos(C) - to_pos(K)).
|
||||
|
||||
% extend a list by repeating it until it is at least N elements long
|
||||
cycle_to(N, List) when length(List) >= N -> List;
|
||||
cycle_to(N, List) -> append(List, cycle_to(N-length(List), List)).
|
||||
|
||||
% Encryption prep: reduce string to only its letters, in uppercase
|
||||
normalize(Str) -> toupper(filter(fun isalpha/1, Str)).
|
||||
|
||||
crypt(RawText, RawKey, Func) ->
|
||||
PlainText = normalize(RawText),
|
||||
zipwith(Func, PlainText, cycle_to(length(PlainText), normalize(RawKey))).
|
||||
|
||||
encrypt(Text, Key) -> crypt(Text, Key, fun encipher/2).
|
||||
decrypt(Text, Key) -> crypt(Text, Key, fun decipher/2).
|
||||
7
Task/Vigen-re-cipher/Erlang/vigen-re-cipher-2.erl
Normal file
7
Task/Vigen-re-cipher/Erlang/vigen-re-cipher-2.erl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
-module(testvigenere).
|
||||
-import(vigenere,[encrypt/2, decrypt/2]).
|
||||
main(_) ->
|
||||
Key = "Vigenere cipher",
|
||||
CipherText = encrypt("Beware the Jabberwock, my son! The jaws that bite, the claws that catch!", Key),
|
||||
RecoveredText = decrypt(CipherText, Key),
|
||||
io:fwrite("Ciphertext: ~s~nDecrypted: ~s~n", [CipherText, RecoveredText]).
|
||||
69
Task/Vigen-re-cipher/Go/vigen-re-cipher.go
Normal file
69
Task/Vigen-re-cipher/Go/vigen-re-cipher.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type vkey string
|
||||
|
||||
func newVigenère(key string) (vkey, bool) {
|
||||
v := vkey(upperOnly(key))
|
||||
return v, len(v) > 0 // key length 0 invalid
|
||||
}
|
||||
|
||||
func (k vkey) encipher(pt string) string {
|
||||
ct := upperOnly(pt)
|
||||
for i, c := range ct {
|
||||
ct[i] = 'A' + (c-'A'+k[i%len(k)]-'A')%26
|
||||
}
|
||||
return string(ct)
|
||||
}
|
||||
|
||||
func (k vkey) decipher(ct string) (string, bool) {
|
||||
pt := make([]byte, len(ct))
|
||||
for i := range pt {
|
||||
c := ct[i]
|
||||
if c < 'A' || c > 'Z' {
|
||||
return "", false // invalid ciphertext
|
||||
}
|
||||
pt[i] = 'A' + (c-k[i%len(k)]+26)%26
|
||||
}
|
||||
return string(pt), true
|
||||
}
|
||||
|
||||
// upperOnly extracts letters A-Z, a-z from a string and
|
||||
// returns them all upper case in a byte slice.
|
||||
// Useful for vkey constructor and encipher function.
|
||||
func upperOnly(s string) []byte {
|
||||
u := make([]byte, 0, len(s))
|
||||
for i := 0; i < len(s); i++ {
|
||||
c := s[i]
|
||||
if c >= 'A' && c <= 'Z' {
|
||||
u = append(u, c)
|
||||
} else if c >= 'a' && c <= 'z' {
|
||||
u = append(u, c-32)
|
||||
}
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
const testKey = "Vigenère Cipher"
|
||||
const testPT = `Beware the Jabberwock, my son!
|
||||
The jaws that bite, the claws that catch!`
|
||||
|
||||
func main() {
|
||||
fmt.Println("Supplied key: ", testKey)
|
||||
v, ok := newVigenère(testKey)
|
||||
if !ok {
|
||||
fmt.Println("Invalid key")
|
||||
return
|
||||
}
|
||||
fmt.Println("Effective key:", v)
|
||||
fmt.Println("Plain text:", testPT)
|
||||
ct := v.encipher(testPT)
|
||||
fmt.Println("Enciphered:", ct)
|
||||
dt, ok := v.decipher(ct)
|
||||
if !ok {
|
||||
fmt.Println("Invalid ciphertext")
|
||||
return
|
||||
}
|
||||
fmt.Println("Deciphered:", dt)
|
||||
}
|
||||
26
Task/Vigen-re-cipher/Haskell/vigen-re-cipher.hs
Normal file
26
Task/Vigen-re-cipher/Haskell/vigen-re-cipher.hs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import Data.Char
|
||||
import Text.Printf
|
||||
|
||||
-- Perform encryption or decryption, depending on f.
|
||||
crypt f key = map toLetter . zipWith f (cycle key)
|
||||
where toLetter = chr . (+) (ord 'A')
|
||||
|
||||
-- Encrypt or decrypt one letter.
|
||||
enc k c = (ord k + ord c) `mod` 26
|
||||
dec k c = (ord c - ord k) `mod` 26
|
||||
|
||||
-- Given a key, encrypt or decrypt an input string.
|
||||
encrypt = crypt enc
|
||||
decrypt = crypt dec
|
||||
|
||||
-- Convert a string to have only upper case letters.
|
||||
convert = map toUpper . filter isLetter
|
||||
|
||||
main = do
|
||||
let key = "VIGENERECIPHER"
|
||||
text = "Beware the Jabberwock, my son! The jaws that bite, "
|
||||
++ "the claws that catch!"
|
||||
encr = encrypt key $ convert text
|
||||
decr = decrypt key encr
|
||||
printf " Input: %s\n Key: %s\nEncrypted: %s\nDecrypted: %s\n"
|
||||
text key encr decr
|
||||
24
Task/Vigen-re-cipher/Icon/vigen-re-cipher-1.icon
Normal file
24
Task/Vigen-re-cipher/Icon/vigen-re-cipher-1.icon
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
procedure main()
|
||||
ptext := "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
write("Key = ",ekey := "VIGENERECIPHER")
|
||||
write("Plain Text = ",ptext)
|
||||
write("Normalized = ",GFormat(ptext := NormalizeText(ptext)))
|
||||
write("Enciphered = ",GFormat(ctext := Vignere("e",ekey,ptext)))
|
||||
write("Deciphered = ",GFormat(ptext := Vignere("d",ekey,ctext)))
|
||||
end
|
||||
|
||||
procedure Vignere(mode,ekey,ptext,alpha) #: Vignere cipher
|
||||
/alpha := &ucase # default
|
||||
if *alpha ~= *cset(alpha) then runerr(205,alpha) # no dups
|
||||
alpha ||:= alpha # unobstructed
|
||||
|
||||
every ctext:="" & p:=ptext[i := 1 to *ptext] & k:=ekey[(i-1)%*ekey+1] do
|
||||
case mode of {
|
||||
"e"|"encrypt":
|
||||
ctext||:=map(p,alpha[1+:*alpha/2],alpha[find(k,alpha)+:(*alpha/2)])
|
||||
"d"|"decrypt":
|
||||
ctext||:=map(p,alpha[find(k,alpha)+:(*alpha/2)],alpha[1+:*alpha/2])
|
||||
default: runerr(205,mode)
|
||||
}
|
||||
return ctext
|
||||
end
|
||||
13
Task/Vigen-re-cipher/Icon/vigen-re-cipher-2.icon
Normal file
13
Task/Vigen-re-cipher/Icon/vigen-re-cipher-2.icon
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
link strings
|
||||
|
||||
procedure NormalizeText(ptext,alpha) #: text/case classical crypto helper
|
||||
/alpha := &ucase # default
|
||||
if &lcase === (alpha := cset(alpha)) then ptext := map(ptext) # lower
|
||||
if &ucase === alpha then ptext := map(ptext,&lcase,&ucase) # upper
|
||||
return deletec(ptext,&cset--alpha) # only alphas
|
||||
end
|
||||
|
||||
procedure GFormat(text) #: 5 letter group formatting helper
|
||||
text ? (s := "", until pos(0) do s ||:= " " || move(5)|tab(0))
|
||||
return s[2:0]
|
||||
end
|
||||
13
Task/Vigen-re-cipher/J/vigen-re-cipher-1.j
Normal file
13
Task/Vigen-re-cipher/J/vigen-re-cipher-1.j
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
NB.*vig c Vigenère cipher
|
||||
NB. cipher=. key 0 vig charset plain
|
||||
NB. plain=. key 1 vig charset cipher
|
||||
vig=: conjunction define
|
||||
:
|
||||
r=. (#y) $ n i.x
|
||||
n {~ (#n) | (r*_1^m) + n i.y
|
||||
)
|
||||
|
||||
ALPHA=: (65,:26) ];.0 a. NB. Character Set
|
||||
preprocess=: (#~ e.&ALPHA)@toupper NB. force uppercase and discard non-alpha chars
|
||||
vigEncryptRC=: 0 vig ALPHA preprocess
|
||||
vigDecryptRC=: 1 vig ALPHA preprocess
|
||||
4
Task/Vigen-re-cipher/J/vigen-re-cipher-2.j
Normal file
4
Task/Vigen-re-cipher/J/vigen-re-cipher-2.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
'VIGENERECIPHER' vigEncryptRC 'Beware the Jabberwock, my son! The jaws that bite, the claws that catch!'
|
||||
WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
|
||||
'VIGENERECIPHER' vigDecryptRC 'WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY'
|
||||
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
|
||||
33
Task/Vigen-re-cipher/Java/vigen-re-cipher.java
Normal file
33
Task/Vigen-re-cipher/Java/vigen-re-cipher.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
public class VigenereCipher {
|
||||
public static void main(String[] args) {
|
||||
String key = "VIGENERECIPHER";
|
||||
String ori = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
String enc = encrypt(ori, key);
|
||||
System.out.println(enc);
|
||||
System.out.println(decrypt(enc, key));
|
||||
}
|
||||
|
||||
static String encrypt(String text, final String key) {
|
||||
String res = "";
|
||||
text = text.toUpperCase();
|
||||
for (int i = 0, j = 0; i < text.length(); i++) {
|
||||
char c = text.charAt(i);
|
||||
if (c < 'A' || c > 'Z') continue;
|
||||
res += (char)((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
|
||||
j = ++j % key.length();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static String decrypt(String text, final String key) {
|
||||
String res = "";
|
||||
text = text.toUpperCase();
|
||||
for (int i = 0, j = 0; i < text.length(); i++) {
|
||||
char c = text.charAt(i);
|
||||
if (c < 'A' || c > 'Z') continue;
|
||||
res += (char)((c - key.charAt(j) + 26) % 26 + 'A');
|
||||
j = ++j % key.length();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
29
Task/Vigen-re-cipher/JavaScript/vigen-re-cipher.js
Normal file
29
Task/Vigen-re-cipher/JavaScript/vigen-re-cipher.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<html><head><title>Vigenère</title></head>
|
||||
<body><pre id='x'></pre>
|
||||
<script type="application/javascript">
|
||||
function disp(x) {
|
||||
var e = document.createTextNode(x + '\n');
|
||||
document.getElementById('x').appendChild(e);
|
||||
}
|
||||
|
||||
function ord(x) { return x.charCodeAt(0) }
|
||||
function chr(x) { return String.fromCharCode(x) }
|
||||
function rot(a, b, decode) {
|
||||
return decode ? chr((26 + ord(a) - ord(b)) % 26 + ord('A'))
|
||||
: chr((26 + ord(a) + ord(b) - ord('A') * 2) % 26 + ord('A')) }
|
||||
|
||||
function trans(msg, key, decode) {
|
||||
var i = 0;
|
||||
key = key.toUpperCase();
|
||||
msg = msg.toUpperCase().replace(/[^A-Z]/g, '');
|
||||
return msg.replace(/([A-Z])/g,
|
||||
function($1) { return rot($1, key[i++ % key.length], decode) });
|
||||
}
|
||||
|
||||
var msg = "The quick brown fox Jumped over the lazy Dog the lazy dog lazy dog dog";
|
||||
var key = 'VIGENERECIPHER';
|
||||
var enc = trans(msg, key);
|
||||
var dec = trans(enc, key, 'decipher');
|
||||
|
||||
disp("Original:" + msg + "\nEncoded: " + enc + "\nDecoded: " + dec);
|
||||
</script></body></html>
|
||||
50
Task/Vigen-re-cipher/Liberty-BASIC/vigen-re-cipher.liberty
Normal file
50
Task/Vigen-re-cipher/Liberty-BASIC/vigen-re-cipher.liberty
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
ori$ = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
key$ = filter$("vigenerecipher")
|
||||
print ori$
|
||||
print key$
|
||||
enc$ = encrypt$(ori$, key$)
|
||||
print enc$
|
||||
dec$ = decrypt$(enc$, key$)
|
||||
print dec$
|
||||
|
||||
end
|
||||
|
||||
function encrypt$(text$, key$)
|
||||
flt$ = filter$(text$)
|
||||
encrypt$ = ""
|
||||
j = 1
|
||||
for i = 1 to len(flt$)
|
||||
m$ = mid$(flt$, i, 1)
|
||||
m = asc(m$)-asc("A")
|
||||
k$ = mid$(key$, j, 1)
|
||||
k = asc(k$)-asc("A")
|
||||
j = (j mod len(key$)) + 1
|
||||
c = (m + k) mod 26
|
||||
c$=chr$(asc("A")+c)
|
||||
encrypt$=encrypt$+c$
|
||||
next
|
||||
end function
|
||||
|
||||
function decrypt$(flt$, key$)
|
||||
decrypt$ = ""
|
||||
j = 1
|
||||
for i = 1 to len(flt$)
|
||||
m$ = mid$(flt$, i, 1)
|
||||
m = asc(m$)-asc("A")
|
||||
k$ = mid$(key$, j, 1)
|
||||
k = asc(k$)-asc("A")
|
||||
j = (j mod len(key$)) + 1
|
||||
c = (m - k + 26) mod 26
|
||||
c$=chr$(asc("A")+c)
|
||||
decrypt$=decrypt$+c$
|
||||
next
|
||||
end function
|
||||
|
||||
function filter$(ori$)
|
||||
'a..z A..Z go caps, other skipped
|
||||
filter$=""
|
||||
for i = 1 to len(ori$)
|
||||
c$ = upper$(mid$(ori$,i,1))
|
||||
if instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", c$) then filter$ = filter$ + c$
|
||||
next
|
||||
end function
|
||||
42
Task/Vigen-re-cipher/Lua/vigen-re-cipher.lua
Normal file
42
Task/Vigen-re-cipher/Lua/vigen-re-cipher.lua
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
function Encrypt( _msg, _key )
|
||||
local msg = { _msg:upper():byte( 1, -1 ) }
|
||||
local key = { _key:upper():byte( 1, -1 ) }
|
||||
local enc = {}
|
||||
|
||||
local j, k = 1, 1
|
||||
for i = 1, #msg do
|
||||
if msg[i] >= string.byte('A') and msg[i] <= string.byte('Z') then
|
||||
enc[k] = ( msg[i] + key[j] - 2*string.byte('A') ) % 26 + string.byte('A')
|
||||
|
||||
k = k + 1
|
||||
if j == #key then j = 1 else j = j + 1 end
|
||||
end
|
||||
end
|
||||
|
||||
return string.char( unpack(enc) )
|
||||
end
|
||||
|
||||
function Decrypt( _msg, _key )
|
||||
local msg = { _msg:byte( 1, -1 ) }
|
||||
local key = { _key:upper():byte( 1, -1 ) }
|
||||
local dec = {}
|
||||
|
||||
local j = 1
|
||||
for i = 1, #msg do
|
||||
dec[i] = ( msg[i] - key[j] + 26 ) % 26 + string.byte('A')
|
||||
|
||||
if j == #key then j = 1 else j = j + 1 end
|
||||
end
|
||||
|
||||
return string.char( unpack(dec) )
|
||||
end
|
||||
|
||||
|
||||
original = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
key = "VIGENERECIPHER";
|
||||
|
||||
encrypted = Encrypt( original, key )
|
||||
decrypted = Decrypt( encrypted, key )
|
||||
|
||||
print( encrypted )
|
||||
print( decrypted )
|
||||
31
Task/Vigen-re-cipher/Mathematica/vigen-re-cipher.math
Normal file
31
Task/Vigen-re-cipher/Mathematica/vigen-re-cipher.math
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
encode[text_String, key_String] :=
|
||||
Module[{textCode, keyCode},
|
||||
textCode =
|
||||
Cases[ToCharacterCode[
|
||||
ToUpperCase@
|
||||
text], _?(IntervalMemberQ[Interval@{65, 90}, #] &)] - 65;
|
||||
keyCode =
|
||||
Cases[ToCharacterCode[
|
||||
ToUpperCase@
|
||||
key], _?(IntervalMemberQ[Interval@{65, 90}, #] &)] - 65;
|
||||
keyCode =
|
||||
If[Length[textCode] < Length[keyCode],
|
||||
keyCode[[;; Length@textCode]],
|
||||
PadRight[keyCode, Length@textCode, keyCode]];
|
||||
FromCharacterCode[Mod[textCode + keyCode, 26] + 65]]
|
||||
|
||||
decode[text_String, key_String] :=
|
||||
Module[{textCode, keyCode},
|
||||
textCode =
|
||||
Cases[ToCharacterCode[
|
||||
ToUpperCase@
|
||||
text], _?(IntervalMemberQ[Interval@{65, 90}, #] &)] - 65;
|
||||
keyCode =
|
||||
Cases[ToCharacterCode[
|
||||
ToUpperCase@
|
||||
key], _?(IntervalMemberQ[Interval@{65, 90}, #] &)] - 65;
|
||||
keyCode =
|
||||
If[Length[textCode] < Length[keyCode],
|
||||
keyCode[[;; Length@textCode]],
|
||||
PadRight[keyCode, Length@textCode, keyCode]];
|
||||
FromCharacterCode[Mod[textCode - keyCode, 26] + 65]]
|
||||
152
Task/Vigen-re-cipher/NetRexx/vigen-re-cipher.netrexx
Normal file
152
Task/Vigen-re-cipher/NetRexx/vigen-re-cipher.netrexx
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols nobinary
|
||||
|
||||
pt = 'Attack at dawn!'
|
||||
key = 'LEMON'
|
||||
test(key, pt)
|
||||
|
||||
key = 'N' -- rot-13
|
||||
test(key, pt)
|
||||
|
||||
key = 'B' -- Caesar
|
||||
test(key, pt)
|
||||
|
||||
pt = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
key = 'A'
|
||||
test(key, pt)
|
||||
|
||||
pt = sampledata()
|
||||
key = 'Hamlet; Prince of Denmark'
|
||||
test(key, pt)
|
||||
|
||||
return
|
||||
|
||||
method vigenere(meth, key, text) public static
|
||||
|
||||
select
|
||||
when 'encipher'.abbrev(meth.lower, 1) then df = 1
|
||||
when 'decipher'.abbrev(meth.lower, 1) then df = -1
|
||||
otherwise signal IllegalArgumentException(meth 'must be "encipher" or "decipher"')
|
||||
end
|
||||
|
||||
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
|
||||
text = stringscrubber(text)
|
||||
key = stringscrubber(key)
|
||||
code = ''
|
||||
loop l_ = 1 to text.length()
|
||||
M = alpha.pos(text.substr(l_, 1)) - 1
|
||||
k_ = (l_ - 1) // key.length()
|
||||
K = alpha.pos(key.substr(k_ + 1, 1)) - 1
|
||||
C = mod((M + K * df), alpha.length())
|
||||
C = alpha.substr(C + 1, 1)
|
||||
code = code || C
|
||||
end l_
|
||||
|
||||
return code
|
||||
|
||||
method vigenere_encipher(key, plaintext) public static
|
||||
|
||||
return vigenere('encipher', key, plaintext)
|
||||
|
||||
method vigenere_decipher(key, ciphertext) public static
|
||||
|
||||
return vigenere('decipher', key, ciphertext)
|
||||
|
||||
method mod(N = int, D = int) private static
|
||||
|
||||
return (D + (N // D)) // D
|
||||
|
||||
method stringscrubber(cleanup) private static
|
||||
|
||||
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
|
||||
cleanup = cleanup.upper.space(0)
|
||||
loop label f_ forever
|
||||
x_ = cleanup.verify(alpha)
|
||||
if x_ = 0 then leave f_
|
||||
cleanup = cleanup.changestr(cleanup.substr(x_, 1), '')
|
||||
end f_
|
||||
|
||||
return cleanup
|
||||
|
||||
method test(key, pt) private static
|
||||
|
||||
ct = vigenere_encipher(key, pt)
|
||||
display(ct)
|
||||
dt = vigenere_decipher(key, ct)
|
||||
display(dt)
|
||||
|
||||
return
|
||||
|
||||
method display(text) public static
|
||||
|
||||
line = ''
|
||||
o_ = 0
|
||||
loop c_ = 1 to text.length()
|
||||
b_ = o_ // 5
|
||||
o_ = o_ + 1
|
||||
if b_ = 0 then line = line' '
|
||||
line = line || text.substr(c_, 1)
|
||||
end c_
|
||||
|
||||
say '....+....|'.copies(8)
|
||||
loop label l_ forever
|
||||
parse line w1 w2 w3 w4 w5 w6 W7 w8 w9 w10 w11 w12 line
|
||||
pline = w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12
|
||||
say pline.strip()
|
||||
if line.strip().length() = 0 then leave l_
|
||||
end l_
|
||||
say
|
||||
|
||||
return
|
||||
|
||||
method sampledata() private static returns Rexx
|
||||
|
||||
NL = char('\n')
|
||||
antic_disposition = Rexx[]
|
||||
|
||||
antic_disposition = [ -
|
||||
Rexx("To be, or not to be--that is the question:" ), -
|
||||
Rexx("Whether 'tis nobler in the mind to suffer" ), -
|
||||
Rexx("The slings and arrows of outrageous fortune" ), -
|
||||
Rexx("Or to take arms against a sea of troubles" ), -
|
||||
Rexx("And by opposing end them. To die, to sleep--" ), -
|
||||
Rexx("No more--and by a sleep to say we end" ), -
|
||||
Rexx("The heartache, and the thousand natural shocks" ), -
|
||||
Rexx("That flesh is heir to. 'Tis a consummation" ), -
|
||||
Rexx("Devoutly to be wished. To die, to sleep--" ), -
|
||||
Rexx("To sleep--perchance to dream: ay, there's the rub,"), -
|
||||
Rexx("For in that sleep of death what dreams may come" ), -
|
||||
Rexx("When we have shuffled off this mortal coil," ), -
|
||||
Rexx("Must give us pause. There's the respect" ), -
|
||||
Rexx("That makes calamity of so long life." ), -
|
||||
Rexx("For who would bear the whips and scorns of time," ), -
|
||||
Rexx("Th' oppressor's wrong, the proud man's contumely" ), -
|
||||
Rexx("The pangs of despised love, the law's delay," ), -
|
||||
Rexx("The insolence of office, and the spurns" ), -
|
||||
Rexx("That patient merit of th' unworthy takes," ), -
|
||||
Rexx("When he himself might his quietus make" ), -
|
||||
Rexx("With a bare bodkin? Who would fardels bear," ), -
|
||||
Rexx("To grunt and sweat under a weary life," ), -
|
||||
Rexx("But that the dread of something after death," ), -
|
||||
Rexx("The undiscovered country, from whose bourn" ), -
|
||||
Rexx("No traveller returns, puzzles the will," ), -
|
||||
Rexx("And makes us rather bear those ills we have" ), -
|
||||
Rexx("Than fly to others that we know not of?" ), -
|
||||
Rexx("Thus conscience does make cowards of us all," ), -
|
||||
Rexx("And thus the native hue of resolution" ), -
|
||||
Rexx("Is sicklied o'er with the pale cast of thought," ), -
|
||||
Rexx("And enterprise of great pith and moment" ), -
|
||||
Rexx("With this regard their currents turn awry" ), -
|
||||
Rexx("And lose the name of action. -- Soft you now," ), -
|
||||
Rexx("The fair Ophelia! -- Nymph, in thy orisons" ), -
|
||||
Rexx("Be all my sins remembered." ) -
|
||||
]
|
||||
|
||||
melancholy_dane = Rexx('')
|
||||
loop l_ = 0 for antic_disposition.length
|
||||
melancholy_dane = melancholy_dane || antic_disposition[l_] || NL
|
||||
end l_
|
||||
|
||||
return melancholy_dane
|
||||
41
Task/Vigen-re-cipher/OCaml/vigen-re-cipher.ocaml
Normal file
41
Task/Vigen-re-cipher/OCaml/vigen-re-cipher.ocaml
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
let cipher src key crypt =
|
||||
let str = String.uppercase src in
|
||||
let key = String.uppercase key in
|
||||
|
||||
(* strip out non-letters *)
|
||||
let len = String.length str in
|
||||
let rec aux i j =
|
||||
if j >= len then String.sub str 0 i else
|
||||
if str.[j] >= 'A' && str.[j] <= 'Z'
|
||||
then (str.[i] <- str.[j]; aux (succ i) (succ j))
|
||||
else aux i (succ j)
|
||||
in
|
||||
let res = aux 0 0 in
|
||||
|
||||
let slen = String.length res in
|
||||
let klen = String.length key in
|
||||
|
||||
let d = int_of_char in
|
||||
let f =
|
||||
if crypt
|
||||
then fun i -> d res.[i] - d 'A' + d key.[i mod klen] - d 'A'
|
||||
else fun i -> d res.[i] - d key.[i mod klen] + 26
|
||||
in
|
||||
for i = 0 to pred slen do
|
||||
res.[i] <- char_of_int (d 'A' + (f i) mod 26)
|
||||
done;
|
||||
(res)
|
||||
|
||||
let () =
|
||||
let str = "Beware the Jabberwock, my son! The jaws that bite, \
|
||||
the claws that catch!" in
|
||||
let key = "VIGENERECIPHER" in
|
||||
|
||||
let cod = cipher str key true in
|
||||
let dec = cipher cod key false in
|
||||
|
||||
Printf.printf "Text: %s\n" str;
|
||||
Printf.printf "key: %s\n" key;
|
||||
Printf.printf "Code: %s\n" cod;
|
||||
Printf.printf "Back: %s\n" dec;
|
||||
;;
|
||||
45
Task/Vigen-re-cipher/Objeck/vigen-re-cipher.objeck
Normal file
45
Task/Vigen-re-cipher/Objeck/vigen-re-cipher.objeck
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
bundle Default {
|
||||
class VigenereCipher {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
key := "VIGENERECIPHER";
|
||||
ori := "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
enc := encrypt(ori, key);
|
||||
IO.Console->Print("encrypt: ")->PrintLine(enc);
|
||||
IO.Console->Print("decrypt: ")->PrintLine(decrypt(enc, key));
|
||||
}
|
||||
|
||||
function : native : encrypt(text : String, key : String) ~ String {
|
||||
res := "";
|
||||
text := text->ToUpper();
|
||||
j := 0;
|
||||
|
||||
each(i : text) {
|
||||
c := text->Get(i);
|
||||
if(c >= 'A' & c <= 'Z') {
|
||||
res->Append(((c + key->Get(j) - 2 * 'A') % 26 + 'A')->As(Char));
|
||||
j += 1;
|
||||
j := j % key->Size();
|
||||
};
|
||||
};
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
function : native : decrypt(text : String, key : String) ~ String {
|
||||
res := "";
|
||||
text := text->ToUpper();
|
||||
j := 0;
|
||||
|
||||
each(i : text) {
|
||||
c := text->Get(i);
|
||||
if(c >= 'A' & c <= 'Z') {
|
||||
res->Append(((c - key->Get(j) + 26) % 26 + 'A')->As(Char));
|
||||
j += 1;
|
||||
j := j % key->Size();
|
||||
};
|
||||
};
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
44
Task/Vigen-re-cipher/PL-I/vigen-re-cipher.pli
Normal file
44
Task/Vigen-re-cipher/PL-I/vigen-re-cipher.pli
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
cypher: procedure options (main); /* 21 September 2012 */
|
||||
declare t(26) character (26);
|
||||
declare (i, j, k, L) fixed binary;
|
||||
declare (original, encoded, coder) character (1000) varying initial ('');
|
||||
declare cypher character (30) varying;
|
||||
declare (co, ct, cc) character (1);
|
||||
|
||||
/* Set up cypher table. */
|
||||
t(1) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
do i = 2 to 26;
|
||||
t(i) = substr(t(i-1), 2, 25) || substr(t(i-1), 1, 1);
|
||||
end;
|
||||
|
||||
cypher = 'VIGILANCE';
|
||||
original = 'Meet me on Tuesday evening at seven.';
|
||||
put edit ('Message=', original) (a);
|
||||
original = uppercase(original);
|
||||
|
||||
/* Create the cypher text, same length as original, or longer. */
|
||||
coder = repeat(cypher, length(original)/length(cypher));
|
||||
|
||||
/* Encode the original message, character by character. */
|
||||
/* Non-alphabetic characters are ignored. */
|
||||
L = 0;
|
||||
do i = 1 to length(original);
|
||||
co = substr(original, i, 1);
|
||||
j = index(t(1), co);
|
||||
if j = 0 then iterate; /* Ignore non-alphabetic character */
|
||||
L = L + 1;
|
||||
ct = substr(coder, L, 1);
|
||||
k = index(t(1), ct);
|
||||
encoded = encoded || substr(t(j), k, 1);
|
||||
end;
|
||||
put skip data (encoded);
|
||||
|
||||
/* DECODING. */
|
||||
put skip list ('Decoded=');
|
||||
do i = 1 to length(encoded);
|
||||
cc = substr(coder, i, 1);
|
||||
j = index(t(1), cc);
|
||||
k = index(t(j), substr(encoded, i, 1));
|
||||
put edit (substr(t(1), k, 1) ) (a(1));
|
||||
end;
|
||||
end cypher;
|
||||
12
Task/Vigen-re-cipher/Perl-6/vigen-re-cipher.pl6
Normal file
12
Task/Vigen-re-cipher/Perl-6/vigen-re-cipher.pl6
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
sub s2v ($s) { $s.uc.comb(/ <[ A..Z ]> /)».ord »-» 65 }
|
||||
sub v2s (@v) { (@v »%» 26 »+» 65)».chr.join }
|
||||
|
||||
sub blacken ($red, $key) { v2s s2v($red) »+» s2v($key) }
|
||||
sub redden ($blk, $key) { v2s s2v($blk) »-» s2v($key) }
|
||||
|
||||
my $red = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
my $key = "Vigenere Cipher!!!";
|
||||
|
||||
say $red;
|
||||
say my $black = blacken($red, $key);
|
||||
say redden($black, $key);
|
||||
40
Task/Vigen-re-cipher/Perl/vigen-re-cipher.pl
Normal file
40
Task/Vigen-re-cipher/Perl/vigen-re-cipher.pl
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
if( @ARGV != 3 ){
|
||||
printHelp();
|
||||
}
|
||||
|
||||
# translate to upper-case, remove anything else
|
||||
map( (tr/a-z/A-Z/, s/[^A-Z]//g), @ARGV );
|
||||
|
||||
my $cipher_decipher = $ARGV[ 0 ];
|
||||
|
||||
if( $cipher_decipher !~ /ENC|DEC/ ){
|
||||
printHelp(); # user should say what to do
|
||||
}
|
||||
|
||||
print "Key: " . (my $key = $ARGV[ 2 ]) . "\n";
|
||||
|
||||
if( $cipher_decipher =~ /ENC/ ){
|
||||
print "Plain-text: " . (my $plain = $ARGV[ 1 ]) . "\n";
|
||||
print "Encrypted: " . Vigenere( 1, $plain ) . "\n";
|
||||
}elsif( $cipher_decipher =~ /DEC/ ){
|
||||
print "Cipher-text: " . (my $cipher = $ARGV[ 1 ]) . "\n";
|
||||
print "Decrypted: " . Vigenere( -1, $cipher ) . "\n";
|
||||
}
|
||||
|
||||
sub printHelp{
|
||||
print "Usage:\n" .
|
||||
"Encrypting:\n perl cipher.pl ENC (plain text) (key)\n" .
|
||||
"Decrypting:\n perl cipher.pl DEC (cipher text) (key)\n";
|
||||
exit -1;
|
||||
}
|
||||
|
||||
sub Vigenere{
|
||||
my ($direction, $text) = @_;
|
||||
for( my $count = 0; $count < length $text; $count ++ ){
|
||||
$key_offset = $direction * ord substr( $key, $count % length $key, 1);
|
||||
$char_offset = ord substr( $text, $count, 1 );
|
||||
$cipher .= chr 65 + ((($char_offset % 26) + ($key_offset % 26)) % 26);
|
||||
# 65 is the ASCII character code for 'A'
|
||||
}
|
||||
return $cipher;
|
||||
}
|
||||
22
Task/Vigen-re-cipher/PicoLisp/vigen-re-cipher.l
Normal file
22
Task/Vigen-re-cipher/PicoLisp/vigen-re-cipher.l
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
(de vigenereKey (Str)
|
||||
(extract
|
||||
'((C)
|
||||
(when (>= "Z" (uppc C) "A")
|
||||
(- (char (uppc C)) 65) ) )
|
||||
(chop Str) ) )
|
||||
|
||||
(de vigenereEncrypt (Str Key)
|
||||
(pack
|
||||
(mapcar
|
||||
'((C K)
|
||||
(char (+ 65 (% (+ C K) 26))) )
|
||||
(vigenereKey Str)
|
||||
(apply circ (vigenereKey Key)) ) ) )
|
||||
|
||||
(de vigenereDecrypt (Str Key)
|
||||
(pack
|
||||
(mapcar
|
||||
'((C K)
|
||||
(char (+ 65 (% (+ 26 (- C K)) 26))) )
|
||||
(vigenereKey Str)
|
||||
(apply circ (vigenereKey Key)) ) ) )
|
||||
61
Task/Vigen-re-cipher/PureBasic/vigen-re-cipher.purebasic
Normal file
61
Task/Vigen-re-cipher/PureBasic/vigen-re-cipher.purebasic
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
Procedure prepString(text.s, Array letters(1))
|
||||
;convert characters to an ordinal (0-25) and remove non-alphabetic characters,
|
||||
;returns dimension size of result array letters()
|
||||
Protected *letter.Character, index
|
||||
Dim letters(Len(text))
|
||||
text = UCase(text)
|
||||
*letter = @text
|
||||
While *letter\c
|
||||
Select *letter\c
|
||||
Case 'A' To 'Z'
|
||||
letters(index) = *letter\c - 65
|
||||
index + 1
|
||||
EndSelect
|
||||
*letter + SizeOf(Character)
|
||||
Wend
|
||||
If index > 0
|
||||
Redim letters(index - 1)
|
||||
EndIf
|
||||
ProcedureReturn index - 1
|
||||
EndProcedure
|
||||
|
||||
Procedure.s VC_encrypt(text.s, keyText.s, reverse = 0)
|
||||
;if reverse <> 0 then reverse the key (decrypt)
|
||||
Protected *letter.Character
|
||||
Dim text(0)
|
||||
Dim keyText(0)
|
||||
If prepString(text, text()) < 0 Or prepString(keyText, keyText()) < 0: ProcedureReturn: EndIf ;exit, nothing to work with
|
||||
|
||||
Protected i, keyLength = ArraySize(keyText())
|
||||
If reverse
|
||||
For i = 0 To keyLength
|
||||
keyText(i) = 26 - keyText(i)
|
||||
Next
|
||||
EndIf
|
||||
|
||||
Protected textLength = ArraySize(text()) ;zero-based length
|
||||
Protected result.s = Space(textLength + 1), *resultLetter.Character
|
||||
keyLength + 1 ;convert from zero-based to one-based count
|
||||
*resultLetter = @result
|
||||
For i = 0 To textLength
|
||||
*resultLetter\c = ((text(i) + keyText(i % keyLength)) % 26) + 65
|
||||
*resultLetter + SizeOf(Character)
|
||||
Next
|
||||
ProcedureReturn result
|
||||
EndProcedure
|
||||
|
||||
Procedure.s VC_decrypt(cypherText.s, keyText.s)
|
||||
ProcedureReturn VC_encrypt(cypherText, keyText.s, 1)
|
||||
EndProcedure
|
||||
|
||||
If OpenConsole()
|
||||
Define VignereCipher.s, plainText.s, encryptedText.s, decryptedText.s
|
||||
|
||||
VignereCipher.s = "VIGNERECIPHER"
|
||||
plainText = "The quick brown fox jumped over the lazy dogs.": PrintN(RSet("Plain text = ", 17) + #DQUOTE$ + plainText + #DQUOTE$)
|
||||
encryptedText = VC_encrypt(plainText, VignereCipher): PrintN(RSet("Encrypted text = ", 17) + #DQUOTE$ + encryptedText + #DQUOTE$)
|
||||
decryptedText = VC_decrypt(encryptedText, VignereCipher): PrintN(RSet("Decrypted text = ", 17) + #DQUOTE$ + decryptedText + #DQUOTE$)
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
19
Task/Vigen-re-cipher/Python/vigen-re-cipher-1.py
Normal file
19
Task/Vigen-re-cipher/Python/vigen-re-cipher-1.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
from itertools import starmap, cycle
|
||||
|
||||
def encrypt(message, key):
|
||||
|
||||
# convert to uppercase.
|
||||
# strip out non-alpha characters.
|
||||
message = filter(lambda _: _.isalpha(), message.upper())
|
||||
|
||||
# single letter encrpytion.
|
||||
def enc(c,k): return chr(((ord(k) + ord(c)) % 26) + ord('A'))
|
||||
|
||||
return "".join(starmap(enc, zip(message, cycle(key))))
|
||||
|
||||
def decrpyt(message, key):
|
||||
|
||||
# single letter decryption.
|
||||
def dec(c,k): return chr(((ord(c) - ord(k)) % 26) + ord('A'))
|
||||
|
||||
return "".join(starmap(dec, zip(message, cycle(key))))
|
||||
9
Task/Vigen-re-cipher/Python/vigen-re-cipher-2.py
Normal file
9
Task/Vigen-re-cipher/Python/vigen-re-cipher-2.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
key = "VIGENERECIPHER"
|
||||
|
||||
encr = encrypt(text, key)
|
||||
decr = decrpyt(encr, key)
|
||||
|
||||
print text
|
||||
print encr
|
||||
print decr
|
||||
18
Task/Vigen-re-cipher/R/vigen-re-cipher.r
Normal file
18
Task/Vigen-re-cipher/R/vigen-re-cipher.r
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
mod1 = function(v, n)
|
||||
# mod1(1:20, 6) => 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2
|
||||
((v - 1) %% n) + 1
|
||||
|
||||
str2ints = function(s)
|
||||
as.integer(Filter(Negate(is.na),
|
||||
factor(levels = LETTERS, strsplit(toupper(s), "")[[1]])))
|
||||
|
||||
vigen = function(input, key, decrypt = F)
|
||||
{input = str2ints(input)
|
||||
key = rep(str2ints(key), len = length(input)) - 1
|
||||
paste(collapse = "", LETTERS[
|
||||
mod1(input + (if (decrypt) -1 else 1)*key, length(LETTERS))])}
|
||||
|
||||
message(vigen("Beware the Jabberwock, my son! The jaws that bite, the claws that catch!", "vigenerecipher"))
|
||||
# WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
|
||||
message(vigen("WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY", "vigenerecipher", decrypt = T))
|
||||
# BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
|
||||
31
Task/Vigen-re-cipher/REXX/vigen-re-cipher-1.rexx
Normal file
31
Task/Vigen-re-cipher/REXX/vigen-re-cipher-1.rexx
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/*REXX program encrypts uppercased text using the Vigenère cypher. */
|
||||
@.1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
L=length(@.1)
|
||||
do j=2 to L; jm=j-1; q=@.jm
|
||||
@.j=substr(q,2,L-1)left(q,1)
|
||||
end /*j*/
|
||||
|
||||
cypher = space('WHOOP DE DOO NO BIG DEAL HERE OR THERE',0)
|
||||
oMsg = 'People solve problems by trial and error; judgement helps pick the trial.'
|
||||
oMsgU = oMsg; upper oMsgU
|
||||
cypher_=copies(cypher,length(oMsg)%length(cypher))
|
||||
say ' original text =' oMsg
|
||||
xMsg=Ncypher(oMsgU)
|
||||
say ' cyphered text =' xMsg
|
||||
bMsg=Dcypher(xMsg)
|
||||
say 're-cyphered text =' bMsg
|
||||
exit
|
||||
/*───────────────────────────────Ncypher subroutine─────────────────────*/
|
||||
Ncypher: parse arg stuff; #=1; nMsg=
|
||||
do i=1 for length(stuff); x=substr(stuff,i,1) /*pick off 1 char.*/
|
||||
if \datatype(x,'U') then iterate /*not a letter? Then ignore it.*/
|
||||
j=pos(x,@.1); nMsg=nMsg || substr(@.j,pos(substr(cypher_,#,1),@.1),1)
|
||||
#=#+1 /*bump the character counter. */
|
||||
end
|
||||
return nMsg
|
||||
/*───────────────────────────────Dcypher subroutine──────────────────────*/
|
||||
Dcypher: parse arg stuff; #=1; dMsg=
|
||||
do i=1 for length(stuff); x=substr(cypher_,i,1) /*pick off 1 char.*/
|
||||
j=pos(x,@.1); dMsg=dMsg || substr(@.1, pos(substr(stuff,i,1),@.j),1)
|
||||
end
|
||||
return dMsg
|
||||
30
Task/Vigen-re-cipher/REXX/vigen-re-cipher-2.rexx
Normal file
30
Task/Vigen-re-cipher/REXX/vigen-re-cipher-2.rexx
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*REXX program encrypts uppercased text using the Vigenère cypher. */
|
||||
@abc='abcdefghijklmnopqrstuvwxyz'; @abcU=@abc; upper @abcU
|
||||
@.1 = @abcU || @abc'0123456789~`!@#$%^&*()_-+={}|[]\:;<>?,./" '''
|
||||
L=length(@.1)
|
||||
do j=2 to length(@.1); jm=j-1; q=@.jm
|
||||
@.j=substr(q,2,length(@.1)-1)left(q,1)
|
||||
end /*j*/
|
||||
|
||||
cypher = space('WHOOP DE DOO NO BIG DEAL HERE OR THERE',0)
|
||||
oMsg = 'Making things easy is just knowing the shortcuts. --- Gerard J. Schildberger'
|
||||
cypher_=copies(cypher,length(oMsg)%length(cypher))
|
||||
say ' original text =' oMsg
|
||||
xMsg=Ncypher(oMsg)
|
||||
say ' cyphered text =' xMsg
|
||||
bMsg=Dcypher(xMsg)
|
||||
say 're-cyphered text =' bMsg
|
||||
exit
|
||||
/*───────────────────────────────Ncypher subroutine─────────────────────*/
|
||||
Ncypher: parse arg stuff; #=1; nMsg=
|
||||
do i=1 for length(stuff); x=substr(stuff,i,1) /*pick off 1 char.*/
|
||||
j=pos(x,@.1); if j==0 then iterate /*character not supported? Ignore*/
|
||||
nMsg=nMsg || substr(@.j,pos(substr(cypher_,#,1),@.1),1); #=#+1
|
||||
end /*j*/
|
||||
return nMsg
|
||||
/*───────────────────────────────Dcypher subroutine──────────────────────*/
|
||||
Dcypher: parse arg stuff; #=1; dMsg=
|
||||
do i=1 for length(stuff); x=substr(cypher_,i,1) /*pick off 1 char.*/
|
||||
j=pos(x,@.1); dMsg=dMsg || substr(@.1, pos(substr(stuff,i,1),@.j),1)
|
||||
end /*j*/
|
||||
return dMsg
|
||||
34
Task/Vigen-re-cipher/Ruby/vigen-re-cipher-1.rb
Normal file
34
Task/Vigen-re-cipher/Ruby/vigen-re-cipher-1.rb
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
class VigenereCipher
|
||||
|
||||
BASE = 'A'.ord
|
||||
SIZE = 'Z'.ord - BASE + 1
|
||||
|
||||
def key=(key)
|
||||
@key = key.upcase.gsub(/[^A-Z]/, '')
|
||||
end
|
||||
|
||||
def initialize(key)
|
||||
self.key= key
|
||||
end
|
||||
|
||||
def encrypt(text)
|
||||
crypt(text, :+)
|
||||
end
|
||||
|
||||
def decrypt(text)
|
||||
crypt(text, :-)
|
||||
end
|
||||
|
||||
def crypt(text, dir)
|
||||
plaintext = text.upcase.gsub(/[^A-Z]/, '')
|
||||
key_iterator = @key.chars.cycle
|
||||
ciphertext = ''
|
||||
plaintext.each_char do |plain_char|
|
||||
offset = key_iterator.next.ord - BASE
|
||||
ciphertext +=
|
||||
((plain_char.ord - BASE).send(dir, offset) % SIZE + BASE).chr
|
||||
end
|
||||
return ciphertext
|
||||
end
|
||||
|
||||
end
|
||||
7
Task/Vigen-re-cipher/Ruby/vigen-re-cipher-2.rb
Normal file
7
Task/Vigen-re-cipher/Ruby/vigen-re-cipher-2.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
vc = VigenereCipher.new('Vigenere cipher')
|
||||
plaintext = 'Beware the Jabberwock, my son! The jaws that bite, the claws that catch!'
|
||||
ciphertext = vc.encrypt(plaintext)
|
||||
recovered = vc.decrypt(ciphertext)
|
||||
puts "Original: #{plaintext}"
|
||||
puts "Encrypted: #{ciphertext}"
|
||||
puts "Decrypted: #{recovered}"
|
||||
49
Task/Vigen-re-cipher/Rust/vigen-re-cipher.rust
Normal file
49
Task/Vigen-re-cipher/Rust/vigen-re-cipher.rust
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
fn uppercase_and_filter(in: &str) -> ~[u8] {
|
||||
let mut result = ~[];
|
||||
|
||||
for in.each_char |c| {
|
||||
if char::is_ascii(c) {
|
||||
if char::is_lowercase(c) {
|
||||
// We know it's ascii, so just do the math directly
|
||||
result.push((c + ('A' - 'a')) as u8)
|
||||
} else if char::is_uppercase(c) {
|
||||
result.push(c as u8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
fn vigenere(key: &str, text: &str, is_encoding: bool) -> ~str {
|
||||
const A: u8 = 'A' as u8;
|
||||
|
||||
let key_bytes = uppercase_and_filter(key);
|
||||
let text_bytes = uppercase_and_filter(text);
|
||||
|
||||
let mut result_bytes = ~[];
|
||||
|
||||
for text_bytes.eachi |i, &c| {
|
||||
let c2 = if is_encoding {
|
||||
(c + key_bytes[i % key_bytes.len()] - 2 * A) % 26 + A
|
||||
} else {
|
||||
(c - key_bytes[i % key_bytes.len()] + 26) % 26 + A
|
||||
};
|
||||
result_bytes.push(c2);
|
||||
}
|
||||
|
||||
return str::from_bytes(result_bytes);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
let key = "VIGENERECIPHER";
|
||||
|
||||
io::println(fmt!("Text: %s", text));
|
||||
io::println(fmt!("Key: %s", key));
|
||||
|
||||
let encoded = vigenere(key, text, true);
|
||||
io::println(fmt!("Code: %s", encoded));
|
||||
let decoded = vigenere(key, encoded, false);
|
||||
io::println(fmt!("Back: %s", decoded));
|
||||
}
|
||||
115
Task/Vigen-re-cipher/Scala/vigen-re-cipher.scala
Normal file
115
Task/Vigen-re-cipher/Scala/vigen-re-cipher.scala
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
import scala.language.implicitConversions
|
||||
|
||||
//
|
||||
// Translate Special Chars to Names
|
||||
// --------------------------------
|
||||
//
|
||||
class VText
|
||||
|
||||
class KeyText( key:String ) extends VText {
|
||||
override def toString = key
|
||||
}
|
||||
|
||||
object KeyText {
|
||||
def apply(k:String) = new KeyText( "[^A-Z]*".r.replaceAllIn(k.toUpperCase,"") )
|
||||
}
|
||||
|
||||
|
||||
class PlainText( message:String ) extends VText {
|
||||
override def toString = {
|
||||
"[^A-Z]*".r.replaceAllIn(message.toUpperCase,"")
|
||||
.replaceAll("FULLSTOP",".")
|
||||
.replaceAll("CERO","0")
|
||||
.replaceAll("EINZ","1")
|
||||
.replaceAll("ZWEI","2")
|
||||
.replaceAll("TRES","3")
|
||||
.replaceAll("CUATRO","4")
|
||||
.replaceAll("CINQ","5")
|
||||
.replaceAll("SECHS","6")
|
||||
.replaceAll("SIETE","7")
|
||||
.replaceAll("HUIT","8")
|
||||
.replaceAll("NEUF","9")
|
||||
}
|
||||
|
||||
def toRawString = message
|
||||
}
|
||||
|
||||
object PlainText {
|
||||
def apply(k:String) = new PlainText( k.toList.map(_.toString).map {
|
||||
case "0" => "CERO"
|
||||
case "1" => "EINZ"
|
||||
case "2" => "ZWEI"
|
||||
case "3" => "TRES"
|
||||
case "4" => "CUATRO"
|
||||
case "5" => "CINQ"
|
||||
case "6" => "SECHS"
|
||||
case "7" => "SIETE"
|
||||
case "8" => "HUIT"
|
||||
case "9" => "NEUF"
|
||||
case "." => "FULLSTOP"
|
||||
case a:String => "[^A-Z]*".r.replaceAllIn(a.toUpperCase,"")
|
||||
}.mkString )
|
||||
}
|
||||
|
||||
|
||||
class CipherText( cipher:String ) extends VText {
|
||||
override def toString = cipher
|
||||
}
|
||||
|
||||
object CipherText {
|
||||
def apply(k:String) = new CipherText(
|
||||
"[^A-Z]*".r.replaceAllIn(k.toUpperCase,"")
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// The Vigenère Cipher
|
||||
// -------------------
|
||||
//
|
||||
def vcipher( key:KeyText )( plaintext:PlainText ) : CipherText = {
|
||||
val k = key.toString
|
||||
val p = plaintext.toRawString
|
||||
val kp = (k * (p.length / k.length + 1)) zip (p)
|
||||
|
||||
CipherText( (for( t <- kp; k = (t._1 - 'A'); p = (t._2 - 'A'); a = 'A' + (p + k) % 26 ) yield (a.toChar)).mkString )
|
||||
}
|
||||
|
||||
def vdecipher( key:KeyText )( ciphertext:CipherText ) : PlainText = {
|
||||
val k = key.toString
|
||||
val c = ciphertext.toString
|
||||
val kc = (k * (c.length / k.length + 1)) zip (c)
|
||||
|
||||
PlainText( (for( t <- kc; k = (t._1 - 'A'); c = (t._2 - 'A'); a = 'A' + (26 + c - k) % 26 ) yield (a.toChar)).mkString )
|
||||
}
|
||||
|
||||
|
||||
// Use Implicits to overcome the ambiguity problem of overloaded partial functions
|
||||
implicit def VTextToString( v:VText ) : String = v match {
|
||||
case k:KeyText => k.toString
|
||||
case p:PlainText => p.toString
|
||||
case c:CipherText => c.toString
|
||||
case _ => ""
|
||||
}
|
||||
implicit def StringToKeyText( s:String ) = KeyText(s)
|
||||
implicit def StringToPlainText( s:String ) = PlainText(s)
|
||||
implicit def StringToCipherText( s:String ) = CipherText(s)
|
||||
|
||||
|
||||
|
||||
// Validate cipher/decipher
|
||||
assert( vcipher("LEMON")("ATTACKATDAWN").toString == "LXFOPVEFRNHR" )
|
||||
assert( vdecipher("LEMON")("LXFOPVEFRNHR").toString == "ATTACKATDAWN" )
|
||||
|
||||
|
||||
// A quick test
|
||||
val todaysKey = "lemon"
|
||||
val todaysCipher = vcipher(todaysKey) _
|
||||
val todaysDecipher = vdecipher(todaysKey) _
|
||||
|
||||
val plaintext = "Attack at 0620."
|
||||
val ciphertext = todaysCipher(plaintext)
|
||||
|
||||
println( " Message: " + plaintext + "\n" )
|
||||
println( "Enciphered: " + ciphertext + "\n" )
|
||||
println( "Deciphered: " + todaysDecipher( ciphertext ) + "\n" )
|
||||
51
Task/Vigen-re-cipher/Seed7/vigen-re-cipher.seed7
Normal file
51
Task/Vigen-re-cipher/Seed7/vigen-re-cipher.seed7
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const func string: vigenereCipher (in string: source, in var string: keyword) is func
|
||||
result
|
||||
var string: dest is "";
|
||||
local
|
||||
var char: ch is ' ';
|
||||
var integer: index is 1;
|
||||
var integer: shift is 0;
|
||||
begin
|
||||
keyword := upper(keyword);
|
||||
for ch range source do
|
||||
if ch in {'A' .. 'Z'} | {'a' .. 'z'} then
|
||||
shift := ord(keyword[succ(pred(index) rem length(keyword))]) - ord('A');
|
||||
dest &:= chr(ord('A') + (ord(upper(ch)) - ord('A') + shift) rem 26);
|
||||
incr(index);
|
||||
end if;
|
||||
end for;
|
||||
end func;
|
||||
|
||||
const func string: vigenereDecipher (in string: source, in var string: keyword) is func
|
||||
result
|
||||
var string: dest is "";
|
||||
local
|
||||
var char: ch is ' ';
|
||||
var integer: index is 0;
|
||||
var integer: shift is 0;
|
||||
begin
|
||||
keyword := upper(keyword);
|
||||
for ch key index range source do
|
||||
if ch in {'A' .. 'Z'} | {'a' .. 'z'} then
|
||||
shift := ord(keyword[succ(pred(index) rem length(keyword))]) - ord('A');
|
||||
dest &:= chr(ord('A') + (ord(upper(ch)) - ord('A') - shift) mod 26);
|
||||
end if;
|
||||
end for;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
const string: input is "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
const string: keyword is "VIGENERECIPHER";
|
||||
var string: encrypted is "";
|
||||
var string: decrypted is "";
|
||||
begin
|
||||
writeln("Input: " <& input);
|
||||
writeln("key: " <& keyword);
|
||||
encrypted := vigenereCipher(input, keyword);
|
||||
writeln("Encrypted: " <& encrypted);
|
||||
decrypted := vigenereDecipher(encrypted, keyword);
|
||||
writeln("Decrypted: " <& decrypted);
|
||||
end func;
|
||||
3
Task/Vigen-re-cipher/Smalltalk/vigen-re-cipher-1.st
Normal file
3
Task/Vigen-re-cipher/Smalltalk/vigen-re-cipher-1.st
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
prep := [:s | s select:[:ch | ch isLetter] thenCollect:[:ch | ch asUppercase]].
|
||||
encrypt := [:s :cypher | (prep value:s) keysAndValuesCollect:[:i :ch | ch rot:((cypher at:((i-1)\\key size+1))-$A) ]].
|
||||
decrypt := [:s :cypher | (prep value:s) keysAndValuesCollect:[:i :ch | ch rot:26-((cypher at:((i-1)\\key size+1))-$A) ]].
|
||||
7
Task/Vigen-re-cipher/Smalltalk/vigen-re-cipher-2.st
Normal file
7
Task/Vigen-re-cipher/Smalltalk/vigen-re-cipher-2.st
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
plain := 'Beware the Jabberwock, my son! The jaws that bite, the claws that catch!'.
|
||||
cypher := 'VIGENERECIPHER'.
|
||||
crypted := encrypt value:plain value:cypher.
|
||||
plain2 := decrypt value:crypted value:cypher.
|
||||
|
||||
crypted -> 'WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY'
|
||||
plain2 -> 'BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH'
|
||||
24
Task/Vigen-re-cipher/TXR/vigen-re-cipher.txr
Normal file
24
Task/Vigen-re-cipher/TXR/vigen-re-cipher.txr
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
@(next :args)
|
||||
@(do
|
||||
(defun letter-mod26-op (func) ;; add or subtract capital letters modulo 26
|
||||
(lambda (a b) (+ #\A (mod (call func (- a #\A) (- b #\A)) 26))))
|
||||
(defun vig (msg key encrypt)
|
||||
(cat-str (mapcar (letter-mod26-op (if encrypt (fun +) (fun -)))
|
||||
(list-str msg)
|
||||
(repeat (list-str key))) "")))
|
||||
@(coll)@{key /[A-Za-z]/}@(end)
|
||||
@(coll)@{msg /[A-Za-z]/}@(end)
|
||||
@(cat key "")
|
||||
@(filter :upcase key)
|
||||
@(cat msg "")
|
||||
@(filter :upcase msg)
|
||||
@(bind encoded @(vig msg key t))
|
||||
@(bind decoded @(vig msg key nil))
|
||||
@(bind check @(vig encoded key nil))
|
||||
@(output)
|
||||
text: @msg
|
||||
key: @key
|
||||
enc: @encoded
|
||||
dec: @decoded
|
||||
check: @check
|
||||
@(end)
|
||||
36
Task/Vigen-re-cipher/Tcl/vigen-re-cipher-1.tcl
Normal file
36
Task/Vigen-re-cipher/Tcl/vigen-re-cipher-1.tcl
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package require Tcl 8.6
|
||||
|
||||
oo::class create Vigenere {
|
||||
variable key
|
||||
constructor {protoKey} {
|
||||
foreach c [split $protoKey ""] {
|
||||
if {[regexp {[A-Za-z]} $c]} {
|
||||
lappend key [scan [string toupper $c] %c]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
method encrypt {text} {
|
||||
set out ""
|
||||
set j 0
|
||||
foreach c [split $text ""] {
|
||||
if {[regexp {[^a-zA-Z]} $c]} continue
|
||||
scan [string toupper $c] %c c
|
||||
append out [format %c [expr {($c+[lindex $key $j]-130)%26+65}]]
|
||||
set j [expr {($j+1) % [llength $key]}]
|
||||
}
|
||||
return $out
|
||||
}
|
||||
|
||||
method decrypt {text} {
|
||||
set out ""
|
||||
set j 0
|
||||
foreach c [split $text ""] {
|
||||
if {[regexp {[^A-Z]} $c]} continue
|
||||
scan $c %c c
|
||||
append out [format %c [expr {($c-[lindex $key $j]+26)%26+65}]]
|
||||
set j [expr {($j+1) % [llength $key]}]
|
||||
}
|
||||
return $out
|
||||
}
|
||||
}
|
||||
7
Task/Vigen-re-cipher/Tcl/vigen-re-cipher-2.tcl
Normal file
7
Task/Vigen-re-cipher/Tcl/vigen-re-cipher-2.tcl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
set cypher [Vigenere new "Vigenere Cipher"]
|
||||
set original "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
set encrypted [$cypher encrypt $original]
|
||||
set decrypted [$cypher decrypt $encrypted]
|
||||
puts $original
|
||||
puts "Encrypted: $encrypted"
|
||||
puts "Decrypted: $decrypted"
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
Get_Input(10, "Key: ", STATLINE+NOCR) // @10 = key
|
||||
Reg_Copy_Block(11, Cur_Pos, EOL_Pos) // @11 = copy of original text
|
||||
EOL Ins_Newline
|
||||
Ins_Text("Key = ") Reg_Ins(10) Ins_Newline
|
||||
|
||||
// Prepare the key into numeric registers #130..:
|
||||
Buf_Switch(Buf_Free)
|
||||
Reg_Ins(10)
|
||||
Case_Upper_Block(0, Cur_Pos)
|
||||
BOF
|
||||
#2 = Reg_Size(10) // #2 = key length
|
||||
for (#3=130; #3 < 130+#2; #3++) {
|
||||
#@3 = Cur_Char
|
||||
Char(1)
|
||||
}
|
||||
Buf_Quit(OK)
|
||||
|
||||
Ins_Text("Encrypted: ")
|
||||
#4 = Cur_Pos
|
||||
Reg_Ins(11) // copy of original text
|
||||
Replace_Block("|!|A", "", #4, EOL_Pos, BEGIN+ALL+NOERR) // remove non-alpha chars
|
||||
Case_Upper_Block(#4, EOL_Pos) // convert to upper case
|
||||
Goto_Pos(#4)
|
||||
#1 = 1; Call("ENCRYPT_DECRYPT") // Encrypt the line
|
||||
Reg_Copy_Block(11, #4, Cur_Pos) // Copy encrypted text text to next line
|
||||
Ins_Newline
|
||||
Ins_Text("Decrypted: ")
|
||||
Reg_Ins(11, BEGIN)
|
||||
#1 = -1; Call("ENCRYPT_DECRYPT") // Decrypt the line
|
||||
|
||||
Return
|
||||
|
||||
// Encrypt or decrypt text on current line in-place, starting from cursor position.
|
||||
// in: #1 = direction (1=encrypt, -1=decrypt)
|
||||
// #2 = key length, #130...#189 = the key
|
||||
//
|
||||
:ENCRYPT_DECRYPT:
|
||||
Num_Push(6,9)
|
||||
#6 = 0
|
||||
While (!At_EOL) {
|
||||
#7 = #6+130 // pointer to key array
|
||||
#8 = #@7 // get key character
|
||||
#9 = (Cur_Char + #8*#1 + 26) % 26 + 'A' // decrypt/encrypt
|
||||
Ins_Char(#9, OVERWRITE) // write the converted char
|
||||
#6 = (#6+1) % #2
|
||||
}
|
||||
Num_Pop(6,9)
|
||||
Return
|
||||
21
Task/Vigen-re-cipher/XPL0/vigen-re-cipher.xpl0
Normal file
21
Task/Vigen-re-cipher/XPL0/vigen-re-cipher.xpl0
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
code ChIn=7, ChOut=8;
|
||||
int Neg, C, Len, I, Key;
|
||||
char KeyWord(80);
|
||||
[Neg:= false; \skip to KEYWORD
|
||||
repeat C:= ChIn(8); if C=^- then Neg:= true; until C>=^A & C<=^Z;
|
||||
Len:= 0; \read in KEYWORD
|
||||
repeat KeyWord(Len):= C-^A; Len:= Len+1; C:= ChIn(8); until C<^A ! C>^Z;
|
||||
I:= 0; \initialize cycling index
|
||||
repeat C:= ChIn(1);
|
||||
if C>=^a & C<=^z then C:= C-$20; \capitalize
|
||||
if C>=^A & C<=^Z then \discard non-alphas
|
||||
[Key:= KeyWord(I); I:= I+1; if I>=Len then I:= 0;
|
||||
if Neg then Key:= -Key; \decrypting?
|
||||
C:= C+Key;
|
||||
if C>^Z then C:= C-26
|
||||
else if C<^A then C:= C+26;
|
||||
ChOut(0, C);
|
||||
];
|
||||
until C=$1A; \EOF
|
||||
ChOut(0, $1A); \encrypted file must end with EOF otherwise the decode will hang
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue