-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update javascript code to match other ciphers
- Loading branch information
1 parent
806674c
commit a1649ef
Showing
3 changed files
with
63 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters