Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java String encryption and decryption without special charectors

Posted on 2013-06-17
6
Medium Priority
?
1,771 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 36

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 36

Accepted Solution

by:
mccarl earned 2000 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 36

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 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:

610 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