基于nodeje的RSA加解密

RAS是一种非对称加密,可以用公钥加密,私钥解密也可以反过来用私钥加密,公钥解密;以下是其实现方式,与java后台匹配,实现双向加解密。

/**
 * RSA最大加密明文大小
 */
const MAX_ENCRYPT_BLOCK = 245;
/**
 * RSA最大解密密文大小
 */
const  MAX_DECRYPT_BLOCK = 256;

通过fs.readFile()读取本地的私钥与公钥文件,要pem格式,如何读取这里就不在细述了...

let privateKey = `
-----BEGIN PRIVATE KEY-----
.........
-----END PRIVATE KEY-----
`

let publicKey = `
-----BEGIN PUBLIC KEY-----
.........
-----END PUBLIC KEY-----
`

 

1.公钥加密码私钥解

/**
 * 公钥加密
 * @param {*} publicKey 公钥
 * @param {*} callback 
 */
function encrypt_publicKey(publicKey){
  const params = {
    "a": '11111111111111111111111111156565656565656565656565656565656'
    "b": "34343434343436767676767676767676676764"   
  }
  const data = JSON.stringify(params)
  // 加密信息用Buffer封装
  const buffer = Buffer.from(data, 'utf-8');
  const len = buffer.byteLength;
  const bufs = [];
  let offSet = 0;
  let endOffSet = MAX_ENCRYPT_BLOCK;
  // 分段加密
  while (len - offSet > 0) {
    if (len - offSet > MAX_ENCRYPT_BLOCK) {
      const chunck = buffer.slice(offSet, endOffSet);
      bufs.push(crypto.publicEncrypt({key: publicKey, padding: crypto.constants.RSA_PKCS1_PADDING}, chunck));
    } else {
      const chunck = buffer.slice(offSet, len);
      bufs.push(crypto.publicEncrypt({key: publicKey, padding: crypto.constants.RSA_PKCS1_PADDING}, chunck));
    }
    offSet += MAX_ENCRYPT_BLOCK;
    endOffSet += MAX_ENCRYPT_BLOCK;
  }
  const result = Buffer.concat(bufs);
  decrypt_privateKey(result)
}
/**
 * 私钥解密
 * @param {*} privateKey 私钥
 * @param {*} arr buffer格式
 */
function decrypt_privateKey(data){
  const buf = _data instanceof Buffer ? _data : Buffer.from(_data, 'base64');
  const len = buf.byteLength; 
  const bufs = [];
  let offSet = 0;
  let endOffSet = MAX_DECRYPT_BLOCK;

  // 分段解密
  while (len - offSet > 0) {
    if (len - offSet > MAX_DECRYPT_BLOCK) {
        const bufTmp = buf.slice(offSet, endOffSet);
        bufs.push(crypto.privateDecrypt({key: privateKey, padding: crypto.constants.RSA_PKCS1_PADDING}, bufTmp));
    } else {
        const bufTmp = buf.slice(offSet, len);
        bufs.push(crypto.privateDecrypt({key: privateKey, padding: crypto.constants.RSA_PKCS1_PADDING}, bufTmp));
    }
    offSet += MAX_DECRYPT_BLOCK;
    endOffSet += MAX_DECRYPT_BLOCK;
  }
  const decrypt = Buffer.concat(bufs);
  console.log('解密后:', decrypt.toString())
}

 

2.钥加密码钥解

/**
 * 私钥加密
 * @param {*} privateKey 私钥
 * @param {*} callback 
 */
function encrypt_privateKey(){
  const params = {
    "a": "121212111111111111111111111111111111111",
    "b": "33333333333333333333333",
  }
  const data = JSON.stringify(params)
  // 加密信息用Buffer封装
  const buffer = Buffer.from(data, 'utf-8');
  const len = buffer.byteLength;
  const bufs = [];
  let offSet = 0;
  let endOffSet = MAX_ENCRYPT_BLOCK;
  console.log('len==', len)
  // 分段加密
  while (len - offSet > 0) {
    if (len - offSet > MAX_ENCRYPT_BLOCK) {
      const chunck = buffer.slice(offSet, endOffSet);
      bufs.push(crypto.privateEncrypt({key: privateKey, padding: crypto.constants.RSA_PKCS1_PADDING}, chunck));
    } else {
      const chunck = buffer.slice(offSet, len);
      bufs.push(crypto.privateEncrypt({key: privateKey, padding: crypto.constants.RSA_PKCS1_PADDING}, chunck));
    }
    offSet += MAX_ENCRYPT_BLOCK;
    endOffSet += MAX_ENCRYPT_BLOCK;
  }
  const result = Buffer.concat(bufs);
  console.log('加密后密文:', result.toString('base64'));
  decrypt_publicKey(result)
}

/**
 * 公钥解密
 * @param {*} publicKey 公钥
 * @param {*} data 
 */
function decrypt_publicKey(data){
  const buf = data instanceof Buffer ? data : Buffer.from(data, 'base64');
  const len = buf.byteLength; 
  const bufs = [];
  let offSet = 0;
  let endOffSet = MAX_DECRYPT_BLOCK;
  // 分段解密
  while (len - offSet > 0) {
    if (len - offSet > MAX_DECRYPT_BLOCK) {
        const bufTmp = buf.slice(offSet, endOffSet);
        bufs.push(crypto.publicDecrypt({key: publicKey, padding: crypto.constants.RSA_PKCS1_PADDING}, bufTmp));
    } else {
        const bufTmp = buf.slice(offSet, len);
        bufs.push(crypto.publicDecrypt({key: publicKey, padding: crypto.constants.RSA_PKCS1_PADDING}, bufTmp));
    }
    offSet += MAX_DECRYPT_BLOCK;
    endOffSet += MAX_DECRYPT_BLOCK;
  }
  const decrypt = Buffer.concat(bufs);
  console.log('解密后:', decrypt.toString())
}

 

posted @ 2024-05-09 16:08  cmwang2017  阅读(6)  评论(0编辑  收藏  举报