implemented in Java:
import java.util.Scanner;
public class HiddenText {
public static void main(String[] args) {
Scanner in = new Scanner(System.in).useDelimiter("\n");
boolean isRunning = true;
while (isRunning) {
System.out.println("Select action:\ne - encode\nd - decode\nq - quit");
String action = in.next();
switch (action) {
case "e" -> {
System.out.println("Enter character: ");
char c = in.next().charAt(0);
System.out.println("Enter text to hide: ");
String secret = in.next();
System.out.println("Encoded: " + encode(c, secret));
}
case "d" -> {
System.out.println("Enter text containing hidden text");
String encoded = in.next();
System.out.println("Decoded: " + decode(encoded));
}
case "q" -> {
isRunning = false;
}
default -> System.out.println("Invalid action");
}
}
}
public static String encode(char c, String secret) {
StringBuilder sb = new StringBuilder().append(c);
for (byte b : secret.getBytes()) {
sb.append(byteToVariation(b));
}
return sb.toString();
}
public static String decode(String encoded) {
char[] chars = encoded.toCharArray();
byte[] data = new byte[chars.length];
int l = 0;
for(int i = 0; i < chars.length; i++) {
int x = chars[i] & 0xFE00;
if (x == 0xFE00) {
data[l++] = (byte) ((chars[i] & 0xF) - 128);
} else if (x == 0xDA00) {
i++;
data[l++] = ((byte) ((chars[i] & 0xFF) + 16 - 128));
}
}
return new String(data, 0, l);
}
public static char[] byteToVariation(byte b) {
int i = b + 128;
int codePoint;
if (i < 16 ) {
codePoint = 0xFE00 | i;
} else {
codePoint = 0xE0100 | (i - 16);
}
return Character.toChars(codePoint);
}
}
#snippets@ilyosshares
import java.util.Scanner;
public class HiddenText {
public static void main(String[] args) {
Scanner in = new Scanner(System.in).useDelimiter("\n");
boolean isRunning = true;
while (isRunning) {
System.out.println("Select action:\ne - encode\nd - decode\nq - quit");
String action = in.next();
switch (action) {
case "e" -> {
System.out.println("Enter character: ");
char c = in.next().charAt(0);
System.out.println("Enter text to hide: ");
String secret = in.next();
System.out.println("Encoded: " + encode(c, secret));
}
case "d" -> {
System.out.println("Enter text containing hidden text");
String encoded = in.next();
System.out.println("Decoded: " + decode(encoded));
}
case "q" -> {
isRunning = false;
}
default -> System.out.println("Invalid action");
}
}
}
public static String encode(char c, String secret) {
StringBuilder sb = new StringBuilder().append(c);
for (byte b : secret.getBytes()) {
sb.append(byteToVariation(b));
}
return sb.toString();
}
public static String decode(String encoded) {
char[] chars = encoded.toCharArray();
byte[] data = new byte[chars.length];
int l = 0;
for(int i = 0; i < chars.length; i++) {
int x = chars[i] & 0xFE00;
if (x == 0xFE00) {
data[l++] = (byte) ((chars[i] & 0xF) - 128);
} else if (x == 0xDA00) {
i++;
data[l++] = ((byte) ((chars[i] & 0xFF) + 16 - 128));
}
}
return new String(data, 0, l);
}
public static char[] byteToVariation(byte b) {
int i = b + 128;
int codePoint;
if (i < 16 ) {
codePoint = 0xFE00 | i;
} else {
codePoint = 0xE0100 | (i - 16);
}
return Character.toChars(codePoint);
}
}
#snippets@ilyosshares