Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

encrypt decrypt javascript

const Cryptr = require('cryptr');
const cryptr = new Cryptr('ReallySecretKey');

const encryptedString = cryptr.encrypt('Popcorn');
const decryptedString = cryptr.decrypt(encryptedString);

console.log(encryptedString);
Comment

javascript encrypt decrypt

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: npm execute script with nodemon 
Javascript :: Javascript Get day number in year from date 
Javascript :: count the number of elements in an array javascript 
Javascript :: check css property jquery 
Javascript :: javascript print path 
Javascript :: ajax file form 
Javascript :: switch case in javascript 
Javascript :: set css var with javascript 
Javascript :: javascript 1 line if 
Javascript :: convert 24 hour to 12 hour moment js 
Javascript :: export data to excel using react js 
Javascript :: moment compare time 
Javascript :: document get elements by id js 
Javascript :: instantiate template playcanvas 
Javascript :: react-native safeareaview 
Javascript :: upload files to api using axios 
Javascript :: for loop in vue 
Javascript :: create node js api 
Javascript :: how to flip a Number in javascript 
Javascript :: round number at 2 decimal places 
Javascript :: javascript stop execution 
Javascript :: laravel send http post request json 
Javascript :: how can ic get the id of div jq 
Javascript :: javascript splice without changing array 
Javascript :: discord.js MessageEmbed 
Javascript :: javascript copy 2d array 
Javascript :: js array find regex 
Javascript :: react script 
Javascript :: js how to check is array empty es6 
Javascript :: formdata append not working 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =