This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,52 @@
class Main
{
static Int shift (Int char, Int key)
{
newChar := char + key
if (char >= 'a' && char <= 'z')
{
if (newChar - 'a' < 0) { newChar += 26 }
if (newChar - 'a' >= 26) { newChar -= 26 }
}
else if (char >= 'A' && char <= 'Z')
{
if (newChar - 'A' < 0) { newChar += 26 }
if (newChar - 'A' >= 26) { newChar -= 26 }
}
else // not alphabetic, so keep as is
{
newChar = char
}
return newChar
}
static Str shiftStr (Str msg, Int key)
{
res := StrBuf()
msg.each { res.addChar (shift(it, key)) }
return res.toStr
}
static Str encode (Str msg, Int key)
{
return shiftStr (msg, key)
}
static Str decode (Str msg, Int key)
{
return shiftStr (msg, -key)
}
static Void main (Str[] args)
{
if (args.size == 2)
{
msg := args[0]
key := Int(args[1])
echo ("$msg with key $key")
echo ("Encode: ${encode(msg, key)}")
echo ("Decode: ${decode(encode(msg, key), key)}")
}
}
}

View file

@ -0,0 +1,26 @@
CaesarCipher := function(s, n)
local r, c, i, lower, upper;
lower := "abcdefghijklmnopqrstuvwxyz";
upper := "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
r := "";
for c in s do
i := Position(lower, c);
if i <> fail then
Add(r, lower[RemInt(i + n - 1, 26) + 1]);
else
i := Position(upper, c);
if i <> fail then
Add(r, upper[RemInt(i + n - 1, 26) + 1]);
else
Add(r, c);
fi;
fi;
od;
return r;
end;
CaesarCipher("IBM", 25);
# "HAL"
CaesarCipher("Vgg cphvi wzdibn vmz wjmi amzz viy zlpvg di ydbidot viy mdbcon.", 5);
# "All human beings are born free and equal in dignity and rights."

View file

@ -0,0 +1,24 @@
def caeserEncode(int cipherKey, String text) {
def builder = new StringBuilder()
text.each { character ->
int ch = character[0] as char
switch(ch) {
case 'a'..'z': ch = ((ch - 97 + cipherKey) % 26 + 97); break
case 'A'..'Z': ch = ((ch - 65 + cipherKey) % 26 + 65); break
}
builder.append(ch as char)
}
builder.toString()
}
def caeserDecode(int cipherKey, String text) { caeserEncode(26 - cipherKey, text) }
def plainText = "The Quick Brown Fox jumped over the lazy dog"
def cipherKey = 12
def cipherText = caeserEncode(cipherKey, plainText)
def decodedText = caeserDecode(cipherKey, cipherText)
println "plainText: $plainText"
println "cypherText($cipherKey): $cipherText"
println "decodedText($cipherKey): $decodedText"
assert plainText == decodedText

View file

@ -0,0 +1,16 @@
procedure main()
ctext := caesar(ptext := map("The quick brown fox jumped over the lazy dog"))
dtext := caesar(ctext,,"decrypt")
write("Plain text = ",image(ptext))
write("Encphered text = ",image(ctext))
write("Decphered text = ",image(dtext))
end
procedure caesar(text,k,mode) #: mono-alphabetic shift cipher
/k := 3
k := (((k % *&lcase) + *&lcase) % *&lcase) + 1
case mode of {
&null|"e"|"encrypt": return map(text,&lcase,(&lcase||&lcase)[k+:*&lcase])
"d"|"decrypt" : return map(text,(&lcase||&lcase)[k+:*&lcase],&lcase)
}
end

View file

@ -0,0 +1,2 @@
cndx=: [: , 65 97 +/ 26 | (i.26)&+
caesar=: (cndx 0)}&a.@u:@cndx@[ {~ a.i.]

View file

@ -0,0 +1,2 @@
2 caesar 'This simple "monoalphabetic substitution cipher" provides almost no security, ...'
Vjku ukorng "oqpqcnrjcdgvke uwduvkvwvkqp ekrjgt" rtqxkfgu cnoquv pq ugewtkva, ...

View file

@ -0,0 +1 @@
CAESAR=:1 :'(26|m&+)&.((26{.64}.a.)&i.)'

View file

@ -0,0 +1,2 @@
20 CAESAR 'HI'
BC

View file

@ -0,0 +1,24 @@
key = 7
Print "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
'Encrypt the text
Print CaesarCypher$("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", key)
'Decrypt the text by changing the key to (26 - key)
Print CaesarCypher$(CaesarCypher$("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", key), (26 - key))
Function CaesarCypher$(string$, key)
If (key < 0) Or (key > 25) Then _
CaesarCypher$ = "Key is Ouside of Bounds" : Exit Function
For i = 1 To Len(string$)
rotate = Asc(Mid$(string$, i, 1))
rotate = (rotate + key)
If Asc(Mid$(string$, i, 1)) > Asc("Z") Then
If rotate > Asc("z") Then rotate = (Asc("a") + (rotate - Asc("z")) - 1)
Else
If rotate > Asc("Z") Then rotate = (Asc("A") + (rotate - Asc("Z")) - 1)
End If
CaesarCypher$ = (CaesarCypher$ + Chr$(rotate))
Next i
End Function

View file

@ -0,0 +1,34 @@
; some useful constants
make "lower_a ascii "a
make "lower_z ascii "z
make "upper_a ascii "A
make "upper_z ascii "Z
; encipher a single character
to encipher_char :char :key
local "code make "code ascii :char
local "base make "base 0
ifelse [and (:code >= :lower_a) (:code <= :lower_z)] [make "base :lower_a] [
if [and (:code >= :upper_a) (:code <= :upper_z)] [make "base :upper_a] ]
ifelse [:base > 0] [
output char (:base + (modulo ( :code - :base + :key ) 26 ))
] [
output :char
]
end
; encipher a whole string
to caesar_cipher :string :key
output map [encipher_char ? :key] :string
end
; Demo
make "plaintext "|The five boxing wizards jump quickly|
make "key 3
make "ciphertext caesar_cipher :plaintext :key
make "recovered caesar_cipher :ciphertext -:key
print sentence "| Original:| :plaintext
print sentence "|Encrypted:| :ciphertext
print sentence "|Recovered:| :recovered
bye

View file

@ -0,0 +1,5 @@
> StringTools:-Encode( "The five boxing wizards jump quickly", encoding = alpharot[3] );
"Wkh ilyh eralqj zlcdugv mxps txlfnob"
> StringTools:-Encode( %, encoding = alpharot[ 23 ] );
"The five boxing wizards jump quickly"

View file

@ -0,0 +1 @@
cypher[mesg_String,n_Integer]:=StringReplace[mesg,Flatten[Thread[Rule[#,RotateLeft[#,3]]]&/@CharacterRange@@@{{"a","z"},{"A","Z"}}]]