2

I have a piece of code that works fine when encrypting, encryption works but i don't like the characters used in the encrypted string since it has to be passed in a URL, characters I would prefer are a-z, A-Z and 0-9, is this possible?

String key = enc_key;

// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");

// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.out.println(new String(encrypted));

the encrypted string looks like this ò'­Ê>‡6?dövNé÷s which is not URL friendly :-(, any suggestions please?

1 Answer 1

5

First use your normal AES enciphering and then base64 encode the result. At the other end, they have to base64 decode and then decipher. If you are on java 8, use built-in base64 codec. If on java 7 or older, use apache commons codec.

Sign up to request clarification or add additional context in comments.

2 Comments

base64 encoding is a standard encoding, meaning it can be decoded by anyone else, and since this will be used on via URL, so that's not safe, or am I wrong?
Base64 encoding is to be used after the encryption in order to encode encrypted bytes into the form that can be used safely in URLs. On the other side, that string will first be base64-decoded and then decrypted in order to get to plain-text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.