-2

I am writing a code which takes ASCII values from a char array and displays it on console.

public class Test1 {

  public static void main(String[] args) { 
       char array[] = new char[] {65, 32, 81, 117, 105, 99, 107, 32, 66, 114, 111, 119, 110, 32, 70, 111, 120, 32, 74, 117, 109, 112, 101, 100, 32, 79, 118, 101, 114, 32, 65, 32, 76, 97, 122, 121, 32, 68, 111, 103 };

       for(int l=0;l<array.length;l++) {
           System.out.print(array[l]);   
       }       
    }
}

is there any efficient way to store the ASCII values into the char array so that the program is more compact and its hard to guess the output.

Chaitanya Uttarwar
  • 318
  • 2
  • 6
  • 13
  • 1
    You seem to be looking for obfuscation. Check [this thread](https://stackoverflow.com/questions/2537568/best-java-obfuscator) to see if you find anything helpful. – Bernat Oct 24 '17 at 07:32
  • 2
    Looks a bit like an [XY problem here](https://meta.stackexchange.com/questions/66377). What is it that you are actually trying to do? [Security through obscurity](https://en.wikipedia.org/wiki/Security_through_obscurity) isn't usually the way to go. – Ivar Oct 24 '17 at 07:33
  • 1
    You don't need to cast each element to a char. They're in a char array. – khelwood Oct 24 '17 at 07:34
  • No i am not looking for obfuscation, i just need to make this code more hard to guess the output – Chaitanya Uttarwar Oct 24 '17 at 07:47
  • @khelwood Edited the casting part. thanks for rectifying it. – Chaitanya Uttarwar Oct 24 '17 at 07:49

1 Answers1

2

If you are going to be storing "secrets" in Java code they will always be vulnerable as it is so easy to decompile and read the code.

If you want to obfuscate the secret, you could convert the bytes of your string to base64 and store them that way, or "Not" the bytes and store them as a binary resource in a jar on your class path.

Ultimately though, unless you go down the road of proper encryption using a framework like JCE with keys that are not shipped with the code, your secrets won't be safe.

(Oh, and "A Quick Brown Fox Jumped Over A Lazy Dog" doesn't contain H, T or S. Normally it's Jumps over the ;-))

Spangen
  • 4,420
  • 5
  • 37
  • 42
  • vulnerability is not an issue here.. i just need to make that code so confusing that one cannot guess the output without compiling and executing it. :D :D ps - thanks for the H,T and S observation.. :) I'll rectify it. – Chaitanya Uttarwar Oct 24 '17 at 07:45
  • I copied your array into excel to decode it. I'm sure that what ever mechanism you choose will either be so convoluted it will be a massive pain to maintain, or else will be relatively easy to decode manually without decompilation. Is the issue that other employees are not trustworthy? Do you have an HR issue rather than a security one? – Spangen Oct 24 '17 at 07:50