sasidhar1229
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.
For example xw8rD/uTSW0=, here without / and = .
I am using Base64Encoder.
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
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=
</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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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().e ncode(enc) ;
return Base64.encodeBase64URLSafe String(enc );
} catch (javax.crypto.BadPaddingEx ception e) {
} catch (IllegalBlockSizeException e) {
} catch (UnsupportedEncodingExcept ion e) {
} catch (java.io.IOException e) {
}
return null;
}
Thanks for your reply.
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().e
return Base64.encodeBase64URLSafe
} catch (javax.crypto.BadPaddingEx
} catch (IllegalBlockSizeException
} catch (UnsupportedEncodingExcept
} catch (java.io.IOException e) {
}
return null;
}
Thanks for your reply.
Not a problem, glad to help!
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.