crypto
crypto
The $crypto module provides support for symmetric encryption (such as AES), asymmetric encryption (such as RSA), and message digest (such as MD5, SHA).
$crypto.digest(message, algorithm[, options])
data
{any} The message that needs to be digestedalgorithm
{string} Message digest algorithm, including:MD5
SHA-1
SHA-224
SHA-256
SHA-384
SHA-512
For details, please refer to MessageDigest
options
{Object} optional options, used to specify input and output type and format- Return {any} returns different data according to the output type specified by
options
The data data
uses the algorithm algorithm
to calculate the message digest. The data data
can be file, binary, base64, hex, string and other data. After decryption, the data can be returned to binary, base64, hex, string or directly written in To the file, see [Types and Formats of Input and Output](#Types and Formats of Input and Output) for details.
// Calculate the md5 of the string abc
console.log($crypto.digest("abc", "MD5"));
// Calculate the sha-256 of the string abc
console.log($crypto.digest("abc", "SHA-256"));
console.log($crypto.digest("CloudControl", "SHA-256", {input: "string", output: "hex" }));
// Calculate md5 of file /sdcard/1.txt
console.log($crypto.digest("/sdcard/1.txt", "MD5", {
input: "file"
}));
$crypto.encrypt(data, key, algorithm[, options])
data
{any} plaintext message, with parameters of different formats according to the input type specified byoptions
key
{Key} Encryption key. Symmetric encryption algorithm uses a single key, asymmetric encryption requires a key pair, see Keyalgorithm
{string} encryption algorithm, including:- AES
- AES/ECB/NoPadding
- AES/ECB/PKCS5Padding
- AES/CBC/NoPadding
- AES/CBC/PKCS5Padding
- AES/CFB/NoPadding
- AES/CFB/PKCS5Padding
- AES/CTR/NoPadding
- AES/CTR/PKCS5Padding
- AES/OFB/PKCS5Padding
- AES/OFB/PKCS5Padding
- RSA/ECB/PKCS1Padding
- RSA/ECB/NoPadding
- ... For details, please refer to javax.crypto.Cipher
options
{Object} optional options, used to specify input and output type and format- Return {any} returns different data according to the output type specified by
options
Use the key key
to encrypt the data data
with the encryption algorithm algorithm
. The data data
can be file, binary, base64, hex, string and other data. After encryption, the data can be returned to binary, base64, hex. , String or write directly to the file, see [Types and Formats of Input and Output](#Types and Formats of Input and Output) for details.
let message = "Hello CloudControl Pro";
// Key, because AES and other algorithms require 128/192/256 bits, our length here is 16, which is 128bits
let str16 = "a".repeat(16);// repeat(n)This method can make a string repeat a specified number of times。
let key = new $crypto.Key(str16);
// AES
toastLog($crypto.encrypt(message, key, "AES")); // [-18, 27, -69, 81, 2, -87, -116, 23, -114, -86, -111, 40 , 58, -127, -29, -59]
// AES output results are displayed in base64
toastLog(
$crypto.encrypt(message, key, "AES", {
output: "base64",
})
); // d0Om4vow/J+DQrAFGwKyItnZ29IOkzuWFWmbrTmx/DE=
// AES default plaintext padding mode PKCS5Padding, the result is the same as above
toastLog(
$crypto.encrypt(message, key, "AES/ECB/PKCS5Padding", {
output: "base64",
})
); // d0Om4vow/J+DQrAFGwKyItnZ29IOkzuWFWmbrTmx/DE=
// AES encryption
let cipherText = $crypto.encrypt(message, key, "AES");
toastLog(cipherText); // [119, 67, -90, -30, -6, 48, -4, -97, -125, 66, -80, 5, 27, 2, -78, 34, -39, -39, -37, -46, 14, -109, 59, -106, 21, 105, -101, -83, 57, -79, -4, 49]
// RSA256KeyPair
let algorithm = "RSA";
let length = "2048";
// Generate RSA key pair
key = $crypto.generateKeyPair(algorithm, length);
let message = "Hello CloudControl Pro";
// RSA encryption
cipherText = $crypto.encrypt(message, key.publicKey, "RSA/ECB/PKCS1Padding");
toastLog(cipherText); // [46, -51, -111, -78, 29, 28, -80, -63, 90, ...]
$crypto.decrypt(data, key, algorithm[, options])
data
{any} The input type specified by the cipher text messageoptions
is a parameter of different formatskey
{Key} Decryption key. Symmetric encryption algorithm uses a single key, asymmetric encryption requires a key pair, see Keyalgorithm
{string} encryption algorithm, including:- AES
- AES/ECB/NoPadding
- AES/ECB/PKCS5Padding
- AES/CBC/NoPadding
- AES/CBC/PKCS5Padding
- AES/CFB/NoPadding
- AES/CFB/PKCS5Padding
- AES/CTR/NoPadding
- AES/CTR/PKCS5Padding
- AES/OFB/PKCS5Padding
- AES/OFB/PKCS5Padding
- RSA/ECB/PKCS1Padding
- RSA/ECB/NoPadding
- ... For details, please refer to javax.crypto.Cipher
options
{Object} optional options, used to specify input and output type and format- Return {any} returns different data according to the output type specified by
options
Use the key key
to decrypt the data data
with the decryption algorithm algorithm
. The data data
can be file, binary, base64, hex, string and other data. After decryption, the data can be returned to binary, base64, hex , String or write directly to the file, see [Types and Formats of Input and Output](#Types and Formats of Input and Output) for details.
// AES encryption, encrypted as base64 data
let key = new $crypto.Key("1234567890123456");
let cipherText = $crypto.encrypt("Hello, CloudControl Pro!", key , "AES", {
input: "string",
"output": "base64"
});
// AES decryption, decrypt base64 data into a string
let plaintext = $crypto.decrypt(cipherText, key, "AES", {
"input": "base64",
"output": "string"
});
toastLog(plaintext);
$crypto.generateKeyPair(algorithm[, length])
algorithm
{string} encryption algorithm, includingDH
DSA
EC
RSA
length
{number} The length of the key. Related to the algorithm, such as the length of the modulus specified in digits. The default is 256.- Return {KeyPair}
Generate a pair of keys, including a public key and a private key. For example, in the RSA encryption algorithm, we can use private key encryption and public key decryption for signature; or public key encryption and private key decryption for data encryption.
let keyPair = $crypto.generateKeyPair("RSA");
console.log("The public key is", keyPair.publicKey);
console.log("The private key is", keyPair.privateKey);
// Public key encryption, private key decryption
let plainText = "Hello World";
let bytes = $crypto.encrypt(plainText, keyPair.publicKey, "RSA");
let decryptedText = $crypto.decrypt(bytes, keyPair.privateKey, "RSA", {
output: "string"
});
console.log(decryptedText);
// Public key decryption, private key encryption
let base64 = $crypto.encrypt(plainText, keyPair.privateKey, "RSA", {
output: "base64"
});
decryptedText = $crypto.decrypt(base64, keyPair.publicKey, "RSA", {
input: "base64",
output: "string"
});
console.log(decryptedText);
Key
The key object. It can be constructed directly through the constructor. For example, new Key('12345678')
.
new Key(data[, options])
data
{any} The content of the key depends on the input format of theoptions
option, the default is a string formatoptions
{Object} Optional parameters, see [Type and format of input and output](#Type and format of input and output)
The constructor, which constructs a Key object.
let key = new $crypto.Key('1234567890123456');
// Get the binary data of Key
let data = key.data;
// Convert to base64
let base64 = android.util.Base64.encodeToString(data, android.util.Base64.NO_WRAP);
// reconstruct a Key from base64
let copiedKey = new $crypto.Key(base64, {input: "base64"});
console.log(copiedKey.toString());
Key.data
byte\[\]
Key binary data.
KeyPair
The key pair object. It can be generated by the $crypto.generateKeyPair()
function or constructed by the constructor.
new KeyPair(publicKey, privateKey[, options])
[Added in Pro 8.7.2]
publicKey
{any} public key data, according to the input format of theoptions
option, the default is a string formatprivateKey
{any} The data of the private key depends on the input format of theoptions
option, the default is a string formatoptions
{Object} Optional parameters, see [Type and format of input and output](#Type and format of input and output)
The constructor, which constructs a KeyPair object.
let keyPair = $crypto.generateKeyPair("RSA");
// Obtain the binary data of the public key and private key and convert it to base64
let data = {
publicKey: base64Bytes(keyPair.publicKey.data),
privateKey: base64Bytes(keyPair.privateKey.data),
};
// reconstruct a Key from base64
let copiedKeyPair = new $crypto.KeyPair(data.publicKey, data.privateKey, {input: "base64"});
console.log(copiedKeyPair);
function base64Bytes(bytes) {
return android.util.Base64.encodeToString(bytes, android.util.Base64.NO_WRAP);
}
KeyPair.privateKey
- {Key}
Private key.
KeyPair.publicKey
- {Key}
Public key.
Type and format of input and output
options
{object} is used to specify the type and format of input and output for encryption, decryption, and message digest.
-
input
{string} Input type, used to specify the type of source data for encryption, decryption, and digest. If the input is a string, the default isstring
; otherwise, the default isbytes
. Optional values include:string
data in string formatbase64
data in base64 formathex
base16 format databytes
Java binary byte arrayfile
file type, the data will be read from the file for encryption and decryption
-
output
{string} Output type, used to specify the type of data after encryption, decryption, and digest. For encryption and decryption, the default isbytes
; for message digest, the default ishex
. Optional values include:string
data in string formatbase64
data in base64 formathex
base16 format databytes
Java binary byte arrayfile
file type, to write the processed data to the file, thedest
parameter must be specified at the same time
-
dest
{string} Output file path, when the type ofoutput
isfile
, it is used to specify the path of the output file after encryption, decryption, and digest -
encoding
{string} encoding format, when the type ofinput
isstring
, it is used to specify the character encoding used to convert the input string into binary data; when the type ofoutput
isstring
, it is used to specify the output The character encoding used to convert data to string data. The default isutf-8
-
iv
{string} | {bytes} Specifies the initialization vector parameter of encryption such as AES, optional. Added in Pro 9.2.12 version.
let filepath = files.join("./test", "1.txt");
let message = "Hello CloudControl Pro";
$files.write(filepath, message);
let str16 = "a".repeat(16);// repeat(n)This method can make a string repeat a specified number of times。
let key = new $crypto.Key(str16);
// Create base64 content before encryption
let base64Content = $base64.encode(key);
// hex content before encryption
let hexContent = "48656c6c6f204175746f6a73";
// Encrypted file, output format is binary
console.log($crypto.encrypt(filepath, key, "AES", {input: "file", output: "base64" }));
// Encrypt the file and output to another file
console.log($crypto.encrypt(filepath, key, "AES", {input: "file", output: "file", dest: "./output.txt" }));
// Encrypt base64 content, the output format is base64
console.log($crypto.encrypt("SGVsbG8gQXV0b2pz", key, "AES", {input: "base64", output: "base64" }));
// Encrypt hex content, the output format is hex
console.log($crypto.encrypt("48656c6c6f204175746f6a73", key, "AES", {input: "hex", output: "hex" }));
console.log($crypto.encrypt("Hello CloudControl Pro", key, "AES"));
// [119, 67, -90, -30, -6, 48, -4, -97, -125, 66, -80, 5, 27, 2, -78, 34, -39, -39, -37, -46, 14, -109, 59, -106, 21, 105, -101, -83, 57, -79, -4, 49]
// Calculate file MD5, output as hex
console.log($crypto.digest(filepath, "MD5", {input: "file", output: "hex" }));