SHARE
TWEET

AES CBC

mjaniec May 3rd, 2014 224 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. ### Encrypt/Decrypt a file using AES-256 CBC
  3. # 2014-05-03 - CBC replaced EBC
  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. rm(list=ls()) # clear everything
  17.  
  18. key             <- "***"
  19.  
  20. input.file      <- "***"
  21. encrypted.file  <- "***"
  22. decrypted.file  <- "***"
  23.  
  24. encrypt         <- FALSE
  25.  
  26. ###
  27.  
  28. library(digest)
  29. library(fBasics) # hex to dec conversion
  30.  
  31. ### initialize AES
  32.  
  33. hash <- digest(key, algo="sha256", raw=TRUE)
  34.  
  35. iv   <- sample(0:255, 16, replace=TRUE) # CBC initialization vector
  36.  
  37. ###
  38.  
  39. ReadClrFile <- function(file.name) {
  40.  
  41.   if (file.exists(file.name)) {
  42.  
  43.     file.size    <- file.info(file.name)$size
  44.    
  45.     if (file.size>2147483647) error("ERROR: too large file.") # .Machine$integer.max
  46.    
  47.     file.obj     <- file(file.name,"rb")
  48.    
  49.     file.content <- readBin(con=file.obj,what=raw(),n=file.size)
  50.    
  51.     close(file.obj)
  52.  
  53.   } else stop(paste("Input file",toupper(file.name),"not found."))
  54.  
  55.   return(file.content)
  56.  
  57. }
  58.  
  59. ReadEncFile <- function(file.name) {
  60.  
  61.   if (file.exists(file.name)) {
  62.  
  63.     file.size    <- file.info(file.name)$size
  64.    
  65.     file.obj     <- file(file.name,"rb")
  66.    
  67.     file.content <- readBin(con=file.obj,what=raw(),n=file.size)
  68.    
  69.     close(file.obj)
  70.  
  71.   } else stop(paste("Encrypted file",toupper(file.name),"not found."))
  72.  
  73.   return( file.content )
  74.  
  75. }
  76.  
  77.  
  78. SaveFile <- function(x,file.name) {
  79.  
  80.   file.obj     <- file(file.name,"wb")
  81.  
  82.   writeBin(x,con=file.obj,size=1)
  83.  
  84.   close(file.obj)
  85.  
  86. }
  87.  
  88. pad0 <- function(x,n=17) {
  89.  
  90.   l <- nchar(x)
  91.  
  92.   if (l<n) x <- paste(x,paste(rep("0",n-l),collapse=""),sep="")
  93.  
  94.   return(x)
  95.  
  96. }
  97.  
  98. AddGarbage16  <- function(x) {
  99.  
  100.   l <- length(x)
  101.  
  102.   if (l %% 16 !=0) {
  103.    
  104.     g  <- 16-(l %% 16)
  105.    
  106.     xg <- sapply(round(runif(g,0,255)), function(x) packBits(intToBits(x))[1])
  107.    
  108.     return( c(x,xg) )
  109.    
  110.   }
  111.  
  112.   return( x )
  113.  
  114. }
  115.  
  116. EncryptFile <- function(x, hash, iv) {
  117.  
  118.   xc  <- memCompress(x,type="gzip")
  119.  
  120.   l   <- length(xc)
  121.  
  122.   h   <- charToRaw(pad0(.dec.to.hex(l)))
  123.  
  124.   xg  <- AddGarbage16(xc)
  125.  
  126.   aes <- AES(hash, mode="CBC", IV=iv)
  127.  
  128.   xe  <- aes$encrypt(xg)
  129.  
  130.   return( c(h,iv,xe) )
  131.  
  132. }
  133.  
  134. DecryptFile <- function(x, hash) {
  135.  
  136.   fs    <- ExtractFileSize(x)
  137.  
  138.   xc    <- tail(x,-17)  # exclude file size
  139.  
  140.   iv    <- head(xc,16)  # extract iv
  141.  
  142.   xc    <- tail(xc,-16) # exclude iv
  143.  
  144.   aes   <- AES(hash, mode="CBC",IV=iv)
  145.    
  146.   xc    <- head(aes$decrypt(xc,raw=TRUE),fs)
  147.  
  148.   x.clr <- memDecompress(xc,type="gzip")
  149.  
  150.   x.clr
  151.  
  152. }
  153.  
  154. # 2014-02-15: modified
  155. ExtractFileSize <- function(x) {
  156.     h0 <- head(x,17)
  157.    
  158.     # last character other than 30 ('0')
  159.     idx <- tail(which(h0!=as.raw(0x30)),1)
  160.  
  161.     .hex.to.dec(rawToChar(h0[1:idx]))
  162. }
  163.  
  164. ###
  165.  
  166. if (encrypt) {
  167.  
  168.   x.clr  <- ReadClrFile(input.file)
  169.  
  170.   x.enc  <- EncryptFile(x.clr, hash=hash, iv=iv)
  171.  
  172.   SaveFile(x.enc,encrypted.file)
  173.  
  174. } else {
  175.  
  176.   x.enc <- ReadEncFile(encrypted.file)
  177.  
  178.   x.clr <- DecryptFile(x.enc, hash=hash)
  179.  
  180.   # tmp   <- ReadClrFile(input.file)  # crude verification which work only when input is present
  181.   # print(all.equal(x.clr,tmp))
  182.  
  183.   SaveFile(x.clr,decrypted.file)
  184.  
  185. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top