Link to home
Start Free TrialLog in
Avatar of sasidhar1229
sasidhar1229Flag for India

asked on

Java String encryption and decryption without special charectors

How can we encrypt a string without any special characters.

For example xw8rD/uTSW0=, here without / and = .

I am using Base64Encoder.
Avatar of mccarl
mccarl
Flag of Australia image

I am using Base64Encoder.
Well, the Base64 spec DOES use    / + =   so you can't have a string that is both Base64 AND does not have those characters.

The only "standard" base-n encoding that contains only alphanumeric characters is Base16 but that results in a much larger representation on the input. Check this link for the details of each scheme, http://www.ietf.org/rfc/rfc4648.txt

Can you explain your requirement for not wanting the special characters? That may help us further to understand what you are trying to do.
I am using Base64Encoder.
... which is actually not much to do with encryption. The purpose of that is to encode arbitrary data such that it can be represented as a String
Avatar of sasidhar1229

ASKER

Can you explain your requirement for not wanting the special characters?

I am using UrlRewriteFilter for clean url in struts.

This is the rule pattern in urlrewrite.xml

<rule>
        <from>search/(.*)</from>
        <to>/searchAction?keyword=$1</to>
</rule>

Now the sample url is :

 search/xw8rD/uTSW0=

Here the extra / in keyword breaking the rule. And it is not forwarding to the searchAction.

in jsp I encrypted the keyword, even though it's not working.

search/xw8rD%2FuTSW0%3D .
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Exactly what I found just now.

 public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");

            // Encrypt
            byte[] enc = ecipher.doFinal(utf8);

            // Encode bytes to base64 to get a string
            //return new sun.misc.BASE64Encoder().encode(enc);
           
            return Base64.encodeBase64URLSafeString(enc);

        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }

Thanks for your reply.
Not a problem, glad to help!