Giter VIP home page Giter VIP logo

gm-crypt's Introduction

gm-crypt-nodejs

JavaScript Style GuideLicense: MIT

基于javascriptTypedArray的国密加密算法实现。

Implement of Chinese encrypt algorithm in JavaScript and TypedArray.

为了兼容浏览器环境和node.je环境,这里我们使用了TextEncoderBase64.js

Here we use TextEncoder and Base64.js for both browser and node.js environment.

由于使用了TextEncoder,所以暂时不支持Edge。Node.js版本最低为8,建议使用最新的LTS版本。

Because of using TextEncoder, this code cannot run in Edge browser. Node.js‘s version should at least be 8, and the newest LTS version is recommended.

Roadmap

  • SM4
  • SM3
  • SM2

Documentation

Install

npm install gm-crypt

SM4

Init

const SM4 = require('gm-crypt').sm4

let sm4Config = {
  // encrypt/decypt main key; cannot be omitted
  key: 'JeF8U9wHFOMfs2Y8',

  // optional; can be 'cbc' or 'ecb'
  mode: 'cbc', // default

  // optional; when use cbc mode, it's �necessary
  iv: 'UISwD9fW6cFh9SNS', // default is null

  // optional: this is the cipher data's type; Can be 'base64' or 'text'
  cipherType: 'base64' // default is base64
}

let sm4 = new SM4(sm4Config)

Encrypt

let plaintext = '**国密加解密算法'
let ciphertext = sm4.encrypt(plaintext)
// ciphertext's result is 'j/+HgSpv8RZQI2YtSq0L1RnemiSokMm1VvLHSTt245U='

Decrypt

let ciphertext = 'j/+HgSpv8RZQI2YtSq0L1RnemiSokMm1VvLHSTt245U='
let plaintext = sm4.decrypt(ciphertext)
// plaintext's result is '**国密加解密算法'

License

MIT

gm-crypt's People

Contributors

pecliu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

gm-crypt's Issues

SM4 的 key 只支持 utf8

我在找 sm4 的实现,找到你这来了。虽然很想用,但是你现在的 key 只支持 UTF8 格式,如果可以能否统一为 base64 格式?不然我无法设置16进制表示的 key。

其实前两个月我开始找 JS 的国密算法,目标平台是 chrome 和 react-native。当时我找到 https://github.com/AlverLyu/sm.js 这个仓库,它已经支持 sm2 和 sm3,不过我还是遇到了一些问题。由于作者也不更新了,我就自己 fork 下来修改了一些兼容性和 bug,并增加了 sm2 的加解密接口。数据对象原作者用的是数组,用起来也不太方便,不过我还没时间去改。

我想提一个建议:用 Buffer 代替 TypedArray。Buffer 是 node 的类,但浏览器也有实现, buffer 或 safe-buffer,这两个库已经很成熟了。我现在的项目一开始也是用 TypedArray 实现,用起来并不是很友好,和数组转换的时候容易写出一些bug。而且很多其他第三方库都用的 Buffer,后面就换了。

这是仓库地址,欢迎交流:)https://github.com/sickworm/sm.js

SM4-ecb 在待加密内容包含大于0x80的字节时,加密结果错误

问题

待加密内容中,某个字节大于等于 0x80 时,加密结果错误。

校验方法

使用在线工具 http://www.lzltool.com/SM4 的加密结果作为对比。

例子

初始化

以下例子中使用 key = 'ABCDEFGHIJKLMNOP'

const SM4 = require('gm-crypt').sm4

const sm4 = new SM4( { key: 'ABCDEFGHIJKLMNOP', mode: 'ecb' } );

例1:加密 0x0080 错误

sm4.encrypt( Buffer.from('0080', 'hex') )

// 加密结果:paO8EVoN9892D5bnp5uKxg==
// 正确结果应为:JA6bac2q2coRfY3Gh4vDDA==

例2:加密 0x0079 正确

sm4.encrypt( Buffer.from('0079', 'hex') )

// ZUbIPsOy2dqmhLgH0baOaw==
// 结果正确

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.