Hide your password in R
There is several way to hide password in R. I recommend you to see this video if you want to have a more accurate picture on how to do it using fully encrypted password. There is specific libraries available in R which deal with encryption like sodium and also using XOR operator and others.
In my work I had to deal with a fix password like for example "password1234" that the administrator gave me and to enter it in my code when I need to login to the SQL database. In my opinion there is huge security issue using password like this. You can store the code name encrypted on your computer, however, when login, you will need to decrypt it and use the plain text format of your password to have access to the database or anything else. Anyway, I found how to encrypt my plain text password using the library PKI which used to encrypt and decrypt word.
For this tutorial you will need to generate a private key and a public key. The private key will generate a random vector which will be use to decrypt the password. You will need to carefully save it on your computer, otherwise you will cannot decrypt your password. The public key will be use to encrypt the password. Thus, you will use the
library(PKI)
#to be erase after first execution
# generate 2048-bit RSA key
key <- PKI.genRSAkey(bits = 2048L)
##
## # extract private and public parts as PEM
priv.key <- PKI.save.key(key)
pub.key <- PKI.save.key(key, private=FALSE)
## # load back the public key separately
pub.k <- PKI.load.key(pub.key)
##
## # encrypt with the public key
find_me <- PKI.encrypt(charToRaw("password1234"), pub.k) #to remove after saving it in a separate file
find_me
Le chargement a nécessité le package : base64enc [1] 73 67 c7 c3 45 9b d4 f2 60 41 23 a0 f6 05 77 43 d2 6f 1c 6f 5f 3d 9b 0e 17 [26] dd 0a 0d fe 0c 3e 51 6e 64 8b e0 c6 fa da 47 cd af 6e 7f 38 c5 22 d5 bb 6a [51] a5 c8 a9 80 5f b9 5e b4 09 1d 52 e2 b5 41 f6 07 b1 bb 01 29 59 91 7a c7 80 [76] 00 62 4b 4a 48 5a d3 41 48 80 71 3e a8 ab 93 03 6f 20 3a 0a 5f bc bc 56 b5 [101] eb 14 cb b1 ad 41 10 83 81 53 61 41 0f ee a5 bf 51 f9 a9 f3 a5 f5 79 6a dc [126] 2d b3 05 29 55 79 03 e7 bb f0 b1 28 98 1c 3e 5e f5 c7 d8 f7 42 b8 bb f3 97 [151] 0b 17 96 98 66 f1 ac 20 12 e5 c3 df 47 13 eb 08 85 17 bc e8 ee db 1c b8 47 [176] fe 88 55 df 42 79 4c 61 aa 65 14 46 0d fb dd 5b 73 ad ce 64 43 9b 49 86 8a [201] 82 bc c2 02 99 e1 72 09 c5 48 3e 3d d8 3c 02 db b8 52 04 7f 7d c4 aa b6 44 [226] c3 c9 93 51 05 0d 93 d4 75 df ec 36 70 9e 40 61 14 d7 13 e6 81 32 55 7d 7b [251] 0b 64 f3 9d 63 32
This is the result of the encryption of password1234
using the public
key.
Then we will need to store this encryption in a file on the computer AND also the private key, otherwise you cannot decrypt the password.
## # decrypt with private key
##
save( priv.key, file = "priv_key") #save private key in a separate file
## # save( pub.key, file = "pub_key") # no need to save public key
save(find_me, file = "find_me") #save encryption
##
## #remove object from the R history
rm( priv.key)
rm( pub.key)
rm(find_me)
## #erase above code after first execution
And here starting a fresh session of R to decrypt your password:
#when starting a new session use:
library(PKI)
load("priv_key") #load private key. Cannot be load in the PKI.decrypt function
load("find_me") #load the password to decrypt
rawToChar(PKI.decrypt(find_me, PKI.load.key(priv.key)))#print password - erase this line after making sure this is working
password1234
Here you had your decrypt password.