how do I encrypt data in R?

mariotomo picture mariotomo · Apr 27, 2011 · Viewed 8.4k times · Source

I am adding a sentry handler to the R logging package. currently django-sentry validates the client (the application sending log records) based on a shared key which travels in clear text in a secured channel.

I was wondering if it was possible to do encryption from R, preferably asymmetric, public key based, but I find nothing relevant.

all right, not "if it was possible", but "how to do that" and whether someone already did this.


after interaction with the author of django-sentry, we opted for hmac, which I have implemented within the digest R package (version 0.5+ contains it). This does not answer the question "how to encrypt data using R", but it solves the problem which formed the base for my initial question.

At this point I am not any more actively working at asymmetric encription in R, however, if you are interested in it and you want to contribute ideas or code, please leave a note here!

Answer

user2030503 picture user2030503 · Sep 14, 2013

You may also find suitable function in the PKI package from the author Simon Urbanek.

This package provides PKI functions such as verifyig certificates, RSA encription and signing which can be used to build PKI infrastructure and perform cryptographic tasks.

Example code from the tutorial:

require(PKI)
key <- PKI.genRSAkey(2048)
x <- charToRaw("Hello, world!")
e <- PKI.encrypt(x, key)
y <- PKI.decrypt(e, key)
stopifnot(identical(x, y))
print(rawToChar(y))

gives as result: [1] "Hello, world!"

while the encrypted message e was:

  [1] 36 83 d3 70 0a 67 b5 05 a6 40 1e 37 28 b9 4e 28 f1 31 92 14 2c 35 c8 8a 61 93 1e 04 62 01 da 3b 2b a0 75 1c 10 58 26
 [40] e4 77 da 7a 47 3f 4e 44 29 8e 97 6f 62 b1 98 44 ba 18 ef 57 1e 9e 9c 27 a8 6e 9c 7b c7 8b c0 c3 a3 00 e2 67 98 8b 6e
 [79] 1a 93 c6 d6 ed 4b 54 e5 7a 07 d7 06 ef a6 bb 36 6a 7f 57 06 b9 15 03 f6 51 3f 07 48 cb f4 2d 25 15 be 42 de f4 8a 06
[118] 72 89 b1 e3 04 d3 ec 80 99 f0 66 0f 84 e1 b5 af 23 24 a1 36 8e 62 65 ae 19 fb 77 d1 36 06 ae 71 95 ee 57 aa 68 5a 6b
[157] 4e 28 ba a2 0d 17 78 11 6c 7f 1b b3 ce 31 65 a9 d3 71 89 76 f9 19 a0 7a bf 02 dd c9 1f cb 9c 39 25 d4 48 a2 23 83 26
[196] b4 a9 b1 40 f5 1d 46 21 35 12 52 73 09 9b f3 52 e1 9e 0d 2a 9b ff 70 81 41 24 49 ed 58 b2 61 dc 3e c9 b3 b2 b1 37 e0
[235] 48 76 18 bf b0 e5 c2 d9 2b 92 2f 6b 49 dd e0 93 b7 10 f8 ba d2 8a