Enforce lower/uppercase consistency; expand readme

This commit is contained in:
Smaug123
2016-01-04 09:31:35 +00:00
parent 80e9d6e8c8
commit 37e40640a0
7 changed files with 71 additions and 35 deletions

View File

@@ -1,14 +1,15 @@
"""
Encrypts the given plaintext according to the Caesar cipher.
The key is given as an integer, being the offset of each character;
so encrypt_caesar("abc", 1) == "bcd".
so encrypt_caesar("abc", 1) == "BCD".
Converts the input to lowercase.
Converts the input to uppercase.
"""
function encrypt_caesar(plaintext, key::Integer)
# plaintext: string; key: integer offset, so k=1 sends "a" to "b"
key = ((key-1) % 26) + 1
encrypt_monoalphabetic(plaintext, join([collect(Char(97+key):'z'); collect('a':Char(97+key-1))]))
keystr = join([collect(Char(97+key):'z'); collect('a':Char(97+key-1))])
encrypt_monoalphabetic(plaintext, keystr)
end
"""
@@ -19,7 +20,7 @@ so decrypt_caesar("abcd", 1) == "zabc".
Converts the input to lowercase.
"""
function decrypt_caesar(ciphertext, key::Integer)
# ciphertext: string; key: integer offset, so k=1 decrypts "B" as "A"
# ciphertext: string; key: integer offset, so k=1 decrypts "B" as "a"
key = ((key-1) % 26) + 1
encrypt_caesar(ciphertext, 26-key)
lowercase(encrypt_caesar(ciphertext, 26-key))
end

View File

@@ -35,9 +35,8 @@ end
function encrypt_monoalphabetic(plaintext, key::AbstractString)
# plaintext: string; key: string of length 26, first character is the image of 'a', etc
# working in lowercase; key is assumed only to have each element appearing once
# and to be in lowercase.
dict = Dict{Char, Char}(map(x -> (x[1]+96, x[2]), enumerate(key)))
encrypt_monoalphabetic(lowercase(plaintext), dict)
dict = Dict{Char, Char}(map(x -> (x[1]+64, x[2]), enumerate(uppercase(key))))
encrypt_monoalphabetic(uppercase(plaintext), dict)
end
function decrypt_monoalphabetic(ciphertext, key::AbstractString)

View File

@@ -1,6 +1,6 @@
"""
Encrypts the given string using the Vigenere cipher according to the given vector of offsets.
For example, encrypt_vigenere("ab", [0, 1]) returns "ac".
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"
@@ -16,16 +16,15 @@ For example, decrypt_vigenere("ac", [0, 1]) returns "ab".
"""
function decrypt_vigenere(ciphertext, key::Array)
# ciphertext: string; key: vector of integer offsets, so [0, 1] decrypts "ac" as "ab"
encrypt_vigenere(ciphertext, 26-key)
lowercase(encrypt_vigenere(ciphertext, 26-key))
end
"""
Encrypts the given string using the Vigenere cipher according to the given keystring.
For example, encrypt_vigenere("ab", "ab") returns "ac".
For example, encrypt_vigenere("ab", "ab") returns "AC".
"""
function encrypt_vigenere(ciphertext, key::AbstractString)
# ciphertext: string; key: string, so "ab" encrypts "ab" as "ac"
# ciphertext: string; key: string, so "ab" encrypts "ab" as "AC"
encrypt_vigenere(ciphertext, [Int(i)-97 for i in lowercase(letters_only(key))])
end
@@ -36,4 +35,4 @@ For example, decrypt_vigenere("ab", "ac") returns "ab".
function decrypt_vigenere(plaintext, key::AbstractString)
# plaintext: string; key: string, so "ab" decrypts "ac" as "ab"
decrypt_vigenere(plaintext, [Int(i)-97 for i in lowercase(letters_only(key))])
end
end