self.encrypt = function(data, key) {
   var cipher = crypto.createCipheriv(algorithm, new Buffer(key), '');
   cipher.setAutoPadding(false);
   try {
     return new Buffer(cipher.update(pad(data), 'utf8', 'binary') + cipher.final('binary'), 'binary').toString('base64');
   } catch (e) {
     return null;
   }
 }
function createCipher(options) {
  'use strict';
  if (_.isString(options.key)) {
    return crypto.createCipher(options.cipher, options.key);
  } else {
    var iv = crypto.createHash('md5').update(options.seed).digest('hex');
    return crypto.createCipheriv(options.cipher, options.key, iv);
  }
};
const encrypt = async (key, text) => {
  if (!Buffer.isBuffer(key)) key = Buffer.from(key, 'hex')

  const iv = await createIv()
  const cipher = crypto.createCipheriv(algorithm, key, iv)
  const encrypted = cipher.update(text)

  return Buffer.concat([encrypted, cipher.final(), iv]).toString('hex')
}
Example #4
0
const encrypt = (inputData, config) => {
  const iv = crypto.randomBytes(12)
  const cipher = crypto.createCipheriv('aes-256-gcm', config.settings.cipher.key, iv)

  let data = cipher.update(JSON.stringify(inputData, null, 2), 'utf8', 'base64')
  data += cipher.final('base64')
  const auth = cipher.getAuthTag()
  return [iv.toString('hex'), auth.toString('hex'), data].join(config.settings.cipher.separator)
}
Example #5
0
// give us ciphertext for this plaintext.
// note that this way of using the block cipher is only intended for the
// specific purpose outlined in this module.
function encrypt(key, iv, plaintext) {
    var cipher = crypto.createCipheriv('aes-256-cfb8', key, iv);
    var ciphertext = cipher.update(plaintext);
    var finaltext = cipher.final();
    assert.equal(finaltext.length, 0);
    if (typeof ciphertext === "string") {
        ciphertext = new Buffer(ciphertext, 'binary');
    }
    return ciphertext; 
}
Example #6
0
export function createCipher(secret, methodName, initialData, _iv) {
  const key = generateKey(methodName, secret);
  const iv = _iv || crypto.randomBytes(getParamLength(methodName)[1]);
  const cipher = crypto.createCipheriv(methodName, key, iv);

  return {
    cipher,
    data: Buffer.concat([iv, cipher.update(initialData)]),
  };
}
function encrypt(text) {
  var cipher = crypto.createCipheriv(algorithm, key, iv);
  var encrypted = cipher.update(text, 'utf8', 'hex');
  encrypted += cipher.final('hex');
  var tag = cipher.getAuthTag();
  return {
    content: encrypted,
    tag: tag
  };
}
exports.aesEncrypt = function(data, key) {
  var iv = '';
  var clearEncoding = 'utf8';
  var cipherEncoding = 'base64';
  var cipher = crypto.createCipheriv('aes-128-ecb', key, iv);
  var cipherChunks = [];
  cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
  cipherChunks.push(cipher.final(cipherEncoding));
  return cipherChunks.join('');
};
Example #9
0
File: licenses.js Project: nsi88/ls
 providers.getRaw(providerName, function(err, providerRaw) {
   assert(!err, err);
   var cipher = crypto.createCipheriv('aes-256-cbc', providerRaw.crypto_key, providerRaw.crypto_iv);
   var enc = Buffer.concat([cipher.update(new Buffer(body, 'hex')), cipher.final()]);
   ls.database.query('SELECT license FROM licenses WHERE provider_id = ? AND content_id = ? AND sequence_id = ?', [providerRaw.id, contentId, sequenceId], function(err, result) {
     assert(!err, err);
     assert(enc.equals(result[0].license), 'Encrypted again license not equal to database original');
     done();
   });
 });
Example #10
0
function calc_resp(password_hash, server_challenge){
    // padding with zeros to make the hash 21 bytes long
    var passHashPadded = new Buffer(21);
    passHashPadded.fill("\0");
    password_hash.copy(passHashPadded, 0, 0, password_hash.length);

    var resArray = [];

    var des = crypto.createCipheriv('DES-ECB', insertZerosEvery7Bits(passHashPadded.slice(0,7)), '');
    resArray.push( des.update(server_challenge.slice(0,8)) );

    des = crypto.createCipheriv('DES-ECB', insertZerosEvery7Bits(passHashPadded.slice(7,14)), '');
    resArray.push( des.update(server_challenge.slice(0,8)) );

    des = crypto.createCipheriv('DES-ECB', insertZerosEvery7Bits(passHashPadded.slice(14,21)), '');
    resArray.push( des.update(server_challenge.slice(0,8)) );

   	return Buffer.concat(resArray);
}
Example #11
0
exports.encrypt = function(text,callBack) {
  var cipher = crypto.createCipheriv(algorithm, password, iv);
  var encrypted = cipher.update(text, 'utf8', 'hex');
  encrypted += cipher.final('hex');
 // var tag = cipher.getAuthTag();
  callBack({
    content: encrypted
   // tag: tag
  });
};
function encrypt(text, password) {
  var hash = crypto.createHash('sha1');
  hash.update(password);
  var key = hash.digest().slice(0, 16);
  var ivBuffer = Buffer.alloc(16);
  var cipher = crypto.createCipheriv(algorithm, key, ivBuffer);
  var crypted = cipher.update(text, 'utf8', 'hex');
  crypted += cipher.final('hex');
  return crypted;
}
Example #13
0
var encrypt_gcm = function (plain, secret, iv) {
    var cipher,
        encrypt = crypto.createCipheriv('aes-256-gcm', secret, iv);
    cipher = encrypt.update(plain, 'utf8', 'base64');
    cipher += encrypt.final('base64');
    return {
        content: cipher,
        tag: encrypt.getAuthTag()
    };
};
Example #14
0
// encrypt the packet
function lineize(to, packet)
{
  var wrap = {type:"line"};
  wrap.line = to.lineIn;
  var iv = crypto.randomBytes(16);
  wrap.iv = iv.toString("hex");
  var aes = crypto.createCipheriv("AES-256-CTR", to.encKey, iv);
  var body = Buffer.concat([aes.update(pencode(packet.js,packet.body)), aes.final()]);
	return pencode(wrap,body);
}
Example #15
0
function makeResponse(hash, nonce) {
	var out = new Buffer(24);
	for ( var i = 0; i < 3; i++) {
		var keybuf = $.oddpar($.expandkey(hash.slice(i * 7, i * 7 + 7)));
		var des = crypto.createCipheriv('DES-ECB', keybuf, '');
		var str = des.update(nonce, 'binary', 'binary');
		out.write(str, i * 8, i * 8 + 8, 'binary');
	}
	return out;
}
Example #16
0
var transformKey = function(key, seed, rounds) {
    for(var round = 0; round < rounds; round++) {
        var cipher = crypto.createCipheriv('aes-256-ecb', seed, new Buffer(0));
        cipher.setAutoPadding(false);
        key = cipher.update(key, 'binary', 'binary') + cipher.final('binary');
    }

    key = crypto.createHash('sha256').update(key, 'binary').digest('binary');
    return key;
}
Example #17
0
 crypto.pbkdf2(this.passphrase, salt, this.options.rounds, 48, function(err, keyiv) {
     if (err) {
         throw(err);
     }
     this.passphrase = null;
     this.cipher = crypto.createCipheriv("aes256", keyiv.slice(0, 32), keyiv.slice(32));
     this.push(salt);
     this.push(this.cipher.update(chunk));
     done();
 }.bind(this));
Example #18
0
function send_data(obj) {
    var aes_iv = crypto.randomBytes(16);
    var cipher = crypto.createCipheriv('aes-256-cbc', AES_KEY, aes_iv);

    var objdata = msgpack.pack(obj);
    var buffer = [cipher.update(objdata), cipher.final()];

    var data = [TOKEN, aes_iv.toString('hex'), objdata.length, Buffer.concat(buffer)];
    sock.send(msgpack.pack(data));
}
Example #19
0
exports.validate = function(){
  if(!ecc || !ursa) return false;
  try {
    var eccKey = new ecc.ECKey(ecc.ECCurves.nistp256);
    var aes = crypto.createCipheriv("AES-256-CTR", crypto.randomBytes(32), crypto.randomBytes(16));
  }catch(E){};
  if(!eccKey || !aes) return false;
  
  return true;
}
Example #20
0
module.exports.generateLangFile = function(generateData, cb){
    var file = generateData.outDir + module.exports.DIR_LANG + generateData.lang + module.exports.LANG_EXT;
    //words.getWords(pg, lang, lesson, function(words){
        var data = generateData.lesson + ';'
            + generateData.lang
            + ';' + generateData.words.length
            + '\n';

        generateData.words.forEach(function(w,idx){
           data += w.link + ";";
           data += w.word + ";";



           generateData.images.some(function(image,ii){
               if(w.link == image.lid){

                   if(realImageFile(image)){
                       data += image.imagefile;
                   }


                   return;
               }
           });

           data += "\n";


        });

    if(Config.debug){
        console.log('word', data);
    }

            var crypto = require('crypto');
            //console.log(crypto.getCiphers(), crypto.getHashes());


            var algorithm = 'aes-128-cbc'; // or any other algorithm supported by OpenSSL
            var key = 'password';

            var buffKey = new Buffer(Config.PKG_KEY);
            var iv = new Buffer(Config.PKG_INIT_KEY);

            var cipher = crypto.createCipheriv(algorithm, buffKey, iv);
            var encrypted = cipher.update(data, 'utf8', 'hex') + cipher.final('hex');

        //console.log('to tech dat moc neni ne?', data, file);
        fs.writeFile(file, encrypted, function (err) {
           cb(err);
        });

    //});
}
Example #21
0
 //加密
 function encrypt() {
     //encrypt  
     var cipher = crypto.createCipheriv(alg, key, iv);
     if(autoPad){
         cipher.setAutoPadding(autoPad) //default true  
     }
     var ciph = cipher.update(plaintext, 'utf8', 'hex');
     ciph += cipher.final('hex');
     // console.log(alg, ciph)  
     return ciph;
 }
Example #22
0
Auth.prototype.__encrypt = function(plaintext) {
	var crypto = require("crypto");
	var shasum = crypto.createHash('md5');
	shasum.update(this.Config.encryption_key);
	var hashed_key = shasum.digest("hex");
	var iv = crypto.randomBytes(8).toString('hex');
	var cipher = crypto.createCipheriv("aes-256-cbc", hashed_key, iv);
	cipher.setAutoPadding(true);
	var encrypted = cipher.update(plaintext, "binary", "hex") + cipher["final"]("hex");
	return iv + encrypted;
};
Example #23
0
  /**
  * Encrypts password.
  *
  * @param {string} plainText - password.
  */
  encrypt(plainText) {
    const IV = new Buffer(crypto.randomBytes(this.config.iv_length));

    const encryptor = crypto.createCipheriv(this.config.algorithm, this.config.key, IV);
    encryptor.setEncoding('hex');
    encryptor.write(plainText);
    encryptor.end();
    const cipherText = encryptor.read();

    return `${cipherText}:${IV.toString('hex')}`;
  };
Example #24
0
function encrypt(plaintext, key, iv) {
  iv = iv.slice(0, 16);
  var cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
  cipher.setAutoPadding(true);
  var ciphertext = cipher.update(plaintext, 'utf8', 'base64');
  ciphertext += cipher.final('base64');

  console.log('ciphertext', ciphertext, ciphertext.length);

  return ciphertext;
}
Example #25
0
 function sendEncryptionKeyResponse() {
   var pubKey = mcPubKeyToURsa(packet.publicKey);
   var encryptedSharedSecretBuffer = pubKey.encrypt(sharedSecret, undefined, undefined, ursa.RSA_PKCS1_PADDING);
   var encryptedVerifyTokenBuffer = pubKey.encrypt(packet.verifyToken, undefined, undefined, ursa.RSA_PKCS1_PADDING);
   client.cipher = crypto.createCipheriv('aes-128-cfb8', sharedSecret, sharedSecret);
   client.decipher = crypto.createDecipheriv('aes-128-cfb8', sharedSecret, sharedSecret);
   client.write(0xfc, {
     sharedSecret: encryptedSharedSecretBuffer,
     verifyToken: encryptedVerifyTokenBuffer,
   });
 }
Example #26
0
		function encrypt(buffer, password) {
			password = Buffer.from(password);
			var cipher = crypto.createCipheriv('aes-256-ctr', crypto.pbkdf2Sync(password, '', 100000, 32, 'sha512'), crypto.createHash('sha1').update(password).digest().slice(0, 16));
			var arrChunks = [];
			var CHUNK_LENGTH = 2003;
			for (var offset = 0; offset < buffer.length; offset += CHUNK_LENGTH) {
				arrChunks.push(cipher.update(buffer.slice(offset, Math.min(offset + CHUNK_LENGTH, buffer.length)), 'utf8'));
			}
			arrChunks.push(cipher.final());
			return Buffer.concat(arrChunks);
		}
Example #27
0
    exports.generateKey(password, options, function (err, key) {

        if (err) {
            return callback(err);
        }

        var cipher = Crypto.createCipheriv(options.algorithm, key.key, key.iv);
        var enc = Buffer.concat([cipher.update(data, 'utf8'), cipher.final()]);

        callback(null, enc, key);
    });
Example #28
0
NodeJs.prototype.encryptData = function(data, key, vector) {
    if (this.$benchmark) var s = Date.now();
    var keyBuffer = (new Buffer(key, "base64")).slice(0, 16);
    var ivBuffer = (new Buffer(vector, "base64")).slice(0, 16);
    var dataBuffer = new Buffer(data, "utf8");
    var cipher = crypto.createCipheriv("aes-128-ctr", keyBuffer, ivBuffer);
    var cdataBuffer = cipher.update(dataBuffer);
    var r = cdataBuffer.toString("base64");
    if (this.$benchmark) console.log("BENCHMARK crypto/NodeJs encryptData took "+(Date.now()-s)+" ms");
    return r;
}
Example #29
0
function contentEncryption(dataEncryptionAlgorithm, 
                           key,
                           plainText,
                           authenticatedData) {
  checkDataEncryptionAlgorithm(dataEncryptionAlgorithm);
  var iv = Random.generateRandomNumber(16);
  var cipher = Crypto.createCipheriv('AES-128-CBC', key.subarray(16), iv);
  var partial = new Uint8Array(cipher.update(plainText));
  var cipherText = ByteArray.add(partial, new Uint8Array(cipher.final()));
  return { cipherText: cipherText, iv: iv, tag: getTag(key, cipherText, iv, authenticatedData) };
}
Example #30
0
    encrypt(text, appid){
        text = new Buffer(text);

        var pad = enclen(text.length),
            pack = new PKCS7().encode(20 + text.length + appid.length),
            random = getRandomStr(),
            content = random + pad + text.toString('binary') + appid + pack;
        var cipher = crypto.createCipheriv(this.mode, this.key, this.key.slice(0, 16));
        cipher.setAutoPadding(false);
        return cipher.update(content, 'binary', 'base64') + cipher.final('base64');
    }