Last active
February 29, 2020 03:58
-
-
Save alvin4frnds/29d70fd79d6c4554f825c0afc052811d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const shajs = require('sha.js'); | |
const mcrypt = require('mcrypt'); | |
const CryptoJS = require("crypto-js"); | |
const crypto = require('crypto'); | |
class MCrypt { | |
static get key() { | |
//noinspection JSUnresolvedFunction | |
return shajs('sha256') | |
.update('oshi12wq!@WQ') | |
.digest('base64') | |
.substr(0, 32); | |
} | |
static encrypt(text) { | |
var key = Buffer.from(MCrypt.key, 'base64'); | |
console.log("key: ", MCrypt.key); | |
var iv = String.fromCharCode(48).repeat(16); | |
var blockSize = 16; | |
var pad = blockSize - text.length; | |
text = text + String.fromCharCode(pad).repeat(pad); | |
let bfEcb = new mcrypt.MCrypt('rijndael-128', 'cbc'); | |
bfEcb.open(key, iv); | |
return bfEcb.encrypt(text).toString('base64'); | |
/*const iv = CryptoJS.enc.Hex.parse("30303030303030303030303030303030"); | |
return CryptoJS.AES | |
.encrypt(text, MCrypt.key, { | |
mode: CryptoJS.mode.CBC, | |
padding: CryptoJS.pad.Pkcs7, | |
iv: iv, | |
}) | |
.toString();*/ | |
} | |
static decrypt(text) { | |
var key = Buffer.from(MCrypt.key, 'base64'); // 'p!24$8bg*&34\0\0\0\0'.toString('utf8'); | |
var iv = '0000000000000000'; | |
let bfEcb = new mcrypt.MCrypt('rijndael-128', 'cbc'); | |
var ivAndCiphertext = Buffer.from(text, 'base64'); | |
bfEcb.open(key, iv); | |
return bfEcb.decrypt(text).toString('base64'); | |
// var blockSize = 16; | |
// var pad = blockSize - text.length; | |
// text = text + String.fromCharCode(pad).repeat(pad); | |
// let bfEcb = new mcrypt.MCrypt('rijndael-128', 'cbc'); | |
// bfEcb.open(key, iv); | |
// return bfEcb.encrypt(text).toString('base64'); | |
// var cipher = crypto.createCipheriv('aes-128-cbc', key, iv); | |
// var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv); | |
// text = "1"; | |
// var encrypted = cipher.update(text, 'utf8', 'binary'); | |
// encrypted += cipher.final('binary'); | |
// console.log("AFter adding to encrypted: ", encrypted.toString('base64')); | |
// var hexVal = Buffer.from(encrypted, 'binary'); | |
// var newEncrypted = hexVal.toString('base64'); | |
// console.log('Encrypted: ', newEncrypted, hexVal); | |
// var decrypted = decipher.update(newEncrypted, 'base64', 'binary'); | |
// decrypted += decipher.final('binary'); | |
// console.log('Decrypted: ', decrypted); | |
// return CryptoJS.AES | |
// .decrypt(text, MCrypt.key, { | |
// mode: CryptoJS.mode.CBC, | |
// padding: CryptoJS.pad.Pkcs7, | |
// }) | |
// .toString(); | |
} | |
static encrypt_bak(text) { | |
let bfEcb = new mcrypt.MCrypt('rijndael-128', 'cbc'); | |
let iv = MCrypt.iv; let key = Buffer.from(MCrypt.key, 'binary'); | |
bfEcb.open(key, iv); | |
return bfEcb.encrypt(text).toString('base64'); | |
} | |
} | |
module.exports = MCrypt; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment