encrypt_string
encrypts a string as a string or a raw
vector and decrypt_string
decrypts the encrypted string or a raw
vector (encrypted using encrypt_string
)
encrypt_string(string, key = "pass", pkey = NULL, ascii = TRUE, url = FALSE)
string | A string(character vector of length 1) without embedded NULL to be encrypted or a raw vector. |
---|---|
key | For symmetric encryption, 'pkey' should be NULL (default) and 'key' can be either a string (Default is 'pass') or a raw object. For asymmetric encryption, both 'key' (private key of the encrypter) and 'pkey' (public key of the decrypter) should be raw objects. |
pkey | See 'key' |
ascii | (flag) When TRUE (default), the output is a string after base64 encoding. Else, the output is a raw vector. |
url | (flag, default: FALSE) Whether the encoded string has to be url friendly. |
An encrypted string or a raw vector.
# symmetric case: temp <- encrypt_string("hello, how are you", key = "secret") all( is.character(temp) , decrypt_string(temp, "secret") == "hello, how are you" , class(try(decrypt_string(temp, "nopass"), silent = TRUE)) == "try-error" )#> [1] TRUE# string encoded as raw res <- encrypt_string("tatvamasi", ascii = FALSE) res#> [1] 62 58 a4 e8 e3 79 f8 1e 8d 9b 9b fe 04 d7 34 19 f4 b1 60 80 f7 34 e3 97 f3#> [1] TRUE# asymmetric case: alice <- keypair() bob <- keypair() temp <- encrypt_string("hello asymmetric", alice$private_key, bob$public_key) temp2 <- decrypt_string(temp, bob$private_key, alice$public_key) identical("hello asymmetric", temp2)#> [1] TRUE