File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Special characters in the decrypted text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Special characters in the decrypted text" Watch "Special characters in the decrypted text" New topic

Special characters in the decrypted text

Balu Herbert

Joined: Dec 01, 2003
Posts: 2
I am trying to encrypt/decrypt a string using JCE blowfish in the servlet (Tomcat engine 4.1.x) The encrypted text will be kept in the user session after the encryption process using a pre-defined key string (eg:- "hello").When I try to decrypt the string using the encrypted text and key in the servlert get method it gives me a corrupted string with some funny specical character in it.
Has anyone come across this problem before.. if someone could help me to sort out this would be helpful as i have to deliver this stuff asap.
pls. see below for the apache window with log messages.
[INFO] Http11Protocol - -Starting Coyote HTTP/1.1 on port 8080
[INFO] Http11Protocol - -Starting Coyote HTTP/1.1 on port 8443
[INFO] ChannelSocket - -JK2: ajp13 listening on
[INFO] JkMain - -Jk running ID=0 time=15/62 config=C:\Program Files\Apache Grou
p\Tomcat 4.1\conf\
in encrypt text - AC9942068017-Aug-2001To17-Aug-2001.csv
in encrypt[B@568fb5
encrypt key = pkjNZmplQN4k4rMxqCXj4Cj2u6HR2gDK0z2rjW+I0iO5n7+tXy6DcQ==
decrypted = AC9942068017-Aug-2001To1?Z
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.*;
protected String encryptFilename(String filenameToEncrypt, String key){
show("in encrypt" + filenameToEncrypt);
//pass the key string as the key value for the encryption
SecretKeySpec keyspec = new SecretKeySpec (key.getBytes("UTF8"), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keyspec);
byte[] plaintext = filenameToEncrypt.getBytes("UTF8");
//byte[] plaintext = filenameToEncrypt.getBytes();
byte[] encrypted = cipher.doFinal(plaintext);
show("in encrypt" + encrypted.toString());
//encode the encrypted string using base64
String encodedStr = new BASE64Encoder().encode(encrypted);
//encodedStr = URLEncoder.encode(encodedStr);
return encodedStr;
//return encrypted.toString();
} catch (Exception e){
show("Encryption Error");
return null;
protected String decryptFilename(String filenameToDecrypt, String key){
SecretKeySpec keyspec2 = new SecretKeySpec (key.getBytes("UTF8"), "Blowfish");
Cipher cipher2 = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher2.init(Cipher.DECRYPT_MODE, keyspec2);
//decode the encrypted string for decrption using base64
//String encoded2 = URLDecoder.decode(filenameToDecrypt);
String encoded2 = new String(new BASE64Decoder().decodeBuffer(filenameToDecrypt));
byte[] decryptedText = cipher2.doFinal(encoded2.getBytes());
String output = new String(decryptedText, "UTF8");
//String output = new String(decryptedText);
return output;
//String decryptencodedStr = new BASE64Encoder().encode(decryptedText);
//return decryptencodedStr;
} catch (Exception e){
show("Decryption Error");
return null;
I agree. Here's the link:
subject: Special characters in the decrypted text
It's not a secret anymore!