crypto
The $crypto module provides symmetric encryption (e.g. AES), asymmetric encryption (e.g. RSA), and message digests (e.g. MD5, SHA).
$crypto.digest(message, algorithm[, options])
data{any} Data to digestkey{Key} Digest key (if applicable)algorithm{string} Digest algorithm, including:MD5SHA-1SHA-224SHA-256SHA-384SHA-512(see MessageDigest)
options{Object} Optional. Specifies the input/output types and formats.Returns {any} Return type depends on
options.
Computes a digest of data using algorithm. data can be a file, binary, Base64, hex, string, etc. The result can be returned as binary/Base64/hex/string or written to a file, depending on options. See Input/output types and formats.
// MD5 of string "abc"
console.log($crypto.digest("abc", "MD5"));
// SHA-256 of string "abc"
console.log($crypto.digest("abc", "SHA-256"));
console.log($crypto.digest("Auto.js", "SHA-256", \{ input: "string", output: "hex" \}));
// 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. The format depends onoptions.input.key{Key} Encryption key. Symmetric algorithms use one key; asymmetric algorithms use key pairs. See Key.algorithm{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
- ... (see javax.crypto.Cipher)
options{Object} Optional. Specifies the input/output types and formats.Returns {any} Return type depends on
options.
Encrypts data with key key using algorithm. data can be a file, binary, Base64, hex, string, etc. The result can be returned as binary/Base64/hex/string or written to a file, depending on options. See Input/output types and formats.
let message = "Hello Autojs";
// Key: AES requires 128/192/256 bits; here we use 16 chars (= 128 bits)
let str16 = "a".repeat(16);
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]
// Show AES output in Base64
toastLog(
$crypto.encrypt(message, key, "AES", {
output: "base64",
})
); // 7hu7UQKpjBeOqpEoOoHjxQ==
// AES default padding is PKCS5Padding, so the result is the same
toastLog(
$crypto.encrypt(message, key, "AES/ECB/PKCS5Padding", {
output: "base64",
})
); // 7hu7UQKpjBeOqpEoOoHjxQ==
// AES encrypt
let cipherText = $crypto.encrypt(message, key, "AES");
toastLog(cipherText); // [-18, 27, -69, 81, 2, -87, -116, 23, -114, -86, -111, 40, 58, -127, -29, -59]
// RSA256KeyPair
let algorithm = "RSA";
let length = "2048";
// Generate RSA key pair
key = $crypto.generateKeyPair(algorithm, length);
let message = "Hello Autojs";
// RSA encrypt
cipherText = $crypto.encrypt(message, key.publicKey, "RSA/ECB/PKCS1Padding");
toastLog(cipherText); // [114, 99, -93, 6, -88, 8, -12, -53, -68, -15, ...]$crypto.decrypt(data, key, algorithm[, options])
data{any} Ciphertext. The format depends onoptions.input.key{Key} Decryption key. Symmetric algorithms use a single key; asymmetric algorithms require a key pair. See Key.algorithm{string} Decryption 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
- ... (see javax.crypto.Cipher)
options{Object} Optional. Specifies the input/output types and formats.Returns {any} Return type depends on
options.
Decrypt data with key key using algorithm. data can be a file, binary, Base64, hex, string, etc. The decrypted result can be returned as binary/Base64/hex/string or written directly to a file, depending on options. See Input/output types and formats.
// AES encrypt, output as Base64
let key = new $crypto.Key("1234567890123456");
let cipherText = $crypto.encrypt("Hello, Auto.js Pro!", "AES", {
input: "string",
"output": "base64"
});
// AES decrypt, decode Base64 to string
let plaintext = $crypto.decrypt(cipherText, key, "AES", {
"input": "base64",
"output": "string"
});
toastLog(plaintext);$crypto.generateKeyPair(algorithm[, length])
algorithm{string} Algorithm, including:DHDSAECRSA
length{number} Key length. Depends on algorithm (e.g. modulus length in bits). Default 256.Returns {KeyPair}
Generate a key pair containing a public key and a private key. For RSA, you can encrypt with the private key and decrypt with the public key for signatures, or encrypt with the public key and decrypt with the private key for data encryption.
let keyPair = $crypto.generateKeyPair("RSA");
console.log("Public key:", keyPair.publicKey);
console.log("Private key:", keyPair.privateKey);
// Encrypt with public key, decrypt with private key
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);
// Decrypt with public key, encrypt with private key
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
Key object. You can construct it directly, e.g. new Key('12345678').
new Key(data[, options])
data{any} Key material. The format depends onoptions.input. Default is string.options{Object} Optional. See Input/output types and formats.
Constructor. Creates a Key object.
let key = new $crypto.Key('1234567890123456');
// Get key bytes
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[]}
Raw bytes of the key.
KeyPair
Key pair object. Can be generated by $crypto.generateKeyPair() or constructed via the constructor.
new KeyPair(publicKey, privateKey[, options])
[Added in Pro 8.7.2]
publicKey{any} Public key data. Format depends onoptions.input. Default is string.privateKey{any} Private key data. Format depends onoptions.input. Default is string.options{Object} Optional. See Input/output types and formats.
Constructor. Creates a KeyPair object.
let keyPair = $crypto.generateKeyPair("RSA");
// Get public/private key bytes and convert to Base64
let data = {
publicKey: base64Bytes(keyPair.publicKey.data),
privateKey: base64Bytes(keyPair.privateKey.data),
};
// Reconstruct a KeyPair 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.
Input/output types and formats
options {object} Specifies input/output types and formats for encryption, decryption, and digests.
input{string} Input type. Specifies the source data type for encrypt/decrypt/digest. If the input is a string, default isstring; otherwise default isbytes. Values:stringString database64Base64 datahexHex (base16) databytesJava byte arrayfileFile path. Reads data from a file for processing
output{string} Output type. Specifies the result data type. For encrypt/decrypt, default isbytes; for digest, default ishex. Values:stringString database64Base64 datahexHex (base16) databytesJava byte arrayfileFile output. Writes result to a file; must also specifydest
dest{string} Output file path used whenoutputisfileencoding{string} Character encoding. Wheninputisstring, used to encode the input string into bytes; whenoutputisstring, used to decode bytes into a string. Defaultutf-8iv{string} | {bytes} Initialization vector for AES etc (optional). Added in Pro 9.2.12.
let filepath = files.join("./test.txt", "1.txt");
let message = "Hello Autojs";
$files.write(filepath, message);
let str16 = "a".repeat(16);
let key = new $crypto.Key(str16);
// Base64 content to encrypt
let base64Content = $base64.encode(key);
// Hex content to encrypt
let hexContent = "48656c6c6f204175746f6a73";
// Encrypt a file, output as Base64
console.log($crypto.encrypt(filepath, key, "AES", \{ input: "file", output: "base64" \}));
// Encrypt a file, output to another file
console.log($crypto.encrypt(filepath, key, "AES", \{ input: "file", output: "file", dest: "./output.txt" \}));
// Encrypt Base64 content, output as Base64
console.log($crypto.encrypt("SGVsbG8gQXV0b2pz", key, "AES", \{ input: "base64", output: "base64" \}));
// Encrypt hex content, output as hex
console.log($crypto.encrypt("48656c6c6f204175746f6a73", key, "AES", \{ input: "hex", output: "hex" \}));
console.log($crypto.encrypt("Hello Autojs", key, "AES"));
// [-18, 27, -69, 81, 2, -87, -116, 23, -114, -86, -111, 40, 58, -127, -29, -59]
// Digest file MD5, output as hex
console.log($crypto.digest(filepath, "MD5", \{ input: "file", output: "hex" \}));