Advertisement
mjaniec

Encrypting files with AES in R

Nov 29th, 2013
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.92 KB | None | 0 0
  1. # visit http://reakkt.com for more information
  2.  
  3. ### Encrypt/Decrypt a file using AES-256 ECB
  4.  
  5. # encryption key is SHA256 from the keyphrase
  6. # before encryption file is compressed in-memory
  7.  
  8. ### script parameters:
  9.  
  10. # key             - keyphrase
  11. # input.file      - file to encrypt
  12. # encrypted.file  - name of the encrypted output file
  13. # decrypted.file  - name of the decrypted output file
  14. # encrypt         - flag: encrypt (TRUE) or decrypt (FALSE)
  15.  
  16. key             <- "ala ma kota"
  17.  
  18. input.file      <- "enc.R"
  19. encrypted.file  <- "test.enc"
  20. decrypted.file  <- "test.R"
  21.  
  22. encrypt         <- FALSE
  23.  
  24. ###
  25.  
  26. library(digest)
  27. library(fBasics) # hex to dec conversion
  28.  
  29. ### initialize AES
  30.  
  31. h   <- digest(key, algo="sha256", raw=TRUE)
  32.  
  33. aes <- AES(h, mode="ECB")
  34.  
  35. ###
  36.  
  37. ReadClrFile <- function(file.name) {
  38.  
  39.   file.size    <- file.info(file.name)$size
  40.  
  41.   if (file.size>2147483647) error("ERROR: too large file.") # .Machine$integer.max
  42.  
  43.   file.obj     <- file(file.name,"rb")
  44.  
  45.   file.content <- readBin(con=file.obj,what=raw(),n=file.size)
  46.  
  47.   close(file.obj)
  48.  
  49.   return(file.content)
  50.  
  51. }
  52.  
  53. ReadEncFile <- function(file.name) {
  54.  
  55.   file.size    <- file.info(file.name)$size
  56.  
  57.   file.obj     <- file(file.name,"rb")
  58.  
  59.   file.content <- readBin(con=file.obj,what=raw(),n=file.size)
  60.  
  61.   close(file.obj)
  62.  
  63.   return( file.content )
  64.  
  65. }
  66.  
  67.  
  68. SaveFile <- function(x,file.name) {
  69.  
  70.   file.obj     <- file(file.name,"wb")
  71.  
  72.   writeBin(x,con=file.obj)
  73.  
  74.   close(file.obj)
  75.  
  76. }
  77.  
  78. pad0 <- function(x,n=17) {
  79.  
  80.   l <- nchar(x)
  81.  
  82.   if (l<n) x <- paste(x,paste(rep("0",n-l),collapse=""),sep="")
  83.  
  84.   return(x)
  85.  
  86. }
  87.  
  88. AddGarbage16  <- function(x) {
  89.  
  90.   l <- length(x)
  91.  
  92.   if (l %% 16 !=0) {
  93.    
  94.     g  <- 16-(l %% 16)
  95.    
  96.     xg <- sapply(round(runif(g,0,255)), function(x) packBits(intToBits(x))[1])
  97.    
  98.     return( c(x,xg) )
  99.    
  100.   }
  101.  
  102.   return( x )
  103.  
  104. }
  105.  
  106. EncryptFile <- function(x) {
  107.  
  108.   xc <- memCompress(x,type="gzip")
  109.  
  110.   l  <- length(xc)
  111.  
  112.   h  <- charToRaw(pad0(.dec.to.hex(l)))
  113.  
  114.   xg <- AddGarbage16(xc)
  115.  
  116.   xe <- aes$encrypt(xg)
  117.  
  118.   return( c(h,xe) )
  119.  
  120. }
  121.  
  122. DecryptFile <- function(x) {
  123.  
  124.   fs    <- ExtractFileSize(x)
  125.  
  126.   xc    <- tail(x,-17)
  127.    
  128.   xc    <- head(aes$decrypt(xc,raw=TRUE),fs)
  129.  
  130.   x.clr <- memDecompress(xc,type="gzip")
  131.  
  132.   x.clr
  133.  
  134. }
  135.  
  136. # 2014-02-15: modified
  137. ExtractFileSize <- function(x) {
  138.     h0 <- head(x,17)
  139.    
  140.     # last character other than 30 ('0')
  141.     idx <- tail(which(h0!=as.raw(0x30)),1)
  142.  
  143.     .hex.to.dec(rawToChar(h0[1:idx]))
  144. }
  145.  
  146. ###
  147.  
  148. if (encrypt) {
  149.  
  150.   x.clr  <- ReadClrFile(input.file)
  151.  
  152.   x.enc  <- EncryptFile(x.clr)
  153.  
  154.   SaveFile(x.enc,encrypted.file)
  155.  
  156. } else {
  157.  
  158.   x.enc <- ReadEncFile(encrypted.file)
  159.  
  160.   x.clr <- DecryptFile(x.enc)
  161.  
  162.   tmp   <- ReadClrFile(input.file)  
  163.   print(all.equal(x.clr,tmp))
  164.  
  165.   SaveFile(x.clr,decrypted.file)
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement