This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View 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));
}

View file

@ -0,0 +1,25 @@
import std.stdio, std.range, std.ascii, std.string, std.algorithm,
std.conv;
enum mod = (in int m, in int n) pure nothrow => ((m % n) + n) % n;
enum _s2v = (in string s) pure /*nothrow*/ =>
s.toUpper.removechars("^A-Z").map!q{ a - 'A' };
string _v2s(R)(R v) pure /*nothrow*/ {
return v.map!(x => uppercase[x.mod(26)]).text;
}
enum encrypt = (in string txt, in string key) pure /*nothrow*/ =>
txt._s2v.zip(key._s2v.cycle).map!q{ a[0] + a[1] }._v2s;
enum decrypt = (in string txt, in string key) pure /*nothrow*/ =>
txt._s2v.zip(key._s2v.cycle).map!q{ a[0] - a[1] }._v2s;
void main() {
immutable key = "Vigenere Cipher!!!";
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));
}

View file

@ -0,0 +1,103 @@
// The Vigenere cipher in reasonably standard Pascal
// <no library functions: all conversions hand-coded>
PROGRAM Vigenere;
// get a letter's alphabetic position (A=0)
FUNCTION letternum(letter: CHAR): BYTE;
BEGIN
letternum := (ord(letter)-ord('A'));
END;
// convert a character to uppercase
FUNCTION uch(ch: CHAR): CHAR;
BEGIN
uch := ch;
IF ch IN ['a'..'z'] THEN
uch := chr(ord(ch) AND $5F);
END;
// convert a string to uppercase
FUNCTION ucase(str: STRING): STRING;
VAR i: BYTE;
BEGIN
ucase := '';
FOR i := 1 TO Length(str) DO
ucase := ucase + uch(str[i]);
END;
// construct a Vigenere-compatible string:
// uppercase; no spaces or punctuation.
FUNCTION vstr(pt: STRING): STRING;
VAR c: Cardinal;
s: STRING;
BEGIN
vstr:= '';
s := ucase(pt);
FOR c := 1 TO Length(s) DO BEGIN
IF s[c] IN ['A'..'Z'] THEN
vstr += s[c];
END;
END;
// construct a repeating Vigenere key
FUNCTION vkey(pt, key: STRING): STRING;
VAR c,n: Cardinal;
k : STRING;
BEGIN
k := vstr(key);
vkey := '';
FOR c := 1 TO Length(pt) DO BEGIN
n := c mod Length(k);
IF n>0 THEN vkey += k[n] ELSE vkey += k[Length(k)];
END;
END;
// Vigenere encipher
FUNCTION enVig(pt,key:STRING): STRING;
VAR ct: STRING;
c,n : Cardinal;
BEGIN
ct := pt;
FOR c := 1 TO Length(pt) DO BEGIN
n := letternum(pt[c])+letternum(key[c]);
n := n mod 26;
ct[c]:=chr(ord('A')+n);
END;
enVig := ct;
END;
// Vigenere decipher
FUNCTION deVig(ct,key:STRING): STRING;
VAR pt : STRING;
c,n : INTEGER;
BEGIN
pt := ct;
FOR c := 1 TO Length(ct) DO BEGIN
n := letternum(ct[c])-letternum(key[c]);
IF n<0 THEN n:=26+n;
pt[c]:=chr(ord('A')+n);
END;
deVig := pt;
END;
VAR key: STRING = 'Vigenere cipher';
msg: STRING = 'Beware the Jabberwock! The jaws that bite, the claws that catch!';
vtx: STRING = '';
ctx: STRING = '';
ptx: STRING = '';
BEGIN
// make Vigenere-compatible
vtx := vstr(msg);
key := vkey(vtx,key);
// Vigenere encipher / decipher
ctx := enVig(vtx,key);
ptx := deVig(ctx,key);
// display results
Writeln('Message : ',msg);
Writeln('Plaintext : ',vtx);
Writeln('Key : ',key);
Writeln('Ciphertext : ',ctx);
Writeln('Plaintext : ',ptx);
END.

View file

@ -0,0 +1,21 @@
#lang racket
(define chr integer->char)
(define ord char->integer)
(define (encrypt msg key)
(define cleaned
(list->string
(for/list ([c (string-upcase msg)]
#:when (char-alphabetic? c)) c)))
(list->string
(for/list ([c cleaned] [k (in-cycle key)])
(chr (+ (modulo (+ (ord c) (ord k)) 26) (ord #\A))))))
(define (decrypt msg key)
(list->string
(for/list ([c msg] [k (in-cycle key)])
(chr (+ (modulo (- (ord c) (ord k)) 26) (ord #\A))))))
(decrypt (encrypt "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
"VIGENERECIPHER")
"VIGENERECIPHER")

View file

@ -0,0 +1 @@
"BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH"