Solved

Java String encryption and decryption without special charectors

Posted on 2013-06-17
6
1,503 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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
swing controls 2 37
jsp error 6 70
Tagging and Merging on Branch 1 68
What does this method description mean? 1 11
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

732 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