Title: | Encrypt and Decrypt Strings, R Objects and Files |
---|---|
Description: | A consistent interface to encrypt and decrypt strings, R objects and files using symmetric and asymmetric key encryption. |
Authors: | KS Srikanth [aut, cre] |
Maintainer: | KS Srikanth <[email protected]> |
License: | GPL-3 |
Version: | 0.2.2 |
Built: | 2024-10-31 02:48:29 UTC |
Source: | https://github.com/talegari/safer |
A consistent interface to encrypt/decrypt strings, R objects, files. Alternatives for base R functions 'serialize/unserialize', 'save/load' are provided.
The following functions are provided:
encrypt_string/decrypt_string: encrypt_string
encrypts a
string as a string and decrypt_string
decrypts the encrypted
string(encrypted using encrypt_string
)
encrypt_object/decrypt object: encrypt_object
encrypts a R
object as a raw object or a string and decrypt_object
decrypts a raw
object or a string(encrypted by encrypt_object
)
encrypt_file/decrypt_file: encrypt_file
encrypts file into
another binary or ascii file. decrypt_file
) decrypts a file
(encrypted by encrypt_file
)
save_object/retrieve_object: save_object
encrypts a R
object to raw or text connection or a file. retrieve_object
decrypts
a raw or a text connection or a file (encrypted by save_object
.)
Maintainer: KS Srikanth [email protected]
Useful links:
encrypt_file
) encrypts a file as a binary or a ascii
file. decrypt_file
) decrypts a text or a binary file (encrypted by
encrypt_file
)
decrypt_file(infile, key = "pass", pkey = NULL, ascii = FALSE, outfile)
decrypt_file(infile, key = "pass", pkey = NULL, ascii = FALSE, outfile)
infile |
file to be decrypted |
key |
For symmetric decryption, 'pkey' should be NULL (default) and 'key' can be either a string (Default is 'pass') or a raw object. For asymmetric decryption, both 'key' (private key of the decrypter) and 'pkey' (public key of the encrypter) should be raw objects. |
pkey |
See 'key' |
ascii |
|
outfile |
Non-existant file where the decrypted output is to be written |
An invisible TRUE
# symmetric case: write.table(iris, "iris.csv") all( encrypt_file("iris.csv", outfile = "iris_encrypted.bin") , file.exists("iris_encrypted.bin") , decrypt_file("iris_encrypted.bin", outfile = "iris_2.csv") , file.exists("iris_2.csv") , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv") , unlink("iris.csv") == 0 , unlink("iris_2.csv") == 0 , unlink("iris_encrypted.bin") == 0 ) write.table(iris, "iris.csv") all( encrypt_file("iris.csv", outfile = "iris_encrypted.txt", ascii = TRUE) , file.exists("iris_encrypted.txt") , decrypt_file("iris_encrypted.txt", outfile = "iris_2.csv", ascii = TRUE) , file.exists("iris_2.csv") , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv") , unlink("iris.csv") == 0 , unlink("iris_2.csv") == 0 , unlink("iris_encrypted.txt") == 0 ) # asymmetric case: alice <- keypair() bob <- keypair() write.table(iris, "iris.csv") all( encrypt_file("iris.csv", alice$private_key, bob$public_key, outfile = "iris_encrypted.bin") , file.exists("iris_encrypted.bin") , decrypt_file("iris_encrypted.bin", bob$private_key, alice$public_key, outfile = "iris_2.csv") , file.exists("iris_2.csv") , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv") , unlink("iris.csv") == 0 , unlink("iris_2.csv") == 0 , unlink("iris_encrypted.bin") == 0 )
# symmetric case: write.table(iris, "iris.csv") all( encrypt_file("iris.csv", outfile = "iris_encrypted.bin") , file.exists("iris_encrypted.bin") , decrypt_file("iris_encrypted.bin", outfile = "iris_2.csv") , file.exists("iris_2.csv") , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv") , unlink("iris.csv") == 0 , unlink("iris_2.csv") == 0 , unlink("iris_encrypted.bin") == 0 ) write.table(iris, "iris.csv") all( encrypt_file("iris.csv", outfile = "iris_encrypted.txt", ascii = TRUE) , file.exists("iris_encrypted.txt") , decrypt_file("iris_encrypted.txt", outfile = "iris_2.csv", ascii = TRUE) , file.exists("iris_2.csv") , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv") , unlink("iris.csv") == 0 , unlink("iris_2.csv") == 0 , unlink("iris_encrypted.txt") == 0 ) # asymmetric case: alice <- keypair() bob <- keypair() write.table(iris, "iris.csv") all( encrypt_file("iris.csv", alice$private_key, bob$public_key, outfile = "iris_encrypted.bin") , file.exists("iris_encrypted.bin") , decrypt_file("iris_encrypted.bin", bob$private_key, alice$public_key, outfile = "iris_2.csv") , file.exists("iris_2.csv") , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv") , unlink("iris.csv") == 0 , unlink("iris_2.csv") == 0 , unlink("iris_encrypted.bin") == 0 )
encrypt_object
encrypts a R object as a raw object or a
string and decrypt_object
decrypts a raw object or a
string(encrypted by encrypt_object
)
decrypt_object(object, key = "pass", pkey = NULL)
decrypt_object(object, key = "pass", pkey = NULL)
object |
Object to be decrypted |
key |
For symmetric decryption, 'pkey' should be NULL (default) and 'key' can be either a string (Default is 'pass') or a raw object. For asymmetric decryption, both 'key' (private key of the decrypter) and 'pkey' (public key of the encrypter) should be raw objects. |
pkey |
See 'key' |
A raw object if ascii
is FALSE
. A string if
ascii
is TRUE
.
# symmetric case: temp <- encrypt_object(1:3) all( is.raw(temp) , decrypt_object(temp) == 1:3) temp <- encrypt_object(iris, ascii = TRUE) all( is.character(temp) , decrypt_object(temp) == iris , identical(decrypt_object(temp), iris)) rm(temp) # asymmetric case: alice <- keypair() bob <- keypair() temp <- encrypt_object(1:10, alice$private_key, bob$public_key) temp2 <- decrypt_object(temp, bob$private_key, alice$public_key) identical(1:10, temp2)
# symmetric case: temp <- encrypt_object(1:3) all( is.raw(temp) , decrypt_object(temp) == 1:3) temp <- encrypt_object(iris, ascii = TRUE) all( is.character(temp) , decrypt_object(temp) == iris , identical(decrypt_object(temp), iris)) rm(temp) # asymmetric case: alice <- keypair() bob <- keypair() temp <- encrypt_object(1:10, alice$private_key, bob$public_key) temp2 <- decrypt_object(temp, bob$private_key, alice$public_key) identical(1:10, temp2)
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
)
decrypt_string(string, key = "pass", pkey = NULL, url = FALSE)
decrypt_string(string, key = "pass", pkey = NULL, url = FALSE)
string |
A string(character vector of length 1) without embedded NULL to be encrypted. or a raw vector. |
key |
For symmetric decryption, 'pkey' should be NULL (default) and 'key' can be either a string (Default is 'pass') or a raw object. For asymmetric decryption, both 'key' (private key of the decrypter) and 'pkey' (public key of the encrypter) should be raw objects. |
pkey |
See 'key' |
url |
(flag, default: FALSE) Whether the encoded string was generated url friendly. |
decrypted string
# 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" ) # string encoded as raw res <- encrypt_string("tatvamasi", ascii = FALSE) res isTRUE(identical(decrypt_string(res), "tatvamasi")) # 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)
# 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" ) # string encoded as raw res <- encrypt_string("tatvamasi", ascii = FALSE) res isTRUE(identical(decrypt_string(res), "tatvamasi")) # 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)
encrypt_file
) encrypts a file as a binary or a ascii
file. decrypt_file
) decrypts a text or a binary file (encrypted by
encrypt_file
)
encrypt_file(infile, key = "pass", pkey = NULL, ascii = FALSE, outfile)
encrypt_file(infile, key = "pass", pkey = NULL, ascii = FALSE, outfile)
infile |
file to be encrypted |
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 |
|
outfile |
Non-existant file where the encrypted output is to be written |
An invisible TRUE
# symmetric case: write.table(iris, "iris.csv") all( encrypt_file("iris.csv", outfile = "iris_encrypted.bin") , file.exists("iris_encrypted.bin") , decrypt_file("iris_encrypted.bin", outfile = "iris_2.csv") , file.exists("iris_2.csv") , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv") , unlink("iris.csv") == 0 , unlink("iris_2.csv") == 0 , unlink("iris_encrypted.bin") == 0 ) write.table(iris, "iris.csv") all( encrypt_file("iris.csv", outfile = "iris_encrypted.txt", ascii = TRUE) , file.exists("iris_encrypted.txt") , decrypt_file("iris_encrypted.txt", outfile = "iris_2.csv", ascii = TRUE) , file.exists("iris_2.csv") , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv") , unlink("iris.csv") == 0 , unlink("iris_2.csv") == 0 , unlink("iris_encrypted.txt") == 0 ) # asymmetric case: alice <- keypair() bob <- keypair() write.table(iris, "iris.csv") all( encrypt_file("iris.csv", alice$private_key, bob$public_key, outfile = "iris_encrypted.bin") , file.exists("iris_encrypted.bin") , decrypt_file("iris_encrypted.bin", bob$private_key, alice$public_key, outfile = "iris_2.csv") , file.exists("iris_2.csv") , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv") , unlink("iris.csv") == 0 , unlink("iris_2.csv") == 0 , unlink("iris_encrypted.bin") == 0 )
# symmetric case: write.table(iris, "iris.csv") all( encrypt_file("iris.csv", outfile = "iris_encrypted.bin") , file.exists("iris_encrypted.bin") , decrypt_file("iris_encrypted.bin", outfile = "iris_2.csv") , file.exists("iris_2.csv") , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv") , unlink("iris.csv") == 0 , unlink("iris_2.csv") == 0 , unlink("iris_encrypted.bin") == 0 ) write.table(iris, "iris.csv") all( encrypt_file("iris.csv", outfile = "iris_encrypted.txt", ascii = TRUE) , file.exists("iris_encrypted.txt") , decrypt_file("iris_encrypted.txt", outfile = "iris_2.csv", ascii = TRUE) , file.exists("iris_2.csv") , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv") , unlink("iris.csv") == 0 , unlink("iris_2.csv") == 0 , unlink("iris_encrypted.txt") == 0 ) # asymmetric case: alice <- keypair() bob <- keypair() write.table(iris, "iris.csv") all( encrypt_file("iris.csv", alice$private_key, bob$public_key, outfile = "iris_encrypted.bin") , file.exists("iris_encrypted.bin") , decrypt_file("iris_encrypted.bin", bob$private_key, alice$public_key, outfile = "iris_2.csv") , file.exists("iris_2.csv") , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv") , unlink("iris.csv") == 0 , unlink("iris_2.csv") == 0 , unlink("iris_encrypted.bin") == 0 )
encrypt_object
encrypts a object as a raw object or a
string and decrypt_object
decrypts a raw object or a
string(encrypted by encrypt_object
)
encrypt_object(object, key = "pass", pkey = NULL, ascii = FALSE)
encrypt_object(object, key = "pass", pkey = NULL, ascii = FALSE)
object |
Object to be encrypted |
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 |
|
A raw object if ascii
is FALSE
. A string if
ascii
is TRUE
.
# symmetric case: temp <- encrypt_object(1:3) all( is.raw(temp) , decrypt_object(temp) == 1:3) temp <- encrypt_object(iris, ascii = TRUE) all( is.character(temp) , decrypt_object(temp) == iris , identical(decrypt_object(temp), iris)) rm(temp) # asymmetric case: alice <- keypair() bob <- keypair() temp <- encrypt_object(1:10, alice$private_key, bob$public_key) temp2 <- decrypt_object(temp, bob$private_key, alice$public_key) identical(1:10, temp2)
# symmetric case: temp <- encrypt_object(1:3) all( is.raw(temp) , decrypt_object(temp) == 1:3) temp <- encrypt_object(iris, ascii = TRUE) all( is.character(temp) , decrypt_object(temp) == iris , identical(decrypt_object(temp), iris)) rm(temp) # asymmetric case: alice <- keypair() bob <- keypair() temp <- encrypt_object(1:10, alice$private_key, bob$public_key) temp2 <- decrypt_object(temp, bob$private_key, alice$public_key) identical(1:10, temp2)
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)
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" ) # string encoded as raw res <- encrypt_string("tatvamasi", ascii = FALSE) res isTRUE(identical(decrypt_string(res), "tatvamasi")) # 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)
# 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" ) # string encoded as raw res <- encrypt_string("tatvamasi", ascii = FALSE) res isTRUE(identical(decrypt_string(res), "tatvamasi")) # 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)
Using sodium's 'keygen' and 'pubkey' based on curve25519
keypair(seed = NULL)
keypair(seed = NULL)
seed |
A raw object. If NULL, a randon seed will be chosen. |
A list with:
public_key: A raw object
private_key: A raw object
seed: A raw object
temp <- keypair() str(temp)
temp <- keypair() str(temp)
save_object
encrypts a R object to raw or text connection
or a file. retrieve_object
decrypts a raw or a text connection or a
file (encrypted by save_object
). Note that retrieve_object
returns the object.
retrieve_object(conn, key = "pass", pkey = NULL, ascii = FALSE)
retrieve_object(conn, key = "pass", pkey = NULL, ascii = FALSE)
conn |
A connection or a file where the decrypted content is written. If
|
key |
For symmetric decryption, 'pkey' should be NULL (default) and 'key' can be either a string (Default is 'pass') or a raw object. For asymmetric decryption, both 'key' (private key of the decrypter) and 'pkey' (public key of the encrypter) should be raw objects. |
pkey |
See 'key' |
ascii |
TRUE, if the encrypted output is a string(written to the text connection). FALSE, if the encrypted output is a raw object(written to the raw connection) |
An invisible TRUE
# symmetric case: all( save_object(iris, conn = "iris_safer.bin") , identical(retrieve_object(conn = "iris_safer.bin"), iris) , unlink("iris_safer.bin") == 0 ) all( save_object(iris, conn = "iris_safer_2.txt", ascii = TRUE) , identical(retrieve_object(conn = "iris_safer_2.txt", ascii = TRUE), iris) , unlink("iris_safer_2.txt") == 0 ) # asymmetric case: alice <- keypair() bob <- keypair() all( save_object(iris, alice$private_key, bob$public_key, conn = "iris_safer.bin") , identical(retrieve_object(conn = "iris_safer.bin", bob$private_key, alice$public_key), iris) , unlink("iris_safer.bin") == 0 )
# symmetric case: all( save_object(iris, conn = "iris_safer.bin") , identical(retrieve_object(conn = "iris_safer.bin"), iris) , unlink("iris_safer.bin") == 0 ) all( save_object(iris, conn = "iris_safer_2.txt", ascii = TRUE) , identical(retrieve_object(conn = "iris_safer_2.txt", ascii = TRUE), iris) , unlink("iris_safer_2.txt") == 0 ) # asymmetric case: alice <- keypair() bob <- keypair() all( save_object(iris, alice$private_key, bob$public_key, conn = "iris_safer.bin") , identical(retrieve_object(conn = "iris_safer.bin", bob$private_key, alice$public_key), iris) , unlink("iris_safer.bin") == 0 )
save_object
encrypts a R object to raw or text connection
or a file. retrieve_object
decrypts a raw or a text connection or a
file (encrypted by save_object
). Note that retrieve_object
returns the object.
save_object(object, key = "pass", pkey = NULL, ascii = FALSE, conn)
save_object(object, key = "pass", pkey = NULL, ascii = FALSE, conn)
object |
A R object to be encrypted |
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 |
TRUE, if the encrypted output is a string(written to the text connection). FALSE, if the encrypted output is a raw object(written to the raw connection) |
conn |
A connection or a file where the encrypted content is written. If
|
An invisible TRUE
# symmetric case: all( save_object(iris, conn = "iris_safer.bin") , identical(retrieve_object(conn = "iris_safer.bin"), iris) , unlink("iris_safer.bin") == 0 ) all( save_object(iris, conn = "iris_safer_2.txt", ascii = TRUE) , identical(retrieve_object(conn = "iris_safer_2.txt", ascii = TRUE), iris) , unlink("iris_safer_2.txt") == 0 ) # asymmetric case: alice <- keypair() bob <- keypair() all( save_object(iris, alice$private_key, bob$public_key, conn = "iris_safer.bin") , identical(retrieve_object(conn = "iris_safer.bin", bob$private_key, alice$public_key), iris) , unlink("iris_safer.bin") == 0 )
# symmetric case: all( save_object(iris, conn = "iris_safer.bin") , identical(retrieve_object(conn = "iris_safer.bin"), iris) , unlink("iris_safer.bin") == 0 ) all( save_object(iris, conn = "iris_safer_2.txt", ascii = TRUE) , identical(retrieve_object(conn = "iris_safer_2.txt", ascii = TRUE), iris) , unlink("iris_safer_2.txt") == 0 ) # asymmetric case: alice <- keypair() bob <- keypair() all( save_object(iris, alice$private_key, bob$public_key, conn = "iris_safer.bin") , identical(retrieve_object(conn = "iris_safer.bin", bob$private_key, alice$public_key), iris) , unlink("iris_safer.bin") == 0 )