node 加密音频文件 和 解密音频文件

fs.readFile('./downsuccess/'+name+'', {flag: 'r+', encoding: ''}, function (err, data) {
          console.log('读取中')
          if(err) {
            return;
          }
          let b = new Buffer(data);
          let c = b.toString('hex');
          let cipherBuffer = _this.cipher(data);
          fs.writeFile('./downsuccess/'+name+'',cipherBuffer,[],function(){
            console.log(`${name}加密完成`);
            _this.downAll(_this.downList,_this.downCall)
          })
        });
export function cipher (buf) {
  var encrypted = "";
  var cip = crypto.createCipher('rc4', '密匙');
  encrypted += cip.update(buf, 'hex', 'hex');
  encrypted += cip.final('hex');
  return encrypted
};
 

Node.js加解密

96 

无论加密解密、字符转换,都需要使用byte,而node对byte的支持不如其他语言(如c/java,可以直接使用byte[])方便快捷,但是也有模块对byte提供支持。

node对byte处理的支持


Buffer

Buffer支持很多格式转换(Ascii, urf8, usc2, utf16le, base64, binary, hex),传入字符串可以直接进行格式转换:
new Buffer(size); // with size
new Buffer(array) // with array
new Buffer(str[, encoding]) // with string[, encoding = 'utf8']

Int*Array

构造函数如下(都是一样的),因此不能直接传入字符串:

Int8Array(bufferOrArrayOrLength,byteOffset,length)

使用Buffer可以直接将字符串转为HEX,使用Int8Array可以直接转换为标准byte(-128 ~ 127)数组:

// hex: 41 ~ 5A
var UPPER = new Int8Array(new Buffer('ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
// hex: 61 ~ 7A
var lower = new Int8Array(new Buffer('abcdefghijklmnopqrstuvwxyz0123456789'));
// hex: 30 ~ 39
var number = new Int8Array(new Buffer('0123456789'));

如果使用Uint8Array,则转换为无符号的byte(0 ~ 255)数组:

var u = new Uint8Array(new Buffer('123456'));

使用的第三模块


forge - npm install node-forge --save

froge Buffer

使用forge,加解密byte数据都需要使用forge.util.ByteStringBuffer
可以通过方法forge.util.createBuffer()来创建一个新的Buffer。

// put, get
util.ByteStringBuffer.prototype.putByte = function(b)
util.ByteStringBuffer.prototype.getByte = function()
...
util.ByteStringBuffer.prototype.at = function(i)

Buffer内部有两个游标,read cursor和write cursor,只用put和get均会使游标前进,使用.at(i)可以不影响游标而直接获取相应的值。

rsa

生成KeyPair:

var rsa = forge.pki.rsa;
// generate an RSA key pair synchronously
var keypair = rsa.generateKeyPair({bits: 2048, e: 0x10001});
// generate an RSA key pair asynchronously (uses web workers if available)
// use workers: -1 to run a fast core estimator to optimize # of workers
rsa.generateKeyPair({bits: 2048, workers: 2}, function(err, keypair) { 
// keypair.privateKey, keypair.publicKey
});

使用

// encrypt data with a public key using RSAES-OAEP/SHA-256/MGF1-SHA-1
// compatible with Java's RSA/ECB/OAEPWithSHA-256AndMGF1Padding
var encrypted = publicKey.encrypt(bytes, 'RSA-OAEP', {
  md: forge.md.sha256.create(),
  mgf1: {
    md: forge.md.sha1.create()
  }
});

// decrypt data with a private key using RSAES-OAEP/SHA-256/MGF1-SHA-1
// compatible with Java's RSA/ECB/OAEPWithSHA-256AndMGF1Padding
var decrypted = privateKey.decrypt(encrypted, 'RSA-OAEP', {
  md: forge.md.sha256.create(),
  mgf1: {
    md: forge.md.sha1.create()
  }
});

通过已经生成的key(asn1格式)来生成Key对象

// public key
var key = forge.asn1.fromDer(pubKey);
var publicKey =  forge.pki.publicKeyFromAsn1(key);
// private key
var key = forge.asn1.fromDer(priKey);
var privateKey =  forge.pki.privateKeyFromAsn1(key);

生成Key对象后解密

/**
 * @param ed the encrypted data to decrypt in as a byte string. 
 * @param key the RSA key to use.
 * @param pub true for a public key operation, false for private. 
 * @param ml the message length, if known, false to disable padding. 
 *
 * @return the decrypted message as a byte string. 
*/
pki.rsa.decrypt = function(ed, key, pub, ml)

解密后的字符串实际上是

util.ByteStringBuffer.prototype.getBytes = function() {...}

aes

aes通过forge.cipher.createCipher('AES-ECB', key)来创建cipher使用(JAVA中AES对应的即是AES-ECB)。

// decrypt aes, aesKey[ByteStringBuffer]
var aesCipher = forge.cipher.createDecipher('AES-ECB', aesKey);
aesCipher.start();
aesCipher.update(encryptedData);
if (!aesCipher.finish()) {  
  throw new Error('decrypt error');
}
var decryptData = aesCipher.output;

md5

// verify md5
var md = forge.md.md5.create();
md.update(decryptData);
var mdToVerify = md.digest();

感谢: http://www.jianshu.com/p/85f152944527 

原文地址:https://www.cnblogs.com/xred/p/7765286.html