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

@vincheung/cipher

v1.0.0

Published

JavaScript 加密库 crypto-js 的封装。

Downloads

33

Readme

cipher

JavaScript 加密库 crypto-js 的封装。

使用

Install:

npm install @vincheung/cipher -S

Import:

<script src="../dist/cipher.min.js"></script>

API

  • cipher 依赖 crypto-js 实现,暴露全局对象 Cipher

  • cipher 的默认加密模式为 ECB,填充方式为 PKCS7

  • Cipher 暴露加密方法 encrypt,解密方法 decrypt

  • encrypt:入参

    • data:明文

    • key:密钥

    • {}:配置

      • cipher:算法的名称

      • mode:模式

      • padding:填充

      • iv:向量

  • decrypt:入参

    • data:密文

    • key:密钥

    • {}:配置

      • cipher:算法的名称

      • mode:模式

      • padding:填充

      • iv:向量

      • compile:CryptoJS.enc.Utf8 转译,默认值 true(解密结果默认会使用 CryptoJS.enc.Utf8 转译,你可以使用 compile: false 来关闭转译)

  • Cipher 对象下的 CryptoJS 属性对象完整继承来源于 crypto-js,你仍可以通过 Cipher.CryptoJS 来使用 crypto-js 的所有功能

示例

import Cipher from 'cipher'

const message = 'https://github.com/vincheung/cipher'
const key = 'cipher'

// DES
const cipherDES = Cipher.encrypt(message, key, {
  cipher: 'DES',
})
const plainDES = Cipher.decrypt(cipherDES, key, {
  cipher: 'DES',
})
console.log(cipherDES)
console.log(plainDES)

// TripleDES
const cipherTripleDES = Cipher.encrypt(message, key, {
  cipher: 'TripleDES',
  mode: 'CBC',
  padding: 'ZeroPadding',
})
const plainTripleDES = Cipher.decrypt(cipherTripleDES, key, {
  cipher: 'TripleDES',
  mode: 'CBC',
  padding: 'ZeroPadding',
})
console.log(cipherTripleDES)
console.log(plainTripleDES)

// AES
const cipherAES = Cipher.encrypt(message, key, {
  cipher: 'AES',
})
const plainAES = Cipher.decrypt(cipherAES, key, {
  cipher: 'AES',
})
console.log(cipherAES)
console.log(plainAES)
// 假设密钥 key 的处理使用
import Cipher from 'cipher'

const { CryptoJS } = Cipher

const message = 'https://github.com/vincheung/cipher'
const key = 'cipher'

const keyHex = CryptoJS.enc.Utf8.parse(key)
const cipherDES = Cipher.encrypt(message, keyHex, {
  cipher: 'DES',
})

拓展

  • CBC 需要初始化向量 iv,来加密第一块 C0

  • PKCS5 只能用来填充 64bit 的数据块

  • 所有需要填充的地方都以 0 填充

  • 在不填充的情况下,加密内容不是 8bit 整数倍加密会报错

  • Java 的默认模式为 ECB,key 的 size 必须为 24默认填充方式为 PKCS5,没有 PKCS7

  • C# 的默认模式为 CBC,默认填充方式为 PKCS7

  • JavaScript 填充方式 PKCS7 对应 Java 填充方式 PKCS5,没有 PKCS5

兼容

cipher 使用广泛支持的 ES6 Object.assign,如果你的环境较为老旧,可以使用垫片 polyfill。

License

The MIT License.