Skip to content

Commit

Permalink
Update javascript code to match other ciphers
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasgruwez committed Oct 12, 2019
1 parent 806674c commit a1649ef
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 30 deletions.
45 changes: 31 additions & 14 deletions js/decrypt.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
/**********************************
/* ==========================================================================
*
* Use:
* 'Zijvw Gnhvh!'.decrypt('mysecretkey')
* => 'Hello World!'
*
* ========================================================================== */

Use: "RIJVSUYVJN".decrypt("key")
=> "HELLOWORLD"
String.prototype.decrypt = function(key) {

**********************************/
function between(x, min, max) {return x >= min && x <= max;}

String.prototype.decrypt = function(key) {
function ordA(a) {
return a.charCodeAt(0) - 65;
let msg = [];
let output = '';
for (char of this) {
let code = char.charCodeAt(0)
if (between(code, 65, 90)) {msg.push([code - 65, 0])}
else if (between(code, 97, 122)) {msg.push([code - 97, 1])}
else {msg.push(char)}
}
let i = 0;
let b;
key = key.toUpperCase().replace(/[^A-Z]/g, '');
return this.toUpperCase().replace(/[^A-Z]/g, '').replace(/[A-Z]/g, a => {
b = key[i++ % key.length];
return String.fromCharCode(((ordA(a) + 26 - ordA(b)) % 26 + 65));
});

key = key.toLowerCase().split('').map(function(c) {
return c.charCodeAt(0) - 65
})

for (var i = 0; i < msg.length; i++) {
if (typeof msg[i] === 'string') {output += msg[i]}
else {
let value = (key[i % key.length] - msg[i][0]) % 26
output += String.fromCharCode(26 - value + 65 + msg[i][1] * 32)
}
}

return output

}
module.exports = (text, key) => text.decrypt(key)
46 changes: 32 additions & 14 deletions js/encrypt.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
/*****************************
/* ==========================================================================
*
* Use:
* 'Hello World!'.encrypt('mysecretkey')
* => 'Zijvw Gnhvh!'
*
* ========================================================================== */

Use: "Hello World".encrypt("key")
=> "RIJVSUYVJN"
******************************/
String.prototype.encrypt = function(key) {
function ordA(a) {
return a.charCodeAt(0) - 65;

function between(x, min, max) {return x >= min && x <= max;}

let msg = [];
let output = '';
for (char of this) {
let code = char.charCodeAt(0)
if (between(code, 65, 90)) {msg.push([code - 65, 0])}
else if (between(code, 97, 122)) {msg.push([code - 97, 1])}
else {msg.push(char)}
}

key = key.toLowerCase().split('').map(function(c) {
return c.charCodeAt(0) - 65
})

for (var i = 0; i < msg.length; i++) {
if (typeof msg[i] === 'string') {output += msg[i]}
else {
let value = (msg[i][0] + key[i % key.length]) % 26
output += String.fromCharCode(value + 65 + msg[i][1] * 32)
}
}
let i = 0;
let b;
key = key.toUpperCase().replace(/[^A-Z]/g, '');
return this.toUpperCase().replace(/[^A-Z]/g, '').replace(/[A-Z]/g, a => {
b = key[i++ % key.length];
return String.fromCharCode(((ordA(a) + ordA(b)) % 26 + 65));
});

return output

}
module.exports = (text, key) => text.encrypt(key)
2 changes: 0 additions & 2 deletions py/encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ def encrypt(initial, key):

key = [ord(char) - 65 for char in key.lower()]



for i in range(len(msg)):
if type(msg[i]) == type(''): output += msg[i]
else:
Expand Down

0 comments on commit a1649ef

Please sign in to comment.