Home » Java » "String obfuscation" code from Java to C#-Exceptionshub

"String obfuscation" code from Java to C#-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have the following code for “string obfuscation” in Java and I do need it in C#. Can somebody please help me converting/transforming this code?

  public String obfuscateString(String in) {

    Random r = new Random(System.currentTimeMillis());
    byte[] b = in.getBytes();
    int c = b.length;
    StringBuilder o = new StringBuilder();
    o.append("(new Object() {");
    o.append("int t;");
    o.append("public String toString() {");
    o.append("byte[] buf = new byte[");
    o.append(c);
    o.append("];");

    for (int i = 0; i < c; ++i) {
        int t = r.nextInt();
        int f = r.nextInt(24) + 1;

        t = (t & ~(0xff << f)) | (b[i] << f);

        o.append("t = ");
        o.append(t);
        o.append(";");
        o.append("buf[");
        o.append(i);
        o.append("] = (byte) (t >>> ");
        o.append(f);
        o.append(");");
    }

    o.append("return new String(buf);");
    o.append("}}.toString())");
    return o.toString();
}

It seems there is no such function to create a custom “object” class:

(new Object() {})

works but adding variables like the following does not

(new Object() {int t;})
How to&Answers: