Merge pull request #13 from cormullion/myfork

Updates for v0.5 and v0.6
This commit is contained in:
Smaug123
2017-04-28 18:38:14 +01:00
committed by GitHub
10 changed files with 22 additions and 23 deletions

View File

@@ -1,2 +1,3 @@
julia 0.4
Iterators
Compat 0.9.5
Iterators

View File

@@ -2,6 +2,8 @@ module ClassicalCiphers
# Monoalphabetic
using Compat
include("common.jl")
include("monoalphabetic.jl")
include("caesar.jl")

View File

@@ -4,7 +4,7 @@ function letters_only(text)
end
function rotateRight(arr, n)
# implementation of the Mathematica function RotateRight
# implementation of the Mathematica function RotateRight - or you could try circshift()?
ans = copy(arr)
for i in 1:length(arr)
ans[i] = arr[((2*length(ans)+i-n-1) % length(ans)) + 1]
@@ -112,11 +112,11 @@ equal to their lowercase counterparts.
function index_of_coincidence(input)
freqs = frequencies(lowercase(letters_only(input)))
len = length(lowercase(letters_only(input)))
ans = 0
for i in 'a':'z'
ans += (x -> x*(x-1))(get(freqs, i, 0))
end
ans /= (len * (len-1) / 26)
end
end

View File

@@ -243,7 +243,7 @@ function encrypt_enigma{I <: Integer}(plaintext,
print(ans, working_ch)
end
uppercase(takebuf_string(ans))
uppercase(String(take!(ans)))
end
function decrypt_enigma(args1...; args2...)

View File

@@ -38,7 +38,7 @@ function encrypt_hill{I <: Integer}(plaintext, key::Array{I, 2})
print(ans, Char(x))
end
end
takebuf_string(ans)
String(take!(ans))
end

View File

@@ -1,5 +1,5 @@
function keystr_to_dict(keystr::AbstractString)
Dict{Char, Char}(map(x -> (x[1]+64, x[2]), enumerate(uppercase(keystr))))
Dict{Char, Char}(map(x -> (Char(x[1]+64), x[2]), enumerate(uppercase(keystr))))
end
"""
@@ -47,7 +47,7 @@ function decrypt_monoalphabetic(ciphertext, key::AbstractString)
# working in lowercase; key is assumed only to have each element appearing once
# and to be in lowercase
# so decrypt_monoalphabetic("cb", "cbade…") is "ab"
dict = [(a => Char(96 + search(lowercase(key), a))) for a in lowercase(key)]
dict = Dict(a => Char(96 + search(lowercase(key), a)) for a in lowercase(key))
encrypt_monoalphabetic(lowercase(ciphertext), dict)
end
@@ -94,7 +94,7 @@ function crack_monoalphabetic(ciphertext; starting_key="",
acceptance_prob=((e,ep,t) -> ep > e ? 1. : exp(-(e-ep)/t)),
chatty=0,
rounds=1)
if starting_key == ""
# most common letters
commonest = "ETAOINSHRDLUMCYWFGBPVKZJXQ"
@@ -174,4 +174,3 @@ function crack_monoalphabetic(ciphertext; starting_key="",
end
(decrypt_monoalphabetic(ciphertext, key), key)
end

View File

@@ -12,9 +12,8 @@ function playfair_key_to_square(key::AbstractString, replacement)
key_sanitised = union(uppercase(letters_only(key)))
# construct key square
remaining = collect(filter(x -> (x != replacement[2] && findfirst(key_sanitised, x) == 0), 'A':'Z'))
keysquare = transpose(reshape([key_sanitised; remaining], 5, 5))
keysquare
keysquare = reshape([key_sanitised; remaining], 5, 5)
return permutedims(keysquare, (2, 1)) # transpose() is deprecated
end
function encrypt_playfair(plaintext, key::AbstractString; combined=('I','J'))
@@ -101,7 +100,7 @@ function encrypt_playfair(plaintext, key::Array{Char, 2}; stripped=false, combin
print(ans, key[((l1pos[1]+1 - 1) % 5)+1, l1pos[2]])
print(ans, key[((l2pos[1]+1 - 1) % 5)+1, l2pos[2]])
else
print(ans, key[l1pos[1], l2pos[2]])
print(ans, key[l2pos[1], l1pos[2]])
end
@@ -109,7 +108,7 @@ function encrypt_playfair(plaintext, key::Array{Char, 2}; stripped=false, combin
i += 2
end
takebuf_string(ans)
String(take!(ans))
end
@@ -126,6 +125,6 @@ Does not attempt to delete X's inserted as padding for double letters.
function decrypt_playfair(ciphertext, key::Array{Char, 2}; combined=('I', 'J'))
# to obtain the decrypting keysquare, reverse every row and every column
keysquare = mapslices(reverse, key, 2)
keysquare = transpose(mapslices(reverse, transpose(keysquare), 2))
keysquare = permutedims(mapslices(reverse, permutedims(keysquare, (2, 1)), 2), (2, 1))
lowercase(encrypt_playfair(ciphertext, keysquare, combined=combined))
end
end

View File

@@ -32,7 +32,7 @@ function encrypt_portas(plaintext, key_in::AbstractString)
end
end
takebuf_string(ans)
String(take!(ans))
end
"""

View File

@@ -1,4 +1,4 @@
include("common.jl")
# include("common.jl")
function next_solitaire(deckIn)
# performs one round of Solitaire on the given deck

View File

@@ -4,10 +4,8 @@ For example, encrypt_vigenere("ab", [0, 1]) returns "AC".
"""
function encrypt_vigenere(plaintext, key::Array)
# plaintext: string; key: vector of integer offsets, so [0, 1] encrypts "ab" as "ac"
ans = [encrypt_caesar(chr, key[(i-1) % length(key)+1]) for (i, chr) in enumerate(letters_only(plaintext))]
join(ans, "")
end
"""
@@ -67,6 +65,6 @@ function crack_vigenere(plaintext; keylength=0)
end
derived_key = join([Char(65+crack_caesar(st)[2]) for st in everyother], "")
(derived_key, takebuf_string(ans))
(derived_key, String(take!(ans)))
end
end