[R] Encrypt/decrypt in R

Hadley Wickham hadley at rice.edu
Fri Mar 19 19:24:20 CET 2010


On Fri, Mar 19, 2010 at 12:35 PM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> On Fri, Mar 19, 2010 at 5:10 PM, Barry Rowlingson
> <b.rowlingson at lancaster.ac.uk> wrote:
>> On Fri, Mar 19, 2010 at 5:00 PM, Hadley Wickham <hadley at rice.edu> wrote:
>>> Hi all,
>>>
>>> Does any one know of any encryption/decryption algorithms in R?  I'm
>>> not looking for anything robust - I want some way of printing output
>>> to the screen that the user can't read immediately, but can decrypt a
>>> little later.  The main thing I don't want to the user to see is a
>>> number, so (e.g.) ROT13 isn't appropriate.
>>
>>  You could just include the numbers in a ROT13-sort of algorithm. It
>> would end up being ROT-18 I guess...
>
>  "ROT-18" (?) using chartr:
>
>  > oldletters=c(letters,0:9)
>  > old=paste(oldletters,collapse="")
>  > new=paste(c(oldletters[19:36],oldletters[1:18]),collapse="")
>  > chartr(old,new,message)
>  [1] "g6c zsdw e65 kl.qr v633s9a"
>  > chartr(old,new,chartr(old,new,message))
>  [1] "you have won 23.89 dollars"


Thanks all for the suggestions.  I ended up going with:

.old <- c(LETTERS, letters, 0:9, " ")
.new <- sample(.old)

.old_string <- paste(.old, collapse = "")
.new_string <- paste(.new, collapse = "")

encrypt <- function(message) {
  chartr(.old_string, .new_string, message)
}

decrypt <- function(message) {
  chartr(.new_string, .old_string, message)
}

Hadley


-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list