Solved

Java String encryption and decryption without special charectors

Posted on 2013-06-17
6
1,325 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now