Following up on @zach comments, this procedure would not be safe in case of an attack. This is uniquely useful is you want to obfuscate a string (e.g. from someone passing by and looking at the screen), security is not implied in this question.
This version of my the functions I reported in the previous answer also accepts strings with numbers and letters:
obfuscate <- function(password, string){
set.seed(password)
vec <- substring(string, seq(nchar(string)), seq(nchar(string)))
encr <- y <- vector("list",length(vec))
letters <- rep(letters,2) # prolongue letters sequence
for (i in 1:length(vec)){
ifelse(is.numeric(vec[i]), # extracting encryption numbers
encr[[i]] <- round(runif(1, 0, 9)), # if numeric
encr[[i]] <- round(runif(1, 0, 26))) # if letters
ifelse( !is.na(as.numeric(vec[i])) , # encrypting
y[[i]] <- as.numeric(vec[i]) + encr[[i]], # if numeric
y[[i]] <- letters[which(letters==vec[i])[1] + encr[[i]]] ) # if letters
}
return(unlist(y))
}
reveal <- function(password, y){
set.seed(password)
for (i in 1:length(y)){
ifelse(is.numeric(y[i]), # extracting encryption numbers
encr[[i]] <- round(runif(1, 0, 9)), # if numeric
encr[[i]] <- round(runif(1, 0, 26))) # if letters
ifelse( !is.na(as.numeric(y[i])) , # encrypting
y[[i]] <- as.numeric(y[i]) - encr[[i]], # if numeric
y[[i]] <- letters[which(letters==y[i])[1] - encr[[i]]] ) # if letters
}
return(paste0(unlist(y), collapse=""))
}
Here is an example:
> obfuscate(2016,"a6b8")
[1] "f" "10" "x" "11"
Warning messages:
1: In ifelse(!is.na(as.numeric(vec[i])), y[[i]] <- as.numeric(vec[i]) + :
NAs introduced by coercion
2: In ifelse(!is.na(as.numeric(vec[i])), y[[i]] <- as.numeric(vec[i]) + :
NAs introduced by coercion
Note that the number of warnings is equal to the number of non-numeric characters. This could be suppressed with suppressWarnings()
as explained in this SO question. My preference is for always expressing R warnings.
> reveal(2016,c("f","10","x","11"))
[1] "a6b8"
Warning messages:
1: In ifelse(!is.na(as.numeric(y[i])), y[[i]] <- as.numeric(y[i]) - :
NAs introduced by coercion
2: In ifelse(!is.na(as.numeric(y[i])), y[[i]] <- as.numeric(y[i]) - :
NAs introduced by coercion