• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Encryption Decryption issue with DES3

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

I am facing a problem while encryption and decryption of password using DES3 algorithm.I do hava a java file BOSEncryption_Citi.java. This file has got methods encryptString and decrypt.I call these methods from

1. separate java file Test.java
2. a servlet

Encryption - Decryption happens correctly while using Test.java but when I use the servlet it gives me an exception IllegalBlockSizeException in the decrypt method.

My code is as follows:

Plz let me know what's wrong with the code:I know tht the code seems to be huge but plz bear.

/************************BOSEncryption_Citi.java*************************/
package security;

import javax.crypto.*;
import javax.crypto.spec.*;

import sun.misc.BASE64Decoder;

import java.security.*;
import java.security.spec.*;

//import log.DebugLog;

/**
* @author 160775
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class BOSEncryption_Citi {


static javax.crypto.Cipher ecipher;
static javax.crypto.Cipher dcipher;

private static final String ENCRYPTION_KEY = "AB1DEFGTYIOP123456789981";

BOSEncryption_Citi(String passPhrase) {
try {
// Create the key
byte [] key1 = passPhrase.getBytes();
KeySpec keySpec = new DESedeKeySpec(key1);
SecretKey key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);
ecipher = javax.crypto.Cipher.getInstance("DESede");
dcipher = javax.crypto.Cipher.getInstance(key.getAlgorithm());
ecipher.init(javax.crypto.Cipher.ENCRYPT_MODE,key);
dcipher.init(javax.crypto.Cipher.DECRYPT_MODE,key);
} catch (java.security.spec.InvalidKeySpecException ikse) {
// DebugLog.l("BOSEncryption_Citi::constructor::InvalidKeySpecException"+ikse.toString());
} catch (javax.crypto.NoSuchPaddingException nspe) {
// DebugLog.l("BOSEncryption_Citi::constructor::NoSuchPaddingException"+nspe.toString());
} catch (java.security.NoSuchAlgorithmException nsae) {
// DebugLog.l("BOSEncryption_Citi::constructor::NoSuchAlgorithmException"+nsae.toString());
} catch (java.security.InvalidKeyException ike) {
// DebugLog.l("BOSEncryption_Citi::constructor::InvalidKeyException"+ike.toString());
}catch(Exception e){
//DebugLog.l("BOSEncryption_Citi::constructor::Exception"+e.toString());
}

}

public static String encryptString(String str) {
try {
// DebugLog.l("BOSEncryption_Citi::from encryptString");
BOSEncryption_Citi encrypter = new BOSEncryption_Citi(ENCRYPTION_KEY);
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
javax.crypto.Cipher ecipher= null;
javax.crypto.Cipher dcipher= null;
return new sun.misc.BASE64Encoder().encode(enc);
} catch (javax.crypto.BadPaddingException bpe) {
// DebugLog.l("BOSEncryption_Citi::encryptString::BadPaddingException"+bpe.toString());
}// catch (IllegalBlockSizeException ibse) {
//DebugLog.l("BOSEncryption_Citi::encryptString::IllegalBlockSizeException"+ibse.toString());
//}
catch (java.io.IOException ioe) {
// DebugLog.l("BOSEncryption_Citi::encryptString::IOException"+ioe.toString());
} catch (Exception e) {
// DebugLog.l("BOSEncryption_Citi::encryptString::Exception"+e.toString());
}
return null;
}

public static String decrypt(String str) {
try {
// DebugLog.l("BOSEncryption_Citi: ecrypt::s");
BOSEncryption_Citi encrypter = new BOSEncryption_Citi(ENCRYPTION_KEY);

byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
byte[] utf8 = dcipher.doFinal(dec);
javax.crypto.Cipher ecipher= null;
javax.crypto.Cipher dcipher= null;
return new String(utf8, "UTF8");
} catch (javax.crypto.BadPaddingException bpe) {
//DebugLog.l("BOSEncryption_Citi: ecrypt::BadPaddingException"+bpe.toString());
} //catch (IllegalBlockSizeException ibse1) {
//DebugLog.l("BOSEncryption_Citi: ecrypt::IllegalBlockSizeException"+ibse.toString());
//}
catch (java.io.IOException ioe) {
//DebugLog.l("BOSEncryption_Citi: ecrypt::IOException"+ioe.toString());
}catch (Exception e) {
//DebugLog.l("BOSEncryption_Citi: ecrypt::Exception"+e.toString());
}
return null;
}
}

/************************Test.java*************************/

/*
* Created on Apr 7, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

package security;

import EncryptTest.BOSEncryption_Citi;


/**
* @author 160775
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/


public final class Test {

public static void main(String args[])
{
String encryptedPassword1 = BOSEncryption_Citi.encryptString("jdbc racle:thin:@192.168.106.50:1521:hslp1r2");
System.out.println(encryptedPassword1);
String encryptedPassword2 = BOSEncryption_Citi.encryptString("cbosowner");
System.out.println(encryptedPassword2);
String encryptedPassword3 = BOSEncryption_Citi.encryptString("cbos123");
System.out.println(encryptedPassword3);

String decryptedPassword1 = BOSEncryption_Citi.decrypt(encryptedPassword1);
String decryptedPassword2 = BOSEncryption_Citi.decrypt(encryptedPassword2);
String decryptedPassword3 = BOSEncryption_Citi.decrypt(encryptedPassword3);
System.out.println(decryptedPassword1);
System.out.println(decryptedPassword2);
System.out.println(decryptedPassword3);


}
}



/*********************WebStartUpServlet_Citi.java*************************/






import config.Config;
import dbconnector.ConnectionPool;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.SQLException;
//import com.javaexchange.dbConnectionBroker.*;
import masterdata.MasterData;
//import client.bos.properties.BOSProperties;
//import log.*;
//import java.net.URLEncoder;
import dbconnector.DbConnector;
import security.BOSEncryption;
import security.BOSEncryption_Citi;

public class WebStartUpServlet_Citi extends HttpServlet
{

//private DbConnectionBroker connectionPool ;
//private ServletContext context ;

public void init(ServletConfig config) throws ServletException
{
super.init(config);
//log("init of WebStartUpServlet");
// DebugLog dlog = new DebugLog("WebStartUpServlet.txt");

try
{


/*********************Code Added by Nikhil on Apr 07,2006 for loading LoginPwd_Citi properties file.
LoginPwd_Citi properties file is generated using DES3 Encryption Algorithm***********************/
ResourceBundle bundle = PropertyResourceBundle.getBundle("LoginPwd_Citi");
//bundle = PropertyResourceBundle.getBundle("LoginPwd_Citi");

String conURL_Citi = (String)bundle.getObject("dburl");
String login_Citi = (String)bundle.getObject("login");
String pwd_Citi = (String)bundle.getObject("pwd");

String conURL_Citi_decrypt="";
String login_Citi_decrypt="";
String pwd_Citi_decrypt="";

log("conURL_Citi::"+conURL_Citi);
log("login_Citi::"+login_Citi);
log("pwd_Citi::"+pwd_Citi);

log("Calling BOSEncryption_Citi.decrypt functiion");


conURL_Citi_decrypt=BOSEncryption_Citi.decrypt(conURL_Citi);
login_Citi_decrypt=BOSEncryption_Citi.decrypt(login_Citi);
pwd_Citi_decrypt=BOSEncryption_Citi.decrypt(pwd_Citi);

log("Decrypted::conURL_Citi::"+conURL_Citi_decrypt);
log("Decrypted::login_Citi::"+login_Citi_decrypt);
log("Decrypted: wd_Citi::"+pwd_Citi_decrypt);



log("after reading from citi file ");
ConnectionPool.manageConnectionsToDb();
ConnectionPool.printConnections();
log("WebStartUpServlet::Connection Pool Created");
DbConnector dbCon=new DbConnector();

MasterData mastData = new MasterData();
log("Object Created for MasterData");
// DebugLog.l("Object Created for MasterData");

mastData.getExchSecurityNameHash(dbCon);
log("getExchSecurityNameHash succesfully called");
mastData.getMtsPrecExchSecurityNameHash(dbCon);
log("Fetching Of data Succesful");
// DebugLog.l("Fetching Of data Succesful");
}
catch(MissingResourceException mre)
{
log("In WebStartUpServlet:: init \"PrecWebProperties.properties\" or \"LoginPwd.properties\" not found " );
}
/*catch(IllegalBlockSizeException ibse)
{
log("IllegalBlockSizeException occured"+ ibse.toString());
}*/
catch (Exception e)
{
log("Problem in initializing WebStartUpServlet"+e.getMessage());
}

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException//, IOException
{
//log("WebStartUpServlet: oPost and calling doGet");
doGet(request,response);
}


public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException//,IOException
{
//log("WebStartUpServlet: oGet and calling doGet");
}

public void destroy()
{
//log("WebStartUpServlet: estroy");
}

}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nikhil, when posting code of any length, please use CODE tags around them - they keep the code formatted and readable. It's next to impossible to make sense of the code displayed as it is. During posting, simply select the code in the input field, and click the CODE button.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic