Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Encrypt/Decrypt a file using AES-256 CBC
- # 2014-05-03 - CBC replaced EBC
- # encryption key is SHA256 from the keyphrase
- # before encryption file is compressed in-memory
- ### script parameters:
- # key - keyphrase
- # input.file - file to encrypt
- # encrypted.file - name of the encrypted output file
- # decrypted.file - name of the decrypted output file
- # encrypt - flag: encrypt (TRUE) or decrypt (FALSE)
- rm(list=ls()) # clear everything
- key <- "***"
- input.file <- "***"
- encrypted.file <- "***"
- decrypted.file <- "***"
- encrypt <- FALSE
- ###
- library(digest)
- library(fBasics) # hex to dec conversion
- ### initialize AES
- hash <- digest(key, algo="sha256", raw=TRUE)
- iv <- sample(0:255, 16, replace=TRUE) # CBC initialization vector
- ###
- ReadClrFile <- function(file.name) {
- if (file.exists(file.name)) {
- file.size <- file.info(file.name)$size
- if (file.size>2147483647) error("ERROR: too large file.") # .Machine$integer.max
- file.obj <- file(file.name,"rb")
- file.content <- readBin(con=file.obj,what=raw(),n=file.size)
- close(file.obj)
- } else stop(paste("Input file",toupper(file.name),"not found."))
- return(file.content)
- }
- ReadEncFile <- function(file.name) {
- if (file.exists(file.name)) {
- file.size <- file.info(file.name)$size
- file.obj <- file(file.name,"rb")
- file.content <- readBin(con=file.obj,what=raw(),n=file.size)
- close(file.obj)
- } else stop(paste("Encrypted file",toupper(file.name),"not found."))
- return( file.content )
- }
- SaveFile <- function(x,file.name) {
- file.obj <- file(file.name,"wb")
- writeBin(x,con=file.obj,size=1)
- close(file.obj)
- }
- pad0 <- function(x,n=17) {
- l <- nchar(x)
- if (l<n) x <- paste(x,paste(rep("0",n-l),collapse=""),sep="")
- return(x)
- }
- AddGarbage16 <- function(x) {
- l <- length(x)
- if (l %% 16 !=0) {
- g <- 16-(l %% 16)
- xg <- sapply(round(runif(g,0,255)), function(x) packBits(intToBits(x))[1])
- return( c(x,xg) )
- }
- return( x )
- }
- EncryptFile <- function(x, hash, iv) {
- xc <- memCompress(x,type="gzip")
- l <- length(xc)
- h <- charToRaw(pad0(.dec.to.hex(l)))
- xg <- AddGarbage16(xc)
- aes <- AES(hash, mode="CBC", IV=iv)
- xe <- aes$encrypt(xg)
- return( c(h,iv,xe) )
- }
- DecryptFile <- function(x, hash) {
- fs <- ExtractFileSize(x)
- xc <- tail(x,-17) # exclude file size
- iv <- head(xc,16) # extract iv
- xc <- tail(xc,-16) # exclude iv
- aes <- AES(hash, mode="CBC",IV=iv)
- xc <- head(aes$decrypt(xc,raw=TRUE),fs)
- x.clr <- memDecompress(xc,type="gzip")
- x.clr
- }
- # 2014-02-15: modified
- ExtractFileSize <- function(x) {
- h0 <- head(x,17)
- # last character other than 30 ('0')
- idx <- tail(which(h0!=as.raw(0x30)),1)
- .hex.to.dec(rawToChar(h0[1:idx]))
- }
- ###
- if (encrypt) {
- x.clr <- ReadClrFile(input.file)
- x.enc <- EncryptFile(x.clr, hash=hash, iv=iv)
- SaveFile(x.enc,encrypted.file)
- } else {
- x.enc <- ReadEncFile(encrypted.file)
- x.clr <- DecryptFile(x.enc, hash=hash)
- # tmp <- ReadClrFile(input.file) # crude verification which work only when input is present
- # print(all.equal(x.clr,tmp))
- SaveFile(x.clr,decrypted.file)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement