Cipher and decipher a number into hex in nodejs

Viewed 26

I am looking for an efficient way to cypher and decypher a number using the same key. This is not used for cryptography or encrypting anything so it doesn't need to be secure.

I have a unique number and I always want the same result out of the cypher. The cypher shouldn't be too long (more than 6 characters). I do care about the speed as I will be making roughly 1000/millisecond cyphers.

The max number I will be looking to cypher is 100,000,000 and considering the alphanumeric = 26 lowercase letters + 26 uppercase letters and 10 numbers for 6 characters that's about 5.680 * 10^9 combinations which should suffice.

Example of pseudocode:

let num_to_cypher = 1;
let cypher = cypher_this_number(num_to_cypher); // ==> Ax53iw
let decypher = decypher_this_number(cypher); // ==> 1

let num_to_cypher_ex_2 = 12
let cypher_ex_2 = cypher_this_number(num_to_cypher_ex_2); // ==> 2R5ty6
let decypher_ex_2 = decypher_this_number(cypher_ex_2); // ==> 1

Edit 1:

I could have done something like below, but I can't define the cypher's length in this example and I don't care about encryption so I could go with something faster.

function encrypt(text){
    let cipher = crypto.createCipher('aes128','d6F3Efeq')
    let crypted = cipher.update(text,'utf8','hex')
    crypted += cipher.final('hex');
    return crypted;
}

function decrypt(text){
    let decipher = crypto.createDecipher('aes128','d6F3Efeq')
    let dec = decipher.update(text,'hex','utf8')
    dec += decipher.final('utf8');
    return dec;
}
1 Answers
Related