1
\$\begingroup\$

I have 2 functions, which are going to hide/ unhide the multiple languages plain text.

Security is not a major concern.

The objectives are

  1. Just want to find a way to hide multiple languages plain text from ordinary user. Even he spends some effort to unhide it, that is completely Ok.
  2. Works well for all kind of String.

I was wondering, do I miss out any possible edge cases?


Functions to obfuscate and deobfuscate String

public static String obfuscate(String string) {
    if (string == null) {
        return null;
    }

    if (string.equals("")) {
        return "";
    }

    byte[] input = string.getBytes(Charset.forName(UTF_8));

    byte[] binary = Base64.encode(input, Base64.NO_WRAP);

    String encoded = new String(binary, Charset.forName(UTF_8));

    // Reverse the string
    String reverse = new StringBuffer(encoded).reverse().toString();

    return reverse;
}

public static String deobfuscate(String reverse) {
    if (reverse == null) {
        return null;
    }

    if (reverse.equals("")) {
        return "";
    }

    // Reverse the string
    String encoded = new StringBuffer(reverse).reverse().toString();

    byte[] binary = encoded.getBytes(Charset.forName(UTF_8));

    try {
        byte[] input = Base64.decode(binary, Base64.NO_WRAP);

        if (input.length == 0) {
            return null;
        }

        return new String(input, Charset.forName(UTF_8));
    } catch (java.lang.IllegalArgumentException e) {
        Log.e(TAG, "", e);
        return null;
    }
}

The below are my unit test, which try to capture all possible cases.

Unit test

@Test
public void obfuscate() {
    String input = null;
    String output = Utils.obfuscate(input);
    String expectedOutput = null;
    assertEquals(expectedOutput, output);

    input = "";
    output = Utils.obfuscate(input);
    expectedOutput = "";
    assertEquals(expectedOutput, output);

    input = " ";
    output = Utils.obfuscate(input);
    expectedOutput = "==AI";
    assertEquals(expectedOutput, output);

    input = "a";
    output = Utils.obfuscate(input);
    expectedOutput = "==QY";
    assertEquals(expectedOutput, output);

    input = "a quick fox jump over the lazy dog 敏捷的棕色狐狸跨過懶狗";
    output = Utils.obfuscate(input);
    expectedOutput = "=c5inb7hm7Ygpj6toj7inD5inLbioX5omTomnfbjm/YlmDyZvRGI5pXYsBSZoRHIyVmdvBCctVnagg3bmByajlWdxBSY";
    assertEquals(expectedOutput, output);

    input = "キツネが怠惰な犬を飛び越える a quick fox jump over the lazy dog 敏捷的棕色狐狸跨過懶狗 Ein schneller Fuchs springt über den faulen Hund สุนัขจิ้งจอกตัวเตี้ยกระโดดข้ามสุนัขขี้เกียจ быстрый лис перепрыгнуть через ленивую собаку";
    output = Utils.obfuscate(input);
    expectedOutput = "=MY06CNsQHL0+CdgRDijRPY0yCNuQ3L01C9uQDytQXL0AGdtQfY0gwY0CG9gR3L0zC9iRDY0/CdtQDY01C9vQDSgRjL07CNI5C9iRDY0CGdgRvY0xCNIIiL4iiL41iL4BiL4AmL4JmL41iL4CiL4CiL4xiL4ZiL44iL4qiL4hiL4yiL4JmL4CiL4UiL4UiL4CmL4wiL4jiL4BiL4iiL4JmL41iL4ViL4AmL4niL4xiL4ViL4BiL4tiL4IiL4HiL4JmL40iL4IiL4CiL4xiL4ZiL44iL4qiL4gQmb1hEIuVGb1FmZg4WZkBiclJGvDDCdn5WayB3cgMHajVnRgIXZsxWZuh2YzBibpVEIXu452eo5OGY6oeL64u45Qu45ymI6VOq5Eq5532o5PWp5gc2bkBSe6FGbgUGa0BiclZ3bgAXb1pGI49mZgs2YpVXcgEGILK44IG44KaL6zG44bOa6SK44sq45qG44wOo5gCo5MG44NO44EO44tK44";
    assertEquals(expectedOutput, output);
}

@Test
public void deobfuscate() {
    String input = null;
    String output = Utils.deobfuscate(input);
    String expectedOutput = null;
    assertEquals(expectedOutput, output);

    input = "";
    output = Utils.deobfuscate(input);
    expectedOutput = "";
    assertEquals(expectedOutput, output);

    input = "==AI";
    output = Utils.deobfuscate(input);
    expectedOutput = " ";
    assertEquals(expectedOutput, output);

    input = "==QY";
    output = Utils.deobfuscate(input);
    expectedOutput = "a";
    assertEquals(expectedOutput, output);

    input = "=c5inb7hm7Ygpj6toj7inD5inLbioX5omTomnfbjm/YlmDyZvRGI5pXYsBSZoRHIyVmdvBCctVnagg3bmByajlWdxBSY";
    output = Utils.deobfuscate(input);
    expectedOutput = "a quick fox jump over the lazy dog 敏捷的棕色狐狸跨過懶狗";
    assertEquals(expectedOutput, output);


    input = "=MY06CNsQHL0+CdgRDijRPY0yCNuQ3L01C9uQDytQXL0AGdtQfY0gwY0CG9gR3L0zC9iRDY0/CdtQDY01C9vQDSgRjL07CNI5C9iRDY0CGdgRvY0xCNIIiL4iiL41iL4BiL4AmL4JmL41iL4CiL4CiL4xiL4ZiL44iL4qiL4hiL4yiL4JmL4CiL4UiL4UiL4CmL4wiL4jiL4BiL4iiL4JmL41iL4ViL4AmL4niL4xiL4ViL4BiL4tiL4IiL4HiL4JmL40iL4IiL4CiL4xiL4ZiL44iL4qiL4gQmb1hEIuVGb1FmZg4WZkBiclJGvDDCdn5WayB3cgMHajVnRgIXZsxWZuh2YzBibpVEIXu452eo5OGY6oeL64u45Qu45ymI6VOq5Eq5532o5PWp5gc2bkBSe6FGbgUGa0BiclZ3bgAXb1pGI49mZgs2YpVXcgEGILK44IG44KaL6zG44bOa6SK44sq45qG44wOo5gCo5MG44NO44EO44tK44";
    output = Utils.deobfuscate(input);
    expectedOutput = "キツネが怠惰な犬を飛び越える a quick fox jump over the lazy dog 敏捷的棕色狐狸跨過懶狗 Ein schneller Fuchs springt über den faulen Hund สุนัขจิ้งจอกตัวเตี้ยกระโดดข้ามสุนัขขี้เกียจ быстрый лис перепрыгнуть через ленивую собаку";
    assertEquals(expectedOutput, output);


    input = "?";
    output = Utils.deobfuscate(input);
    expectedOutput = null;
    assertEquals(expectedOutput, output);
}
\$\endgroup\$
  • 2
    \$\begingroup\$ Obfuscation is part of security. You want to hide plain text from user, but also state that security isn't a main concern. Could you explain more precisely what exactly you are looking for? \$\endgroup\$ – dfhwze Sep 19 '19 at 8:19
  • \$\begingroup\$ Where does the input come from? Should you be testing every character? \$\endgroup\$ – dustytrash Sep 19 '19 at 16:22
  • \$\begingroup\$ The input is from user, and it can be anything. \$\endgroup\$ – Cheok Yan Cheng Sep 20 '19 at 4:02
  • 2
    \$\begingroup\$ @dfhwze We want to stop average Joe from looking at the content of String. But, we don't need bank level security encryption like AES and key management. That's why we name it "obfuscate" instead of "encryption". \$\endgroup\$ – Cheok Yan Cheng Sep 20 '19 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.