npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-des-cbc

v1.0.2

Published

node版des-cbc加解密

Downloads

5

Readme

node-des-cbc

node版des-cbc加解密

前言

为了保护请求内容不被抓包软件暴露出来,很多产品都采用了请求加密的方式。DES-CBC就是一种对称加密方式。在网上查资料时发现node的资料不多,所以研究之后记录一下使用node加解密DES-CBC。

代码

首先是加密函数:

/**
 * @description DES加密,以hex为例
 * @param {String} encodeTxt 待加密的字符串
 * @param {String} key 秘钥
 * @param {String} iv 偏移量
 */
function desEncode(encodeTxt, key, iv) {
    key = key.length >= 8 ? key.slice(0, 8) : key.concat('0'.repeat(8 - key.length))
    iv = iv.length >= 8 ? iv.slice(0, 8) : iv.concat('0'.repeat(8 - iv.length))
    const keyHex = Buffer.from(key)
    const ivHex = Buffer.from(iv)
    const encipher = crypto.createCipheriv('des-cbc', keyHex, ivHex)
    //编码方式除了可以使用hex还可以使用base64。
    let encode = encipher.update(encodeTxt, 'utf8', 'hex')
    encode += encipher.final('hex')
    return encode
}

解密函数:

/**
 * @description DES解密,要和加密方式对应起来
 * @param {String} decodeTxt 待解密的字符串
 * @param {String} key 秘钥
 * @param {String} iv 偏移量
 */
function desDecode(decodeTxt, key, iv) {
    key = key.length >= 8 ? key.slice(0, 8) : key.concat('0'.repeat(8 - key.length))
    iv = iv.length >= 8 ? iv.slice(0, 8) : iv.concat('0'.repeat(8 - iv.length))
    const keyHex = Buffer.from(key)
    const ivHex = Buffer.from(iv)
    const decipher = crypto.createDecipheriv('des-cbc', keyHex, ivHex)
    let decode = decipher.update(decodeTxt, 'hex', 'utf8')
    decode += decipher.final('utf8')
    return decodes
}

总结

在使用时要特别注意加密和解密的编码方式要对应起来,如果没有对应起来就会报错或者得不到结果。