Encryption Functions
For all encryption functions there are again the same rules as for string functions, i.e.:
- when the argument is ‘nullptr’, it returns again ‘nullptr’;
- when the (first) argument is ‘pointer’, it returns again ‘pointer’.
rsa_encrypt_string(str, public_key)
rsa_encrypt_ustring(ustr, public_key)
(since EVL 2.6)
First argument ‘(u)str’ is mandatory and is of data type ‘string’ or ‘ustring’. In both cases the function returns ‘string’ data type.
Second argument is also mandatory and contains the public key previously defined in the mapping by
static rsa_public_key public_key("/path/to/key.pub");
Encrypted string is actually binary data, so if there is a need to store this encrypted data in text mode, e.g. in CSV file, then for example ‘str_to_base64’ function can be used. See example below.
rsa_decrypt_string(str, private_key)
rsa_decrypt_ustring(str, private_key)
(since EVL 2.6)
First argument ‘str’ is mandatory and is a (binary) string previously encrypted by ‘rsa_encrypt_string’ or ‘rsa_encrypt_ustring’. It is necessary to keep the string or ustring couple, e.g. ‘rsa_decrypt_ustring’ use for string encrypted by ‘rsa_encrypt_ustring’. In both case the function returns ‘string’ data type.
Second argument is mandatory and contains the private key previously defined in the mapping by
static rsa_private_key private_key("/path/to/key.priv");
Mapping example for encryption
Both output fields are of type string, input field ‘name’ is of type ustring.
static rsa_public_key pubkey("/path/to/key.pub"); out->name_encrypted_binary = rsa_encrypt_ustring(in->name,pubkey); out->name_encrypted_textual = str_to_base64(rsa_encrypt_ustring(in->name,pubkey)); // Proper way in this particular example would be of course to use // out->name_encrypted_textual = str_to_base64(out->name_encrypted_binary); // to avoid applying encryption function twice
Mapping example for decryption afterwards
Both output fields are of type ustring and store the same value.
static rsa_private_key privkey("/path/to/key.priv"); out->name1 = rsa_decrypt_ustring(in->name_encrypted_binary,privkey); out->name2 = rsa_decrypt_ustring( base64_to_str(in->name_encrypted_textual),privkey);