From 8c3a152225f67847b1cf7652629f1a30c77644c1 Mon Sep 17 00:00:00 2001 From: cormullion Date: Fri, 28 Apr 2017 11:06:03 +0100 Subject: [PATCH] some fixes for v0.5 --- src/common.jl | 6 +++--- src/monoalphabetic.jl | 7 +++---- src/playfair.jl | 11 +++++------ src/solitaire.jl | 2 +- src/vigenere.jl | 4 +--- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/common.jl b/src/common.jl index 9b4be39..b4d6507 100644 --- a/src/common.jl +++ b/src/common.jl @@ -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 \ No newline at end of file +end diff --git a/src/monoalphabetic.jl b/src/monoalphabetic.jl index 6ea3986..d84fcd1 100644 --- a/src/monoalphabetic.jl +++ b/src/monoalphabetic.jl @@ -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 - diff --git a/src/playfair.jl b/src/playfair.jl index 69701cc..b750f4f 100644 --- a/src/playfair.jl +++ b/src/playfair.jl @@ -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 @@ -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 \ No newline at end of file +end diff --git a/src/solitaire.jl b/src/solitaire.jl index 3b3cd9b..43f2a08 100644 --- a/src/solitaire.jl +++ b/src/solitaire.jl @@ -1,4 +1,4 @@ -include("common.jl") +# include("common.jl") function next_solitaire(deckIn) # performs one round of Solitaire on the given deck diff --git a/src/vigenere.jl b/src/vigenere.jl index 566a2da..6d947b0 100644 --- a/src/vigenere.jl +++ b/src/vigenere.jl @@ -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 """ @@ -69,4 +67,4 @@ function crack_vigenere(plaintext; keylength=0) derived_key = join([Char(65+crack_caesar(st)[2]) for st in everyother], "") (derived_key, takebuf_string(ans)) -end \ No newline at end of file +end