-1

I want to encrypt a String to an cipher which consists only of alphabets (a-z/A-Z) and numbers(0-9). I doesn't want it to have any other characters (=, &, !.. etc).

Is there any way to achieve it? If so, can please someone help me out. A java code will be preferred without any external libraries.

Note:

  • I have already tried base64 encoding but it does't work as it uses '=' as padding character.

  • This generated cipher will be used in web urls to pass values.

  • The cipher should also be decoded back to original String.

Prasenjeet Paul
  • 121
  • 1
  • 9
  • 1
    Why do you think web URLs must be limited to alphanumeric? – Jim Garrison May 27 '18 at 18:17
  • I'm trying to pass some values in the url. Eg: `http://website.com?value=cipher&value=cipher` where the value must be alphanumeric. If the cipher contains any other characters like '=' or '&' it will break the link structure. That's why I'm concerned. @JimGarrison – Prasenjeet Paul May 27 '18 at 18:22
  • You can simply use any cipher and then encode is using e.g. URLEncoder – Michal May 27 '18 at 18:27
  • How to get that cipher (alphanumeric) ? @Michal – Prasenjeet Paul May 27 '18 at 18:45
  • for two way encryption you can take a look here https://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example. What you get is encrypted Base64 encoded string and then encode it with URLEncoder.encode(string, "UTF-8"). after you can safely use it as url parameter – Michal May 27 '18 at 18:56
  • For a different use, I am using Base64 encoding and then replace '+', '/', '=' with '-', '_', and '.' – Juan May 27 '18 at 19:19
  • Thanks for all your inputs. I found the solution on my own. I used Base32 encoding instead. [Post link](https://stackoverflow.com/questions/4375830/java-based-encrypter-that-produces-only-alphanumeric-characters?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) This post was very helpful. – Prasenjeet Paul May 28 '18 at 12:46

1 Answers1

-1

Assuming one way hash is enough for you, you can implement something like this

public String hashToHexString(String whatever) throws NoSuchAlgorithmException{

        byte[] digest = MessageDigest.getInstance("your one way hash alghoritm")
                .digest(whatever.getBytes());
        return DatatypeConverter.printHexBinary(digest);

}
Michal
  • 970
  • 7
  • 11