Skip to content

加密相关

javascript
const core = require('cheese-js');
lua
local core = require('cheese-lua')

AES加解密: aesCrypt(data: string, key: string, operation: string, mode: string, iv?: string, padding?: boolean): string

参数:

  • string (data): 待处理数据
  • string (key): AES密钥
  • string (operation): encryptdecrypt
  • string (mode): 模式,如 CBCECB
  • string (iv): 初始化向量
  • boolean (padding): 是否使用补位

返回值:

  • 🟢string: 加解密结果
  • 🔴null

用法示例:

javascript
const core = require('cheese-js');
const cryptlib = core.cryptlib;
const key = cryptlib.generateAesKey(16);
const iv = cryptlib.generateAesIv();
const cipher = cryptlib.aesCrypt("hello cheese", key, "encrypt", "CBC", iv, true);
const plain = cryptlib.aesCrypt(cipher, key, "decrypt", "CBC", iv, true);
console.log("cipher:", cipher);
console.log("plain:", plain);
lua
local core = require('cheese-lua')
local cryptlib = core.cryptlib
local key = cryptlib.generateAesKey(16)
local iv = cryptlib.generateAesIv()
local cipher = cryptlib.aesCrypt("hello cheese", key, "encrypt", "CBC", iv, true)
local plain = cryptlib.aesCrypt(cipher, key, "decrypt", "CBC", iv, true)
print("cipher:", cipher)
print("plain:", plain)

生成AES密钥: generateAesKey(keyLength: number): string

参数:

  • number (keyLength): 密钥长度,常用 162432

返回值:

  • 🟢string: 生成的密钥
  • 🔴null

用法示例:

javascript
const core = require('cheese-js');
const cryptlib = core.cryptlib;
console.log(cryptlib.generateAesKey(16));
lua
local core = require('cheese-lua')
local cryptlib = core.cryptlib
print(cryptlib.generateAesKey(16))

生成AES向量: generateAesIv(): string

返回值:

  • 🟢string: 生成的IV
  • 🔴null

用法示例:

javascript
const core = require('cheese-js');
const cryptlib = core.cryptlib;
console.log(cryptlib.generateAesIv());
lua
local core = require('cheese-lua')
local cryptlib = core.cryptlib
print(cryptlib.generateAesIv())

生成RSA密钥对: generateRsaKeyPair(keyBits?: number): string[]

参数:

  • number (keyBits): 位数,常用 10242048

返回值:

  • 🟢string[]: 密钥对数组
  • 🔴null

用法示例:

javascript
const core = require('cheese-js');
const cryptlib = core.cryptlib;
const keyPair = cryptlib.generateRsaKeyPair(2048);
console.log("public:", keyPair[0]);
console.log("private:", keyPair[1]);
lua
local core = require('cheese-lua')
local cryptlib = core.cryptlib
local keyPair = cryptlib.generateRsaKeyPair(2048)
print("public:", keyPair[1])
print("private:", keyPair[2])

RSA加密: rsaEncrypt(data: string, key: string, isPublicKey: boolean): string

参数:

  • string (data): 明文数据
  • string (key): 公钥或私钥
  • boolean (isPublicKey): 是否为公钥

返回值:

  • 🟢string: 密文
  • 🔴null

用法示例:

javascript
const core = require('cheese-js');
const cryptlib = core.cryptlib;
const keyPair = cryptlib.generateRsaKeyPair(2048);
const cipher = cryptlib.rsaEncrypt("hello rsa", keyPair[0], true);
console.log(cipher);
lua
local core = require('cheese-lua')
local cryptlib = core.cryptlib
local keyPair = cryptlib.generateRsaKeyPair(2048)
local cipher = cryptlib.rsaEncrypt("hello rsa", keyPair[1], true)
print(cipher)

RSA解密: rsaDecrypt(data: string, key: string, isPublicKey: boolean): string

参数:

  • string (data): 密文数据
  • string (key): 公钥或私钥
  • boolean (isPublicKey): 是否为公钥

返回值:

  • 🟢string: 明文
  • 🔴null

用法示例:

javascript
const core = require('cheese-js');
const cryptlib = core.cryptlib;
const keyPair = cryptlib.generateRsaKeyPair(2048);
const cipher = cryptlib.rsaEncrypt("hello rsa", keyPair[0], true);
console.log(cryptlib.rsaDecrypt(cipher, keyPair[1], false));
lua
local core = require('cheese-lua')
local cryptlib = core.cryptlib
local keyPair = cryptlib.generateRsaKeyPair(2048)
local cipher = cryptlib.rsaEncrypt("hello rsa", keyPair[1], true)
print(cryptlib.rsaDecrypt(cipher, keyPair[2], false))

转Base64: toBase64(data: string): string

参数:

  • string (data): 原始字符串

返回值:

  • 🟢string: Base64字符串
  • 🔴null

用法示例:

javascript
const core = require('cheese-js');
const cryptlib = core.cryptlib;
console.log(cryptlib.toBase64("cheese"));
lua
local core = require('cheese-lua')
local cryptlib = core.cryptlib
print(cryptlib.toBase64("cheese"))

Base64转字符串: fromBase64(base64Data: string): string

参数:

  • string (base64Data): Base64字符串

返回值:

  • 🟢string: 原始字符串
  • 🔴null

用法示例:

javascript
const core = require('cheese-js');
const cryptlib = core.cryptlib;
const b64 = cryptlib.toBase64("cheese");
console.log(cryptlib.fromBase64(b64));
lua
local core = require('cheese-lua')
local cryptlib = core.cryptlib
local b64 = cryptlib.toBase64("cheese")
print(cryptlib.fromBase64(b64))

Released under the GPL-3.0 License.