-1

I need an as short and fast as possible code to change a String to something unreadable (for humans that is), and also to make it readable again. It all needs to happen in java.

Something like this:

encrypt("test");

Would result in something like this:

ôT¿ÄÜTV CÁˆ“5="ËÂÀœššbÀß{¡ä³

and

decrypt("ôT¿ÄÜTV CÁˆ“5 1="ËÂÀœššbÀß{¡ä³");

would than again result into the original

test

What direction should I go, are there any classes that can do this for me? I don't mean something like Base68Encryption or whatever it might be called, I mean a true unreadable text that I can safely send over the internet.

Rheel
  • 410
  • 4
  • 19
  • 1
    Base_64 is readable by humans? Hmmm, you know some smart humans. – jn1kk Apr 26 '13 at 14:14
  • To be safe to share the encrypted message publicly, you need either a shared cipher or a public/private key encryption. Please elaborate on what threats you actually need to protect against. Casual reading? Something stronger? – Ted Hopp Apr 26 '13 at 14:15
  • http://stackoverflow.com/questions/8622367/what-are-best-practices-for-using-aes-encryption-in-android?rq=1 – durron597 Apr 26 '13 at 14:16
  • @jsn No, but is is pretty easy – Rheel Apr 26 '13 at 14:50
  • @Roller the way you worded your question suggested to me that ROT13 would be an acceptable answer. It is easy for a computer to decipher, but most humans would not bother, hence it is unreadable for humans. – emory Apr 26 '13 at 15:01
  • @emory I need way more safety than that – Rheel Apr 26 '13 at 15:05
  • @ROLLER, you really think it is "pretty easy" for a human to understand that `aSBsaWtlIHRvIHBsYXkgc29jY2VyLg==` means `i like to play soccer.`? – jn1kk Apr 26 '13 at 15:17
  • @jsn not understand, but to figure out, yes. – Rheel Apr 26 '13 at 15:44
  • @ROLLER, then you are probably asking for encryption, not obfuscation. – jn1kk Apr 26 '13 at 17:37
  • @jsn have I ever asked for obfuscation? – Rheel Apr 26 '13 at 19:12

3 Answers3

1

See encryption and decryption of data algorithgms with example code http://www.code2learn.com/2011/06/encryption-and-decryption-of-data-using.html. Encryption / decription is embedded in JRE and is not difficult to use.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

Here is a very short example of using true encryption. It's 128 bit AES, which is farily secure - certainly not readable by any stretch of the imagination.

It generates a random key, so it would be different on each run. You would need to share they key between the two programs exchanging data somehow.

private static final String ENCRYPTION_ALGORITHM = "AES/ECB/PKCS5Padding";
private static final SecureRandom RANDOM = new SecureRandom();

public static void main(String[] args) throws UnsupportedEncodingException, GeneralSecurityException {
    final KeyGenerator keyGen = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM.substring(0, ENCRYPTION_ALGORITHM.indexOf('/')));
    keyGen.init(128, RANDOM);
    final SecretKey key = keyGen.generateKey();
    final String s = "My topsecret string";
    System.out.println(s);
    final Cipher encryption = getCipher(key, Cipher.ENCRYPT_MODE);
    final String enc = DatatypeConverter.printBase64Binary(encryption.doFinal(s.getBytes("UTF-8")));
    System.out.println(enc);
    final Cipher decryption = getCipher(key, Cipher.DECRYPT_MODE);
    final String dec = new String(decryption.doFinal(DatatypeConverter.parseBase64Binary(enc)), "UTF-8");
    System.out.println(dec);
}

private static Cipher getCipher(final Key key, final int mode) throws GeneralSecurityException {
    final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
    cipher.init(mode, key, RANDOM);
    return cipher;
}

Output:

My topsecret string
ip4La5KUBJGTTYenoE920V5w0VBHwALv4fp3qyLTY9o=
My topsecret string
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

What direction should I go, are there any classes that can do this for me? I don't mean something like Base68Encryption or whatever it might be called, I mean a true unreadable text that I can safely send over the internet.

There are two ways to address this:

  • You could pick one of the existing high quality encryption systems that is implemented as standard in a typical JVM. Then:

    1. Encode the string as bytes; e.g. using UTF-8.

    2. Encrypt the bytes using an agreed encryption system, and a previously agreed key.

    3. Encode the bytes using base_64 or equivalent.

    and at the other end:

    1. Decode the base_64

    2. Decrypt the bytes with the same system and key.

    3. Decode the UTF-8 to get a String.

  • Use SSL/TLS to secure the TCP/IP connection, and send the String over the connection as-is.

Neither of these options is particularly fast. But don't be tempted to try and invent your own faster encryption system. The chances are that a "home brewed" encryption system it will be a lot easier to break than you realize.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216