- Global - 全局对象
- Automator - 自动化
- AutoJs6 - 本体应用
- App - 通用应用
- Color - 颜色
- Image - 图像
- OCR - 光学字符识别
- Barcode - 条码
- QR Code - 二维码
- Keys - 按键
- Device - 设备
- Storage - 储存
- File - 文件
- Engine - 引擎
- Task - 任务
- Module - 模块
- Plugins - 插件
- Toast - 消息浮动框
- Notice - 消息通知
- Console - 控制台
- Shell
- Shizuku
- Media - 多媒体
- Sensor - 传感器
- Recorder - 记录器
- Timer - 定时器
- Thread - 线程
- Continuation - 协程
- Event - 事件监听
- Dialog - 对话框
- Floaty - 悬浮窗
- Canvas - 画布
- UI - 用户界面
- Web - 万维网
- HTTP
- Base64
- Crypto - 密文
- OpenCC - 中文转换
- Internationalization - 国际化
- Standardization - 标准化
- E4X
- UiSelector - 选择器
- UiObject - 控件节点
- UiObjectCollection - 控件集合
- UiObjectActions - 控件节点行为
- WebSocket
- EventEmitter - 事件发射器
- ImageWrapper - 包装图像类
- App - 应用枚举类
- Color - 颜色类
- Version - 版本工具类
- Polyfill - 代码填泥
- Arrayx - Array 扩展
- Numberx - Number 扩展
- Mathx - Math 扩展
- Glossaries - 术语
- HttpHeader - HTTP 标头
- HttpRequestMethods - HTTP 请求方法
- MimeType - MIME 类型
- NotificationChannel - 通知渠道
- Data Types - 数据类型
- Omnipotent Types - 全能类型
- Storage - 存储类
- AndroidBundle
- AndroidRect
- CryptoCipherOptions
- CryptoKey
- CryptoKeyPair
- ConsoleBuildOptions
- HttpRequestBuilderOptions
- HttpRequestHeaders
- HttpResponseBody
- HttpResponseHeaders
- HttpResponse
- InjectableWebClient
- InjectableWebView
- NoticeOptions
- NoticeChannelOptions
- NoticePresetConfiguration
- NoticeBuilder
- Okhttp3HttpUrl
- OcrOptions
- Okhttp3Request
- OpenCVPoint
- OpenCVRect
- OpenCVSize
- OpenCCConversion
AutoJs6 文档 - 6.6.4
CryptoCipherOptions#
CryptoCipherOptions 是一个用于 密码 (Cipher)
加解密的选项接口, 主要用于 密文 模块.
常见相关方法或属性:
- crypto.encrypt(data, key, transformation, options)
- crypto.decrypt(data, key, transformation, options)
CryptoCipherOptions
[p?] input#
- [
'string'
] {'file'
|'base64'
|'hex'
|'string'
} - 输入数据类型
指定密码的输入数据类型.
let key = new crypto.Key('test'.repeat(4));
/* 输入数据类型为 string, 即字符串. */
/* 此时 input 选项可省略, 因 'string' 为默认值. */
console.log(crypto.encrypt('hello world', key, 'AES', { input: 'string' })); // [-52, 100, 9, -87, 1, 80, 33, -26, -70, -9, 17, 106, 38, 63, -94, -41]
/* 输入数据类型为 base64. */
console.log(crypto.encrypt(base64.encode('hello world'), key, 'AES', { input: 'base64' })); // [-52, 100, 9, -87, 1, 80, 33, -26, -70, -9, 17, 106, 38, 63, -94, -41]
/* 输入数据类型为 hex, 即十六进制值. */
console.log(crypto.encrypt('68656c6c6f20776f726c64', key, 'AES', { input: 'hex' })); // [-52, 100, 9, -87, 1, 80, 33, -26, -70, -9, 17, 106, 38, 63, -94, -41]
/* 输入数据类型为 file, 即文件. */
/* input 参数作为文件名, 支持绝对路径及相对路径. */
console.log(crypto.encrypt('./test.txt', key, 'AES', { input: 'file' })); /* 结果取决于具体文件内容. */
/* 加密并将加密结果 (字符串值) 写入文件. */
crypto.encrypt('68656c6c6f20776f726c64', key, 'AES', { input: 'hex', output: 'file', dest: 'encrypted.txt' });
[p?] output#
- [
'bytes'
] {'bytes'
|'base64'
|'hex'
|'string'
|'file'
} - 输出数据类型
指定密码的输出数据类型.
输出数据类型默认是 'bytes'
, 即字节数组. 如需输出字符串, 可指定 'string'
值.
output
属性通常用于 crypto.decrypt 方法的选项参数中.
let key = new crypto.Key('test'.repeat(4));
let encrypted = crypto.encrypt('hello world', key, 'AES');
/* 解密时默认输出 bytes 格式, 即字节数组. */
console.log(crypto.decrypt(encrypted, key, 'AES')); // [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
/* 解密输出字符串值. */
console.log(crypto.decrypt(encrypted, key, 'AES', { output: 'string' })); // hello world
/* 解密输出十六进制值. */
console.log(crypto.decrypt(encrypted, key, 'AES', { output: 'hex' })); // 68656c6c6f20776f726c64
/* 解密输出字符串并写入文件. */
crypto.decrypt(encrypted, key, 'AES', { output: 'file', dest: './decrypted.txt' });
注: 当
output
指定为'file'
时, 需同时指定dest
属性, 否则将抛出异常.
[p?] encoding#
- [
'UTF-8'
] {'US-ASCII'
|'ISO-8859-1'
|'UTF-8'
|'UTF-16BE'
|'UTF-16LE'
|'UTF-16'
} - 编码
指定输入或输出的字符串编码.
encoding
属性仅在 input
属性为 'string'
或 output
属性为 'string'
时有效, 其他情况 encoding
属性的值将被忽略.
let key = new crypto.Key('test'.repeat(4));
let encrypted = crypto.encrypt('hello world', key, 'AES', { input: 'string', encoding: 'utf-16', output: 'hex' });
console.log(encrypted); // 0005750e34f9827f925780eaabd785c18281d84320ccb380b116c3239846e3b4
let decrypted = crypto.decrypt(encrypted, key, 'AES', { input: 'hex', output: 'string', encoding: 'utf-16' });
console.log(decrypted); // hello world
[p?] dest#
- { string } - 目标路径
指定数据处理结果需要写入的目标路径, 是 "destination" 的简写.
dest
属性仅当 output
属性为 'file'
时有效.
let key = new crypto.Key('test'.repeat(4));
let encrypted = crypto.encrypt('hello world', key, 'AES');
/* 解密输出字符串并写入文件. */
crypto.decrypt(encrypted, key, 'AES', { output: 'file', dest: './decrypted.txt' });
[p?] iv#
- { string | JsByteArray | ByteArray | java.security.spec.AlgorithmParameterSpec } - 初始向量
iv
属性用于指定初始向量.
IV 称为初始向量, 不同 IV 加密后的数据结果不同, 加密和解密需要相同的 IV. 对于每个块来说, key 是不变的, 但仅第一个块的 IV 由用户提供, 其他块 IV 自动生成. IV 的长度通常被认为是 16 字节 (可能会自动按需补齐或截断).
例如在使用 CBC 有向量模式时, iv
属性是必要的, 而如果使用 ECB 无向量模式, 则无需 iv
属性.
let key = new crypto.Key('test'.repeat(4));
/* 将抛出异常, 因为缺少 IV. */
/* java.security.InvalidAlgorithmParameterException: IV must be specified in CBC mode */
crypto.encrypt('hello world', key, 'AES/CBC/PKCS5Padding');
/* 传入一个 iv 参数 (长度为 16) 进行加密. */
/* 加密的模式为 CBC. */
let encrypted = crypto.encrypt('hello world', key, 'AES/CBC/PKCS5Padding', { iv: 'a 16-byte string' });
console.log(encrypted); // [-91, 30, -6, 67, -61, -32, -56, 26, 0, -85, -13, 113, -45, -68, -118, -115]
/* 使用与加密时相同的 iv 参数进行解密. */
/* 除 iv 参数外, Key, 工作模式, 填充方式也都需要与加密时相同. */
let decrypted = crypto.decrypt(encrypted, key, 'AES/CBC/PKCS5Padding', { input: 'hex', output: 'string', iv: 'a 16-byte string' });
console.log(decrypted); // hello world
iv
参数也支持 AlgorithmParameterSpec
类型:
let key = new crypto.Key('test'.repeat(4));
let iv = new javax.crypto.spec.IvParameterSpec(new java.lang.String('a 16-byte string').getBytes());
let encrypted = crypto.encrypt('hello world', key, 'AES/CBC/PKCS5Padding', { iv: iv });
console.log(encrypted);
let decrypted = crypto.decrypt(encrypted, key, 'AES/CBC/PKCS5Padding', { input: 'hex', output: 'string', iv: iv });
console.log(decrypted);