Re: [R] Encrypt/decrypt in R

2010-03-20 Thread Carl Witthoft

 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.

Nobody seems to have asked what you mean by don't want the user to see 
a number.
Do you mean that there should be no numbers in the output (impossible 
unless you stir in ten non-alpha characters), or simply that numbers 
should be translated (at least some of the time) into alpha?



Anyway,  here's a toy I wrote for my own amusement back when learning R:


# rotrw -- a func to read from a file, rot, and write to
# a new file
# file: name of source file
# rot:  number to rot by
# field: ascii or alpha, selects what characters to rot thru
#If select alpha, only alphas are rotted.
#If select ascii, all ascii chars are rotted

rotrw-function(file,rot=13,field=ascii)
{
readLines(file)-fin
if(field=='alpha')
{
rot13(fin,rot)-tempf
}
else if(field=='ascii')
{
rotit(fin,rot)-tempf
}
else
{
stop(Error: ,field,' is unknown type.\n')
}
#   strip the .extension so can get  file name and build
#   output name 'file'[rot].ext
strsplit(file,split='\\.')-splitfil# to get file name alone
unlist(splitfil)-unfil # makes it a char vector,
# so unlist[1] is name, unlist[2] is ext
paste(unfil[1],rot,field,'.',unfil[2],sep=)-outname
writeLines(tempf,con=outname)
}

rot13 - function(string,rot=13){
  cstr-as.character(string)
  #get fancy: force ROT into 0-25, by shifting negative values and
  # taking a modulus:
  rtm-(rot+(rot0)*26)%%26
  old-paste(c(letters,LETTERS),collapse=)
  shift-c(rtm+1,26,1,rtm)
   	 
new-paste(c(letters[shift[1]:shift[2]],letters[shift[3]:shift[4]],LETTERS[shift[1]:shift[2]],LETTERS[shift[3]:shift[4]]),collapse=)

  rotted-chartr(old = old, new = new, x = string)
  return(rotted)
}

rotit - function(string,rot=13){
library(sfsmisc) #needed for chars8bit()
  cstr-as.character(string)
  #get fancy: force ROT into 0-95, by shifting negative values and
  # taking a modulus:
  rtm-(rot+(rot0)*95)%%95
  # OLD is full baseline ASCII char set. Note that it's a vector
  old-c(chars8bit(seq(32,126)))
  shift-c(rtm+1,95,1,rtm)
  new-paste(c(old[shift[1]:shift[2]],old[shift[3]:shift[4]]),collapse=)
  rotted-chartr(old = paste(old,collapse=), new = new, x = string)
  return(rotted)
}

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Encrypt/decrypt in R

2010-03-19 Thread Hadley Wickham
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.

Hadley

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

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Encrypt/decrypt in R

2010-03-19 Thread Barry Rowlingson
On Fri, Mar 19, 2010 at 5:00 PM, Hadley Wickham had...@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...

 Would a single permutation of c(letters,0:9) be sufficient? Or you
could add other characters. And what about non-ascii encodings?
Hmmm...

 a bit of strsplit, match, paste magic should do it...

Barry

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Encrypt/decrypt in R

2010-03-19 Thread Gabor Grothendieck
chartr?

On Fri, Mar 19, 2010 at 1:00 PM, Hadley Wickham had...@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.

 Hadley

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

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Encrypt/decrypt in R

2010-03-19 Thread Marc Schwartz
On Mar 19, 2010, at 12:00 PM, Hadley Wickham 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.
 
 Hadley

I don't know of anything offhand in R and anything beyond very simple stuff 
would be rather slow in an interpreted language. You can always create 
character translations and such using functions such as chartr().

There is GnuPG (http://www.gnupg.org), which provides a broad range of 
algorithms via a CLI interface, which can be called via system().

HTH,

Marc Schwartz

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Encrypt/decrypt in R

2010-03-19 Thread Henrique Dallazuanna
Perhaps, If is there a connection with mysql:

library(RMySQL)
#conn - dbConnect(...)
encStr - dbGetQuery(conn, SELECT AES_ENCRYPT('teste', 'password'))
dbGetQuery(conn, sprintf(SELECT AES_DECRYPT('%s', 'password'), encStr))


On Fri, Mar 19, 2010 at 2:00 PM, Hadley Wickham had...@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.

 Hadley

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

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Encrypt/decrypt in R

2010-03-19 Thread Barry Rowlingson
On Fri, Mar 19, 2010 at 5:10 PM, Barry Rowlingson
b.rowling...@lancaster.ac.uk wrote:
 On Fri, Mar 19, 2010 at 5:00 PM, Hadley Wickham had...@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

Barry

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Encrypt/decrypt in R

2010-03-19 Thread Hadley Wickham
On Fri, Mar 19, 2010 at 12:35 PM, Barry Rowlingson
b.rowling...@lancaster.ac.uk wrote:
 On Fri, Mar 19, 2010 at 5:10 PM, Barry Rowlingson
 b.rowling...@lancaster.ac.uk wrote:
 On Fri, Mar 19, 2010 at 5:00 PM, Hadley Wickham had...@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/

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.