langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,92 @@
/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
messages = [ -
'The five boxing wizards jump quickly', -
'Attack at dawn!', -
'HI']
keys = [1, 2, 20, 25, 13]
loop m_ = 0 to messages.length - 1
in = messages[m_]
loop k_ = 0 to keys.length - 1
say 'Caesar cipher, key:' keys[k_].right(3)
ec = caesar_encipher(in, keys[k_])
dc = caesar_decipher(ec, keys[k_])
say in
say ec
say dc
say
end k_
say 'Rot-13:'
ec = rot13(in)
dc = rot13(ec)
say in
say ec
say dc
say
end m_
return
method rot13(input) public static signals IllegalArgumentException
return caesar(input, 13, isFalse)
method caesar(input = Rexx, idx = int, caps = boolean) public static signals IllegalArgumentException
if idx < 1 | idx > 25 then signal IllegalArgumentException()
-- 12345678901234567890123456
itab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
shift = itab.length - idx
parse itab tl +(shift) tr
otab = tr || tl
if caps then input = input.upper
cipher = input.translate(itab || itab.lower, otab || otab.lower)
return cipher
method caesar_encipher(input = Rexx, idx = int, caps = boolean) public static signals IllegalArgumentException
return caesar(input, idx, caps)
method caesar_decipher(input = Rexx, idx = int, caps = boolean) public static signals IllegalArgumentException
return caesar(input, int(26) - idx, isFalse)
method caesar_encipher(input = Rexx, idx = int) public static signals IllegalArgumentException
return caesar(input, idx, isFalse)
method caesar_decipher(input = Rexx, idx = int) public static signals IllegalArgumentException
return caesar(input, int(26) - idx, isFalse)
method caesar_encipher(input = Rexx, idx = int, opt = Rexx) public static signals IllegalArgumentException
return caesar(input, idx, opt)
method caesar_decipher(input = Rexx, idx = int, opt = Rexx) public static signals IllegalArgumentException
return caesar(input, int(26) - idx, opt)
method caesar(input = Rexx, idx = int, opt = Rexx) public static signals IllegalArgumentException
if opt.upper.abbrev('U') >= 1 then caps = isTrue
else caps = isFalse
return caesar(input, idx, caps)
method caesar(input = Rexx, idx = int) public static signals IllegalArgumentException
return caesar(input, idx, isFalse)
method isTrue public static returns boolean
return (1 == 1)
method isFalse public static returns boolean
return \isTrue

View file

@ -0,0 +1,51 @@
let islower c =
c >= 'a' && c <= 'z'
let isupper c =
c >= 'A' && c <= 'Z'
let rot x str =
let upchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
and lowchars = "abcdefghijklmnopqrstuvwxyz" in
let rec decal x =
if x < 0 then decal (x + 26) else x
in
let x = (decal x) mod 26 in
let decal_up = x - (int_of_char 'A')
and decal_low = x - (int_of_char 'a') in
let len = String.length str in
let res = String.create len in
for i = 0 to pred len do
let c = str.[i] in
if islower c then
let j = ((int_of_char c) + decal_low) mod 26 in
res.[i] <- lowchars.[j]
else if isupper c then
let j = ((int_of_char c) + decal_up) mod 26 in
res.[i] <- upchars.[j]
else
res.[i] <- c
done;
(res)
(* or in OCaml 4.00+:
let rot x =
let upchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
and lowchars = "abcdefghijklmnopqrstuvwxyz" in
let rec decal x =
if x < 0 then decal (x + 26) else x
in
let x = (decal x) mod 26 in
let decal_up = x - (int_of_char 'A')
and decal_low = x - (int_of_char 'a') in
String.map (fun c ->
if islower c then
let j = ((int_of_char c) + decal_low) mod 26 in
lowchars.[j]
else if isupper c then
let j = ((int_of_char c) + decal_up) mod 26 in
upchars.[j]
else
c
)
*)

View file

@ -0,0 +1,9 @@
let () =
let key = 3 in
let orig = "The five boxing wizards jump quickly" in
let enciphered = rot key orig in
print_endline enciphered;
let deciphered = rot (- key) enciphered in
print_endline deciphered;
Printf.printf "equal: %b\n" (orig = deciphered)
;;

View file

@ -0,0 +1,5 @@
enc(s,n)={
Strchr(Vecsmall(apply(k->if(k>96&&k<123,(k+n-97)%26+97, if(k>64&&k<91, (k+n-65)%26+65, k)),
Vec(Vecsmall(s)))))
};
dec(s,n)=enc(s,-n);

View file

@ -0,0 +1,20 @@
caesar: procedure options (main);
declare cypher_string character (52) static initial
((2)'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
declare (text, encyphered_text) character (100) varying,
offset fixed binary;
get edit (text) (L); /* Read in one line of text */
get list (offset);
if offset < 1 | offset > 25 then signal error;
put skip list ('Plain text=', text);
encyphered_text = translate(text, substr(cypher_string, offset+1, 26),
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' );
put skip list ('Encyphered text=', encyphered_text);
text = translate(encyphered_text, substr(cypher_string, 27-offset, 26),
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' );
put skip list ('Decyphered text=', text);
end caesar;

View file

@ -0,0 +1,37 @@
Program CaesarCipher(output);
procedure encrypt(var message: string; key: integer);
var
i: integer;
begin
for i := 1 to length(message) do
case message[i] of
'A'..'Z': message[i] := chr(ord('A') + (ord(message[i]) - ord('A') + key) mod 26);
'a'..'z': message[i] := chr(ord('a') + (ord(message[i]) - ord('a') + key) mod 26);
end;
end;
procedure decrypt(var message: string; key: integer);
var
i: integer;
begin
for i := 1 to length(message) do
case message[i] of
'A'..'Z': message[i] := chr(ord('A') + (ord(message[i]) - ord('A') - key + 26) mod 26);
'a'..'z': message[i] := chr(ord('a') + (ord(message[i]) - ord('a') - key + 26) mod 26);
end;
end;
var
key: integer;
message: string;
begin
key := 3;
message := 'The five boxing wizards jump quickly';
writeln ('Original message: ', message);
encrypt(message, key);
writeln ('Encrypted message: ', message);
decrypt(message, key);
writeln ('Decrypted message: ', message);
end.

View file

@ -0,0 +1,15 @@
my @alpha = 'A' .. 'Z';
sub encrypt ( $key where 1..25, $plaintext ) {
$plaintext.trans( @alpha Z=> @alpha.rotate($key) );
}
sub decrypt ( $key where 1..25, $cyphertext ) {
$cyphertext.trans( @alpha.rotate($key) Z=> @alpha );
}
my $original = 'THE FIVE BOXING WIZARDS JUMP QUICKLY';
my $en = encrypt( 13, $original );
my $de = decrypt( 13, $en );
.say for $original, $en, $de;
say 'OK' if $original eq all( map { .&decrypt(.&encrypt($original)) }, 1..25 );

View file

@ -0,0 +1,37 @@
Procedure.s CC_encrypt(plainText.s, key, reverse = 0)
;if reverse <> 0 then reverse the encryption (decrypt)
If reverse: reverse = 26: key = 26 - key: EndIf
Static alphabet$ = "ABCEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
Protected result.s, i, length = Len(plainText), letter.s, legal
If key < 1 Or key > 25: ProcedureReturn: EndIf ;keep key in range
For i = 1 To length
letter = Mid(plainText, i, 1)
legal = FindString(alphabet$, letter, 1 + reverse)
If legal
result + Mid(alphabet$, legal + key, 1)
Else
result + letter
EndIf
Next
ProcedureReturn result
EndProcedure
Procedure.s CC_decrypt(cypherText.s, key)
ProcedureReturn CC_encrypt(cypherText, key, 1)
EndProcedure
If OpenConsole()
Define key, plainText.s, encryptedText.s, decryptedText.s
key = Random(24) + 1 ;get a random key in the range 1 -> 25
plainText = "The quick brown fox jumped over the lazy dogs.": PrintN(RSet("Plain text = ", 17) + #DQUOTE$ + plainText + #DQUOTE$)
encryptedText = CC_encrypt(plainText, key): PrintN(RSet("Encrypted text = ", 17) + #DQUOTE$ + encryptedText + #DQUOTE$)
decryptedText = CC_decrypt(encryptedText, key): PrintN(RSet("Decrypted text = ", 17) + #DQUOTE$ + decryptedText + #DQUOTE$)
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf

View file

@ -0,0 +1,21 @@
Procedure.s CC_encrypt(text.s, key, reverse = 0)
;if reverse <> 0 then reverse the encryption (decrypt)
Protected i, *letter.Character, *resultLetter.Character, result.s = Space(Len(text))
If reverse: key = 26 - key: EndIf
If key < 1 Or key > 25: ProcedureReturn: EndIf ;exit if key out of range
*letter = @text: *resultLetter = @result
While *letter\c
Select *letter\c
Case 'A' To 'Z'
*resultLetter\c = ((*letter\c - 65 + key) % 26) + 65
Case 'a' To 'z'
*resultLetter\c = ((*letter\c - 97 + key) % 26) + 97
Default
*resultLetter\c = *letter\c
EndSelect
*letter + SizeOf(Character): *resultLetter + SizeOf(Character)
Wend
ProcedureReturn result
EndProcedure

View file

@ -0,0 +1,14 @@
{{
variable offset
: rotate ( cb-c ) tuck - @offset + 26 mod + ;
: rotate? ( c-c )
dup 'a 'z within [ 'a rotate ] ifTrue
dup 'A 'Z within [ 'A rotate ] ifTrue ;
---reveal---
: ceaser ( $n-$ )
!offset dup [ [ @ rotate? ] sip ! ] ^types'STRING each@ ;
}}
( Example )
"THEYBROKEOURCIPHEREVERYONECANREADTHIS" 3 ceaser ( returns encrypted string )
23 ceaser ( returns decrypted string )

View file

@ -0,0 +1,21 @@
input "Gimme a ofset:";ofst ' set any offset you like
a$ = "Pack my box with five dozen liquor jugs"
print " Original: ";a$
a$ = cipher$(a$,ofst)
print "Encrypted: ";a$
print "Decrypted: ";cipher$(a$,ofst+6)
FUNCTION cipher$(a$,ofst)
for i = 1 to len(a$)
aa$ = mid$(a$,i,1)
code$ = " "
if aa$ <> " " then
ua$ = upper$(aa$)
a = asc(ua$) - 64
code$ = chr$((((a mod 26) + ofst) mod 26) + 65)
if ua$ <> aa$ then code$ = lower$(code$)
end if
cipher$ = cipher$;code$
next i
END FUNCTION

View file

@ -0,0 +1,29 @@
$ include "seed7_05.s7i";
const func string: rot (in string: stri, in integer: encodingKey) is func
result
var string: encodedStri is "";
local
var char: ch is ' ';
var integer: index is 0;
begin
encodedStri := stri;
for ch key index range stri do
if ch >= 'a' and ch <= 'z' then
ch := chr((ord(ch) - ord('a') + encodingKey) rem 26 + ord('a'));
elsif ch >= 'A' and ch <= 'Z' then
ch := chr((ord(ch) - ord('A') + encodingKey) rem 26 + ord('A'));
end if;
encodedStri @:= [index] ch;
end for;
end func;
const proc: main is func
local
const integer: exampleKey is 3;
const string: testText is "The five boxing wizards jump quickly";
begin
writeln("Original: " <& testText);
writeln("Encrypted: " <& rot(testText, exampleKey));
writeln("Decrypted: " <& rot(rot(testText, exampleKey), 26 - exampleKey));
end func;

View file

@ -0,0 +1,24 @@
$$ MODE TUSCRIPT
text="THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
PRINT "text orginal ",text
abc="ABCDEFGHIJKLMNOPQRSTUVWXYZ",key=3,caesarskey=key+1
secretbeg=EXTRACT (abc,#caesarskey,0)
secretend=EXTRACT (abc,0,#caesarskey)
secretabc=CONCAT (secretbeg,secretend)
abc=STRINGS (abc,":</:"),secretabc=STRINGS (secretabc,":</:")
abc=SPLIT (abc), secretabc=SPLIT (secretabc)
abc2secret=JOIN(abc," ",secretabc),secret2abc=JOIN(secretabc," ",abc)
BUILD X_TABLE abc2secret=*
DATA {abc2secret}
BUILD X_TABLE secret2abc=*
DATA {secret2abc}
ENCODED = EXCHANGE (text,abc2secret)
PRINT "text encoded ",encoded
DECODED = EXCHANGE (encoded,secret2abc)
PRINT "encoded decoded ",decoded

View file

@ -0,0 +1,23 @@
@(next :args)
@(cases)
@{key /[0-9]+/}
@text
@(or)
@ (throw error "specify <key-num> <text>")
@(end)
@(do
(defvar k (int-str key 10)))
@(bind enc-dec
@(collect-each ((i (range 0 25)))
(let* ((p (tostringp (+ #\a i)))
(e (tostringp (+ #\a (mod (+ i k) 26))))
(P (upcase-str p))
(E (upcase-str e)))
'(((,p ,e) (,P ,E))
((,e ,p) (,E ,P))))))
@(deffilter enc . @(mappend (fun first) enc-dec))
@(deffilter dec . @(mappend (fun second) enc-dec))
@(output)
encoded: @{text :filter enc}
decoded: @{text :filter dec}
@(end)

View file

@ -0,0 +1,11 @@
#import std
#import nat
enc "n" = * -:~&@T ^p(rep"n" ~&zyC,~&)~~K30K31X letters # encryption function
dec "n" = * -:~&@T ^p(~&,rep"n" ~&zyC)~~K30K31X letters # decryption function
plaintext = 'the five boxing wizards jump quickly THE FIVE BOXING WIZARDS JUMP QUICKLY'
#show+ # exhaustive test
test = ("n". <.enc"n",dec"n"+ enc"n"> plaintext)*= nrange/1 25

View file

@ -0,0 +1,11 @@
#10 = Get_Num("Enter the key: positive to cipher, negative to de-cipher: ", STATLINE)
Goto_Pos(Block_Begin)
while(Cur_Pos < Block_End) {
#11 = Cur_Char & 0x60 + 1
if (Cur_Char >= 'A') {
Ins_Char((Cur_Char - #11 + 26 + #10) % 26 + #11, OVERWRITE)
} else {
Char(1)
}
}

View file

@ -0,0 +1,13 @@
code ChIn=7, ChOut=8, IntIn=10;
int Key, C;
[Key:= IntIn(8);
repeat C:= ChIn(1);
if C>=^a & C<=^z then C:= C-$20;
if C>=^A & C<=^Z then
[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
]