[R] help with encrypting a text string in a data.frame

gavinr g.rudge at bham.ac.uk
Sun Feb 23 19:53:48 CET 2014


I am trying to encrypt postcodes (zipcodes) which, in my datasets, are
strings with 8 characters, including spaces.  By referring to other help
files and other R resources I've built some (clumsy looking) code that turns
a postcode into an output I'm happy with.  See below. 

library("PKI")
#make a character string of the post code in 8 character format
x<-"GG22 1WD"
#get it into a string of numbers 
y<-raw2hex((charToRaw(x)),sep="")
#make a 16 digit pin for the key, in this case with a simple set of
sequential numbers
pin<-as.raw(1:16)
#make a encryption key
mykey<-AES(pin,mode="ECB")
#encrypt the postcode
z<-mykey$encrypt(y)
#finally convert this to a manageable string
pccrypt<-raw2hex(z,sep="")
#summary of the outputs
x
y
z
pccrypt

Now, I’ve got a couple of questions about this

Firstly and most importantly if I have a data.frame where one of my columns
contains these postcodes (e.g. df below) how can I run this workflow (or
something slicker, that does the same thing) and put the result into a
column in the data.frame? I’ve tried making a function and I’ve tried
sapply, but been unable to do achieve this.

Secondly there seem to be a lot of steps to get the text string into a
format that the AES command can use, is there a way to make this simpler?

#make a simple data.frame with some sample codes
postcode<-c("AB5  2TG","B1   8AA","TG22 4XB")
df<-as.data.frame(postcode)
df

Any help gratefully received.

GavinR



--
View this message in context: http://r.789695.n4.nabble.com/help-with-encrypting-a-text-string-in-a-data-frame-tp4685716.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list