> 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+(rot<0)*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+(rot<0)*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.

Reply via email to