
Hide text from prying eyes (example on Scala)
After reading this post , I had an idea, but is it possible to make a certain text completely invisible? The method described below does not claim to be effective in terms of speed or data volume.
We turn each byte of the string into a three-digit octal number and replace each digit with one of the invisible characters.
» Link to GitHub
Let's analyze this code in parts.
val v is an array containing most (except U + 180E) invisible Unicode characters.
def str2oct - a function that turns an array of bytes (for example, "hello" .getBytes () ) into its octal display, for example 150 145 154 154 157 (spaces are inserted for clarity).
def oct2str is the function that performs the inverse transform. We break the string into pieces of 3 characters, turn the triplets into the corresponding character and collect the string back.
def voidEnc - replaces an octal number (for example, 7) with the corresponding character from our array of invisible characters.
def voidDec - Returns a number corresponding to an invisible character.
def char2void - first we turn the string into octal triplets ( str2oct ), and then replace each number with an invisible character ( voidEnc )
def void2char - first decrypt octal triplets, and then turn them into a string.
PS I do not recommend publishing War and Peace or any other long, encrypted message in the comments, because it can break the browser for many users.
Briefly
We turn each byte of the string into a three-digit octal number and replace each digit with one of the invisible characters.
Implementation on Scala
» Link to GitHub
object Main extends App {
val v = Array("\u2060", "\u200B", "\u2061", "\u2062", "\u2063", "\uFEFF", "\u200C", "\u200D")
def str2oct(buf: Array[Byte]): String = buf.map("%03o" format _).mkString
def oct2str(string: String): String = new String(string.sliding(3,3).toArray.map(x => BigInt(x,8).toByte))
def voidEnc(char: String):String = v(char.toInt)
def voidDec(char:String):String = v.indexOf(char).toString
def char2void(string: String): String = str2oct(string.getBytes()).map(x=>voidEnc(x.toString)).mkString
def void2char(string: String):String = oct2str(string.map(x=>voidDec(x.toString)).mkString)
val void = char2void("Hello! Привет!")
println(void)
val text = void2char(void)
println(text)
}
Let's analyze this code in parts.
val v is an array containing most (except U + 180E) invisible Unicode characters.
def str2oct - a function that turns an array of bytes (for example, "hello" .getBytes () ) into its octal display, for example 150 145 154 154 157 (spaces are inserted for clarity).
def oct2str is the function that performs the inverse transform. We break the string into pieces of 3 characters, turn the triplets into the corresponding character and collect the string back.
def voidEnc - replaces an octal number (for example, 7) with the corresponding character from our array of invisible characters.
def voidDec - Returns a number corresponding to an invisible character.
def char2void - first we turn the string into octal triplets ( str2oct ), and then replace each number with an invisible character ( voidEnc )
def void2char - first decrypt octal triplets, and then turn them into a string.
PS I do not recommend publishing War and Peace or any other long, encrypted message in the comments, because it can break the browser for many users.