Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Vigen-re-cipher/CoffeeScript/vigen-re-cipher.coffee
Normal file
35
Task/Vigen-re-cipher/CoffeeScript/vigen-re-cipher.coffee
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
# Simple helper since charCodeAt is quite long to write.
|
||||
code = (char) -> char.charCodeAt()
|
||||
|
||||
encrypt = (text, key) ->
|
||||
res = []
|
||||
j = 0
|
||||
|
||||
for c in text.toUpperCase()
|
||||
continue if c < 'A' or c > 'Z'
|
||||
|
||||
res.push ((code c) + (code key[j]) - 130) % 26 + 65
|
||||
j = ++j % key.length
|
||||
|
||||
String.fromCharCode res...
|
||||
|
||||
decrypt = (text, key) ->
|
||||
res = []
|
||||
j = 0
|
||||
|
||||
for c in text.toUpperCase()
|
||||
continue if c < 'A' or c > 'Z'
|
||||
|
||||
res.push ((code c) - (code key[j]) + 26) % 26 + 65
|
||||
j = ++j % key.length
|
||||
|
||||
String.fromCharCode res...
|
||||
|
||||
# Trying it out
|
||||
key = "VIGENERECIPHER"
|
||||
original = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
encrypted = encrypt original, key
|
||||
|
||||
console.log "Original : #{original}"
|
||||
console.log "Encrypted : #{encrypted}"
|
||||
console.log "Decrypted : #{decrypt encrypted, key}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue