Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -9,3 +9,9 @@ This simple "monoalphabetic substitution cipher" provides almost no security, be
Caesar cipher is identical to [[Vigenère cipher]] with a key of length 1. <br>
Also, [[Rot-13]] is identical to Caesar cipher with key 13.
;See also:
* [[Rot-13]]
* [[Substitution Cipher]]
* [[Vigenère Cipher/Cryptanalysis]]
<br>

View file

@ -0,0 +1,3 @@
65+>>>>10p100p1>:v:+>#*,#g1#0-#0:#!<<
"`"::_@#!`\*84:<~<$<^+"A"%*2+9<v"{"\`
**-"A"-::0\`\55*`+#^_\0g+"4"+4^>\`*48

View file

@ -0,0 +1,3 @@
65+>>>>10p100p1>:v:+>#*,#g1#0-#0:#!<<
"`"::_@#!`\*84:<~<$<^+"A"%*2+9<v"{"\`
**-"A"-::0\`\55*`+#^_\0g-"4"+4^>\`*48

View file

@ -1,74 +1,71 @@
#define system.
#define system'routines.
#define extensions.
#define system'math.
// --- Constants ---
#define extensions.
#symbol Letters = "abcdefghijklmnopqrstuvwxyz".
#symbol BigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
#symbol TestText = "Pack my box with five dozen liquor jugs.".
#symbol Key = 12.
// --- Encrypt / Decript ---
#class Encrypting
#class Encrypting :: Enumerator
{
#field theKey.
#field theExtendee.
#field theEnumerator.
#constructor new : aKey &extending:anObject
#constructor new &key:aKey &text:aText
[
theKey := aKey.
theExtendee := anObject.
theEnumerator := aText enumerator.
]
#method eval : aChar
#method next => theEnumerator.
#method reset => theEnumerator.
#method get
[
#var anIndex := Letters indexOf &index:0 &char:aChar.
#var aChar := theEnumerator get.
#var anIndex := Letters indexOf:0:aChar.
(-1 < anIndex)
? [
theExtendee eval:(Letters @ ((theKey+anIndex) int mod:26))
^ Letters @ ((theKey+anIndex) mod:26).
]
! [
anIndex := BigLetters indexOf &index:0 &char:aChar.
anIndex := BigLetters indexOf:0:aChar.
(-1 < anIndex)
? [
theExtendee eval:(BigLetters @ ((theKey+anIndex) int mod:26))
^ BigLetters @ ((theKey+anIndex) mod:26).
]
! [
theExtendee eval:aChar.
^ aChar.
].
].
]
#method => theExtendee.
}
// --- Functions ---
#class(extension)encryptOp
{
#method encrypt : aKey
= Encrypting new &key:aKey &text:self summarize:(String new).
#symbol encrypt = (:aText:aKey)
[ Encrypting new:aKey &extending:(Summing new:(String new)) foreach:aText literal ].
#symbol decrypt = (:aText:aKey)
[ Encrypting new:(26 - aKey) &extending:(Summing new:(String new)) foreach:aText literal ].
// --- Program ---
#method decrypt :aKey
= Encrypting new &key:(26 - aKey) &text:self summarize:(String new).
}
#symbol program =
[
#var anS := TestText.
console writeLine:"Original text :" :TestText.
consoleEx writeLine:"Original text :" :anS.
#var anEncryptedText := TestText encrypt:Key.
anS := encrypt:anS:Key.
console writeLine:"Encrypted text:" :anEncryptedText.
consoleEx writeLine:"Encrypted text:" :anS.
#var aDecryptedText := anEncryptedText decrypt:Key.
anS := decrypt:anS:Key.
console writeLine:"Decrypted text:" :aDecryptedText.
consoleEx writeLine:"Decrypted text:" :anS.
consoleEx readChar.
console readChar.
].

View file

@ -0,0 +1,18 @@
defmodule Caesar_cipher do
defp set_map(map, range, key) do
org = Enum.map(range, &List.to_string [&1])
{a, b} = Enum.split(org, key)
Enum.zip(org, b ++ a) |> Enum.into(map)
end
def encode(text, key) do
map = Map.new |> set_map(?a..?z, key) |> set_map(?A..?Z, key)
String.codepoints(text) |> Enum.map_join(fn c -> Dict.get(map, c, c) end)
end
end
text = "The five boxing wizards jump quickly"
key = 3
IO.puts "Original: #{text}"
IO.puts "Encrypted: #{enc = Caesar_cipher.encode(text, key)}"
IO.puts "Decrypted: #{Caesar_cipher.encode(enc, -key)}"

View file

@ -0,0 +1,26 @@
# based on Rot-13 solution: http://rosettacode.org/wiki/Rot-13#R
ceasar <- function(x, key)
{
# if key is negative, wrap to be positive
if (key < 0) {
key <- 26 + key
}
old <- paste(letters, LETTERS, collapse="", sep="")
new <- paste(substr(old, key * 2 + 1, 52), substr(old, 1, key * 2), sep="")
chartr(old, new, x)
}
# simple examples from description
print(ceasar("hi",2))
print(ceasar("hi",20))
# more advanced example
key <- 3
plaintext <- "The five boxing wizards jump quickly."
cyphertext <- ceasar(plaintext, key)
decrypted <- ceasar(cyphertext, -key)
print(paste(" Plain Text: ", plaintext, sep=""))
print(paste(" Cypher Text: ", cyphertext, sep=""))
print(paste("Decrypted Text: ", decrypted, sep=""))