JSFiddle - React, Tailwind, and code Playground
cipher / decipher - Simple text encryption
by skibulk
JavaScript
console.clear();
// Source: https://stackoverflow.com/a/54026460/1017480
function crypto(salt){
function textToChars(text){
return text.split('').map(function(c){
c.charCodeAt(0)
});
}
function applySaltToChar(code){
return textToChars(salt).reduce(function(a,b){
return a ^ b
}, code);
}
function byteHex(n){
return ("0" + Number(n).toString(16)).substr(-2);
}
function cipher(input){
return input.split('')
.map(textToChars)
.map(applySaltToChar)
.map(byteHex)
.join('');
}
function decipher(input){
return input.match(/.{1,2}/g)
.map(hex => parseInt(hex, 16))
.map(applySaltToChar)
.map(charCode => String.fromCharCode(charCode))
.join('');
}
return {
cipher: cipher,
decipher: decipher
};
}
var myCrypto = crypto("2q458-yqp4h3-271q4h;wrt546");
var ciphered = myCrypto.cipher("Hello World");
var deciphered = myCrypto.decipher(ciphered);
console.log(myCrypto, ciphered, deciphered);