codeberg is different ig
This commit is contained in:
parent
d801db3867
commit
6a8becba84
11 changed files with 7 additions and 15 deletions
42
bin/crypt.js
Normal file
42
bin/crypt.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
// Implement modulo by replacing the negative operand
|
||||
// with an equivalent positive operand that has the same wrap-around effect
|
||||
function mod(n, p) {
|
||||
if (n < 0)
|
||||
n = p - Math.abs(n) % p;
|
||||
|
||||
return n % p;
|
||||
}
|
||||
|
||||
|
||||
// Function will implement Caesar Cipher to
|
||||
// encrypt / decrypt the msg by shifting the letters
|
||||
// of the message acording to the key
|
||||
function encrypt(msg, key) {
|
||||
var encMsg = "";
|
||||
|
||||
key -= 0;
|
||||
|
||||
for (var i = 0; i < msg.length; i++) {
|
||||
var code = msg.charCodeAt(i);
|
||||
|
||||
// Encrypt only letters in 'A' ... 'Z' interval
|
||||
if (code >= 97 && code <= 97 + 26 - 1) {
|
||||
code -= 97;
|
||||
code = mod(code + key, 26);
|
||||
code += 97;
|
||||
}
|
||||
if (code >= 65 && code <= 65 + 26 - 1) {
|
||||
code -= 65;
|
||||
code = mod(code + key, 26);
|
||||
code += 65;
|
||||
}
|
||||
|
||||
encMsg += String.fromCharCode(code);
|
||||
}
|
||||
|
||||
return encMsg;
|
||||
}
|
||||
|
||||
function decrypt(msg, key) {
|
||||
return encrypt(msg, 26 - key);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue