This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Balu Herbert
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic