Encrypt string in PHP and decrypt in Node.js

前端 未结 4 464
误落风尘
误落风尘 2020-12-01 18:53

I am sending data through insecure connection between Apache and Node.js servers. I need to encrypt data in PHP and decrypt in Node.js. I\'ve spent 2 days trying to get it t

4条回答
  •  [愿得一人]
    2020-12-01 19:40

    This is codeiginiter framework default decryption equivalent js script (aes128cbc), hope this will help someone.

     let crypto = require("crypto");
     let secret = 'xxxxxxxxxxxxxxxxxxxx';
    
     // ikm is initial keying material
     var hkdf = function (hashAlg, salt, ikm) {
      this.hashAlg = hashAlg;
    
      // create the hash alg to see if it exists and get its length
      var hash = crypto.createHash(this.hashAlg);
      this.hashLength = hash.digest().length;
      this.salt = salt || new Buffer(this.hashLength).fill(0).toString();
      this.ikm = ikm;
    
      // now we compute the PRK
      var hmac = crypto.createHmac(this.hashAlg, this.salt);
      hmac.update(this.ikm);
      this.prk = hmac.digest();
    };
    
    hkdf.prototype = {
    derive: function(info, size, cb) {
      var prev = new Buffer(0);
      var output;
      var buffers = [];
      var num_blocks = Math.ceil(size / this.hashLength);
      info = new Buffer(info);
    
      for (var i=0; i

提交回复
热议问题