# @xnsk/crypto

> 加解密使用前后端交互的方式，具体请详见加密设计文档。

Latest version **1.2.0** (published 2023-09-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install @xnsk/crypto
pnpm add @xnsk/crypto
yarn add @xnsk/crypto
bun add @xnsk/crypto
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2023-09-22 |
| First published | 2023-03-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14 |
| Dependencies | 3 |
| Unpacked size | 2.2 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | lvjianqin, zhiqiang03, gzb9527, gaopy, xnsk-openlayers-sdk, weiyurun |

## Links

- npm: https://www.npmjs.com/package/@xnsk/crypto
- npm.io page: https://npm.io/package/@xnsk/crypto

## Dependencies (3)

- [axios](https://npm.io/package/axios.md) ^1.3.4
- [crypto-js](https://npm.io/package/crypto-js.md) ^4.1.1
- [jsencrypt](https://npm.io/package/jsencrypt.md) ^3.3.2

## Recent versions

- 1.2.0 (latest) — 2023-09-22
- 1.1.1 — 2023-07-20
- 1.1.0 — 2023-07-20
- 1.0.0 — 2023-03-09

## README

# web加解密通用组件
加解密使用前后端交互的方式，具体请详见加密设计文档。

## 安装

```bash
npm i @xnsk/crypto

yarn add @xnsk/crypto

pnpm add @xnsk/crypto

cnpm i @xnsk/crypto
```

## 使用

下面代码为示例代码，其中密钥片断、请求接口和参数为测试数据

``` ts
  import { Crypto } from '@xnsk/crypto'
  
  const crypto = new Crypto({
    // 密钥
    rsaKey: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHEJojo33lFegWSRKwcibZgBoKO7oGfMTPGcUzPifl3QLT4Xo9czolYZ1hgaQh+birl9EP0LrRxosA9/QyiEBwHC0kut5GpbBNaJJSHwvYrd/IB12o96ZUoLYhg+Qf+4ejN45fylmAA6fJW2S6YMc8xi8kIc0X5oQIDAQAB',
    axios,
    onlyData: false  // 是否只返回data。false则将接口返回值全部返给前端
  })

  // 使用加密方式发起请求
  const res = await crypto.post('/api/login/xxx', {
    username: 'username',
    password: 'password',
  })

```

## 更多示例

``` ts
const crypto = new Crypto({
  rsaKeyFragment: 'aJJSHwvYrd/IB12o96',
  axios,
})

// 生成aes key，默认32位
const key = crypto.generateAESKey()

// 使用密钥加密
const cipher = crypto.encrypt('Data String', key);

// 使用密钥解密
const data = crypto.decrypt(cipher, key);

```

---
_Source: https://npm.io/package/@xnsk/crypto · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
