Solved

Java String encryption and decryption without special charectors

Posted on 2013-06-17
6
1,428 Views
Last Modified: 2013-06-17
How can we encrypt a string without any special characters.

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

I am using Base64Encoder.
0
Comment
Question by:sasidhar1229
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 39253279
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39253956
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
0
 

Author Comment

by:sasidhar1229
ID: 39255187
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 .
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39255206
By Base64Encoder, are you referring to "sun.misc.Base64Encoder"?

If so, apart from the fact that you shouldn't really use internal classes, you probably should change to a more full featured encoder. Have a look at Apache Commons version...

http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html

It has methods (such as "encodeBase64URLSafeString") that use - and _ instead of + and /   and also don't add extra padding (ie. the = at the end). The purpose of these methods are for exactly what you are trying to do!
0
 

Author Comment

by:sasidhar1229
ID: 39255211
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.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39255214
Not a problem, glad to help!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 61
java jdbc batch example 8 39
How to convert from xls to xlsx using java 7 52
java mysql insert application 14 25
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question