Quick actions

cmd+k|ctrl+k

Navigation

Languages

Simple Stupid Encryption

Snippet info

Language

JavaScript

Visibility

public

Author

homam

Created

2022-02-03T22:55:03.102247Z

Updated

2022-02-03T22:55:03.102247Z

const {enc, dec} = require("./enc.js");

btoa = x => Buffer.from(x).toString('base64');
atob = x => Buffer.from(x, 'base64').toString();

function encrypt(text) {
    return btoa([...new Array(text.length)].map((_,i) => (enc(text[i].charCodeAt(0)))).join(":"))
}

function decrypt(e) {
    return atob(e).split(":").map(x => String.fromCharCode(dec(+x))).join("")
}

const original = "https://stackoverflow.com/questions/12332002/how-to-store-a-byte-array-in-javascript️"
const encrypted = encrypt(original)
console.log(encrypted);
const decrypted = decrypt(encrypted);
console.log(decrypted)

console.log(decrypted === original)
INFO