June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,41 +1,46 @@
|
|||
function encrypt(msg::ASCIIString, key::ASCIIString)
|
||||
msg = uppercase(join(filter(isalpha, collect(msg))))
|
||||
len = length(msg)
|
||||
key = uppercase(join(filter(isalpha, collect(key))))
|
||||
function encrypt(msg::AbstractString, key::AbstractString)
|
||||
msg = uppercase(join(filter(isalpha, collect(msg))))
|
||||
key = uppercase(join(filter(isalpha, collect(key))))
|
||||
msglen = length(msg)
|
||||
keylen = length(key)
|
||||
|
||||
if length(key) < len
|
||||
key = (key^(div(len - length(key), length(key)) + 2))[1:len]
|
||||
if keylen < msglen
|
||||
key = repeat(key, div(msglen - keylen, keylen) + 2)[1:msglen]
|
||||
end
|
||||
|
||||
enc = Array(Char, len)
|
||||
enc = Vector{Char}(msglen)
|
||||
|
||||
for i=1:length(msg)
|
||||
@inbounds for i in 1:length(msg)
|
||||
enc[i] = Char((Int(msg[i]) + Int(key[i]) - 130) % 26 + 65)
|
||||
end
|
||||
|
||||
join(enc)
|
||||
return join(enc)
|
||||
end
|
||||
|
||||
function decrypt(enc::ASCIIString, key::ASCIIString)
|
||||
enc = uppercase(join(filter(isalpha, collect(enc))))
|
||||
len = length(enc)
|
||||
key = uppercase(join(filter(isalpha, collect(key))))
|
||||
function decrypt(enc::AbstractString, key::AbstractString)
|
||||
enc = uppercase(join(filter(isalpha, collect(enc))))
|
||||
key = uppercase(join(filter(isalpha, collect(key))))
|
||||
msglen = length(enc)
|
||||
keylen = length(key)
|
||||
|
||||
if length(key) < len
|
||||
key = (key^(div(len - length(key), length(key)) + 2))[1:len]
|
||||
if keylen < msglen
|
||||
key = repeat(key, div(msglen - keylen, keylen) + 2)[1:msglen]
|
||||
end
|
||||
|
||||
msg = Array(Char, len)
|
||||
msg = Vector{Char}(msglen)
|
||||
|
||||
for i=1:length(enc)
|
||||
@inbounds for i in 1:length(enc)
|
||||
msg[i] = Char((Int(enc[i]) - Int(key[i]) + 26) % 26 + 65)
|
||||
end
|
||||
|
||||
join(msg)
|
||||
return join(msg)
|
||||
end
|
||||
|
||||
const msg = "Attack at dawn."
|
||||
const messages = ("Attack at dawn.", "Don't attack.", "The war is over.")
|
||||
const key = "LEMON"
|
||||
|
||||
println(encrypt(msg, key))
|
||||
println(decrypt(encrypt(msg, key), key))
|
||||
for msg in messages
|
||||
enc = encrypt(msg, key)
|
||||
dec = decrypt(enc, key)
|
||||
println("Original: $msg\n -> encrypted: $enc\n -> decrypted: $dec")
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue