aspose file tools*
The moose likes Security and the fly likes Special characters in the decrypted text in servle, jce provider using Bouncy Castlte Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "Special characters in the decrypted text in servle, jce provider using Bouncy Castlte" Watch "Special characters in the decrypted text in servle, jce provider using Bouncy Castlte" New topic
Author

Special characters in the decrypted text in servle, jce provider using Bouncy Castlte

Balu Herbert
Greenhorn

Joined: Dec 01, 2003
Posts: 2
I am trying to encrypt/decrypt a string using JCE blowfish in the servlet. 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 0.0.0.0/0.0.0.0:8009
[INFO] JkMain - -Jk running ID=0 time=15/62 config=C:\Program Files\Apache Grou
p\Tomcat 4.1\conf\jk2.properties
in encrypt text - AC9942068017-Aug-2001To17-Aug-2001.csv
in encrypt[B@568fb5
encrypt key = pkjNZmplQN4k4rMxqCXj4Cj2u6HR2gDK0z2rjW+I0iO5n7+tXy6DcQ==
pkjNZmplQN4k4rMxqCXj4Cj2u6HR2gDK0z2rjW+I0iO5n7+tXy6DcQ==
decrypted = AC9942068017-Aug-2001To1?Z
Code:
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.*;
import java.net.*;
protected String encryptFilename(String filenameToEncrypt, String key){
try{
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){
e.printStackTrace();
show("Encryption Error");
return null;
}
}
protected String decryptFilename(String filenameToDecrypt, String key){
try{
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){
e.printStackTrace();
show("Decryption Error");
return null;
}
}
 
jQuery in Action, 2nd edition
 
subject: Special characters in the decrypted text in servle, jce provider using Bouncy Castlte