![]() |
Empower C |
install tutorials examples documentation license about |
|
11.0.11 released on Jul 25, 2022 |
encrypt-data <data> to [ define ] <result> password <password> \
[ input-length <input length> ] \
[ salt <salt> ] \
[ output-length [ define ] <output length> ] \
[ binary ] \
vv ... --lib="crypto" ...
// Original string to encrypt
char *orig_data="something to encrypt!";
// Encrypted data is in "res" variable
encrypt-data orig_data password "mypass" to define res
// Decrypt what was just encrypted, decrypted data is in "dec_data"
decrypt-data res password "mypass" to define dec_data
// Check that decrypted data matches the original
if (!strcmp (orig_data, dec_data)) {
@Success!
} else {
@Failure!
}
A more involved example below encrypts specific number of bytes (6 in this case). random-string is used to produce salt. The length of data to encrypt is given with "input-length" clause. The encrypted data is specified to be "binary" (meaning not as a human-readable string), so the "output-length" of such binary output is specified. The decryption thus uses "input-length" clause to specify the length of data to decrypt, and also "output-length" to get the length of decrypted data. Finally, the original data is compared with the decrypted data, and the length of such data must be the same as the original (meaning 6):// Original data (only the first 6 bytes are encrypted)
char *orig_data="something to encrypt!";
// Get 16 random bytes to be the salt
random-string to define newsalt length 16
// Encrypt data using salt and produce binary output (meaning it's not a null-terminated character string), with the
// length of such output in "encrypted_len" variable.
encrypt-data orig_data input-length 6 output-length define encrypted_len password "mypass" salt newsalt to define res binary
// Decrypt the data encrypted above. The length of encrypted data is passed in "encrypted_len" variable, and then length of decrypted data
// is obtained in "decrypted_len" variable.
decrypt-data res output-length define decrypted_len password "mypass" salt newsalt to define dec_data input-length encrypted_len binary
// Check if the 6 bytes of the original data matches decrypted data, and if exactly 6 bytes was decrypted
if (!strncmp(orig_data,dec_data, 6) && decrypted_len == 6) {
@Success!
} else {
@Failure!
}