xxxxxxxxxx
const Cryptr = require('cryptr');
const cryptr = new Cryptr('ReallySecretKey');
const encryptedString = cryptr.encrypt('Popcorn');
const decryptedString = cryptr.decrypt(encryptedString);
console.log(encryptedString);
xxxxxxxxxx
const cipher = salt => {
const textToChars = text => text.split('').map(c => c.charCodeAt(0));
const byteHex = n => ("0" + Number(n).toString(16)).substr(-2);
const applySaltToChar = code => textToChars(salt).reduce((a,b) => a ^ b, code);
return text => text.split('')
.map(textToChars)
.map(applySaltToChar)
.map(byteHex)
.join('');
}
const decipher = salt => {
const textToChars = text => text.split('').map(c => c.charCodeAt(0));
const applySaltToChar = code => textToChars(salt).reduce((a,b) => a ^ b, code);
return encoded => encoded.match(/.{1,2}/g)
.map(hex => parseInt(hex, 16))
.map(applySaltToChar)
.map(charCode => String.fromCharCode(charCode))
.join('');
}
// To create a cipher
const myCipher = cipher('mySecretSalt')
//Then cipher any text:
console.log(myCipher('the secret string'))
//To decipher, you need to create a decipher and use it:
const myDecipher = decipher('mySecretSalt')
console.log(myDecipher("7c606d287b6d6b7a6d7c287b7c7a61666f"))
Run code snippet
xxxxxxxxxx
function encryptString(value) {
let encryptedValue = ''
for (let i = 0; i < value.length; i++) {
const charCode = value.charCodeAt(i)
const encryptedCharCode = charCode ^ 42 // XOR dengan 42 untuk enkripsi
encryptedValue += String.fromCharCode(encryptedCharCode)
}
return encryptedValue
}
function decryptString(encryptedValue) {
let decryptedValue = ''
for (let i = 0; i < encryptedValue.length; i++) {
const encryptedCharCode = encryptedValue.charCodeAt(i)
const decryptedCharCode = encryptedCharCode ^ 42 // XOR dengan 42 untuk dekripsi
decryptedValue += String.fromCharCode(decryptedCharCode)
}
return decryptedValue
}
const originalValue = 'qwerty12'
const encryptedValue = encryptString(originalValue)
console.log('Encrypted Value:', encryptedValue)
const decryptedValue = decryptString(encryptedValue)
console.log('Decrypted Value:', decryptedValue)