September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,63 +1,55 @@
|
|||
with Ada.Text_IO;
|
||||
WITH Ada.Text_IO, Ada.Characters.Handling;
|
||||
USE Ada.Text_IO, Ada.Characters.Handling;
|
||||
|
||||
procedure Vignere_Cipher is
|
||||
PROCEDURE Main IS
|
||||
SUBTYPE Alpha IS Character RANGE 'A' .. 'Z';
|
||||
TYPE Ring IS MOD (Alpha'Pos (Alpha'Last)-Alpha'Pos (Alpha'First) + 1);
|
||||
TYPE Seq IS ARRAY (Integer RANGE <>) OF Ring;
|
||||
|
||||
subtype Letter is Character range 'A' .. 'Z';
|
||||
subtype Lowercase is Character range 'a' .. 'z';
|
||||
FUNCTION "+" (S, Key : Seq) RETURN Seq IS
|
||||
R : Seq (S'Range);
|
||||
BEGIN
|
||||
FOR I IN R'Range LOOP
|
||||
R (I) := S (I) + Key (Key'First + (I - R'First) MOD Key'Length);
|
||||
END LOOP;
|
||||
RETURN R;
|
||||
END "+";
|
||||
|
||||
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 "-" (S : Seq) RETURN Seq IS
|
||||
R : Seq (S'Range);
|
||||
BEGIN
|
||||
FOR I IN R'Range LOOP
|
||||
R (I) := - S (I);
|
||||
END LOOP;
|
||||
RETURN R;
|
||||
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 To_Seq (S : String) RETURN Seq IS
|
||||
R : Seq (S'Range);
|
||||
I : Integer := R'First;
|
||||
BEGIN
|
||||
FOR C OF To_Upper (S) LOOP
|
||||
IF C IN Alpha THEN
|
||||
R (I) := Ring'Mod (Alpha'Pos (C) - Alpha'Pos (Alpha'First));
|
||||
I := I + 1;
|
||||
END IF;
|
||||
END LOOP;
|
||||
RETURN R (R'First .. I - 1);
|
||||
END To_Seq;
|
||||
|
||||
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 To_String (S : Seq ) RETURN String IS
|
||||
R : String (S'Range);
|
||||
BEGIN
|
||||
FOR I IN R'Range LOOP
|
||||
R (I) := Alpha'Val ( Integer (S (I)) + Alpha'Pos (Alpha'First));
|
||||
END LOOP;
|
||||
RETURN R;
|
||||
END To_String;
|
||||
|
||||
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;
|
||||
Input : Seq := To_Seq (Get_Line);
|
||||
Key : Seq := To_Seq (Get_Line);
|
||||
Crypt : Seq := Input + Key;
|
||||
BEGIN
|
||||
Put_Line ("Encrypted: " & To_String (Crypt));
|
||||
Put_Line ("Decrypted: " & To_String (Crypt + (-Key)));
|
||||
END Main;
|
||||
|
|
|
|||
25
Task/Vigen-re-cipher/Ceylon/vigen-re-cipher.ceylon
Normal file
25
Task/Vigen-re-cipher/Ceylon/vigen-re-cipher.ceylon
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
shared void run() {
|
||||
|
||||
function normalize(String text) => text.uppercased.filter(Character.letter);
|
||||
|
||||
function crypt(String text, String key, Character(Character, Character) transform) => String {
|
||||
for ([a, b] in zipPairs(normalize(text), normalize(key).cycled))
|
||||
transform(a, b)
|
||||
};
|
||||
|
||||
function encrypt(String clearText, String key) =>
|
||||
crypt(clearText, key, (Character a, Character b) =>
|
||||
('A'.integer + ((a.integer + b.integer - 130) % 26)).character);
|
||||
|
||||
function decrypt(String cipherText, String key) =>
|
||||
crypt(cipherText, key, (Character a, Character b) =>
|
||||
('A'.integer + ((a.integer - b.integer + 26) % 26)).character);
|
||||
|
||||
value key = "VIGENERECIPHER";
|
||||
value message = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
value encrypted = encrypt(message, key);
|
||||
value decrypted = decrypt(encrypted, key);
|
||||
|
||||
print(encrypted);
|
||||
print(decrypted);
|
||||
}
|
||||
|
|
@ -1,48 +1,48 @@
|
|||
import system'text.
|
||||
import system'math.
|
||||
import system'routines.
|
||||
import extensions.
|
||||
import system'text;
|
||||
import system'math;
|
||||
import system'routines;
|
||||
import extensions;
|
||||
|
||||
class VCipher
|
||||
{
|
||||
literal encrypt(LiteralValue txt, LiteralValue pw, IntNumber d)
|
||||
[
|
||||
var output := TextBuilder new.
|
||||
int pwi := 0.
|
||||
string encrypt(string txt, string pw, int d)
|
||||
{
|
||||
auto output := new TextBuilder();
|
||||
int pwi := 0;
|
||||
|
||||
literal $pw := pw upperCase.
|
||||
string PW := pw.upperCase();
|
||||
|
||||
txt upperCase; forEach(:t)
|
||||
[
|
||||
txt.upperCase().forEach:(t)
|
||||
{
|
||||
if(t >= $65)
|
||||
[
|
||||
int tmp := t toInt - 65 + d * ($pw[pwi] toInt - 65).
|
||||
{
|
||||
int tmp := t.toInt() - 65 + d * (PW[pwi].toInt() - 65);
|
||||
if (tmp < 0)
|
||||
[
|
||||
{
|
||||
tmp += 26
|
||||
].
|
||||
output write((65 + tmp mod:26) toChar).
|
||||
pwi += 1.
|
||||
if (pwi == $pw length) [ pwi := 0 ]
|
||||
]
|
||||
].
|
||||
};
|
||||
output.write((65 + tmp.mod:26).toChar());
|
||||
pwi += 1;
|
||||
if (pwi == PW.Length) { pwi := 0 }
|
||||
}
|
||||
};
|
||||
|
||||
^ output literal
|
||||
]
|
||||
^ output.Value
|
||||
}
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
var v := VCipher new.
|
||||
public program()
|
||||
{
|
||||
var v := new VCipher();
|
||||
|
||||
var s0 := "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!".
|
||||
var pw := "VIGENERECIPHER".
|
||||
var s0 := "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
var pw := "VIGENERECIPHER";
|
||||
|
||||
console printLine(s0,'newLine,pw,'newLine).
|
||||
var s1 := v encrypt(s0, pw, 1).
|
||||
console printLine("Encrypted:",s1).
|
||||
s1 := v encrypt(s1, "VIGENERECIPHER", -1).
|
||||
console printLine("Decrypted:",s1).
|
||||
console printLine("Press any key to continue..").
|
||||
console readKey.
|
||||
].
|
||||
console.printLine(s0,newLine,pw,newLine);
|
||||
var s1 := v.encrypt(s0, pw, 1);
|
||||
console.printLine("Encrypted:",s1);
|
||||
s1 := v.encrypt(s1, "VIGENERECIPHER", -1);
|
||||
console.printLine("Decrypted:",s1);
|
||||
console.printLine("Press any key to continue..");
|
||||
console.readChar()
|
||||
}
|
||||
|
|
|
|||
49
Task/Vigen-re-cipher/Python/vigen-re-cipher.py
Normal file
49
Task/Vigen-re-cipher/Python/vigen-re-cipher.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
'''Vigenere encryption and decryption'''
|
||||
|
||||
from itertools import starmap, cycle
|
||||
|
||||
|
||||
def encrypt(message, key):
|
||||
'''Vigenere encryption of message using key.'''
|
||||
|
||||
# Converted to uppercase.
|
||||
# Non-alpha characters stripped out.
|
||||
message = filter(str.isalpha, message.upper())
|
||||
|
||||
def enc(c, k):
|
||||
'''Single letter encryption.'''
|
||||
|
||||
return chr(((ord(k) + ord(c) - 2 * ord('A')) % 26) + ord('A'))
|
||||
|
||||
return ''.join(starmap(enc, zip(message, cycle(key))))
|
||||
|
||||
|
||||
def decrypt(message, key):
|
||||
'''Vigenere decryption of message using key.'''
|
||||
|
||||
def dec(c, k):
|
||||
'''Single letter decryption.'''
|
||||
|
||||
return chr(((ord(c) - ord(k) - 2 * ord('A')) % 26) + ord('A'))
|
||||
|
||||
return ''.join(starmap(dec, zip(message, cycle(key))))
|
||||
|
||||
|
||||
def main():
|
||||
'''Demonstration'''
|
||||
|
||||
text = 'Beware the Jabberwock, my son! The jaws that bite, ' + (
|
||||
'the claws that catch!'
|
||||
)
|
||||
key = 'VIGENERECIPHER'
|
||||
|
||||
encr = encrypt(text, key)
|
||||
decr = decrypt(encr, key)
|
||||
|
||||
print(text)
|
||||
print(encr)
|
||||
print(decr)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue