• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Encryption: Runtime Exception when run as JavaBean

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I have been using a password based encryption decryption.
Its giving problems.
I want to use a javabean for using it along with a JSP form which accepts
user password and encrypts before passing it on to database. Iam using
resin 2.0.5 as the http server and servlet/jsp container.
The first program below is working fine when i run it on the command prompt.
The complete program listing is given below
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
class DesEncrypter
{
Cipher ecipher;
Cipher dcipher;
// 8-byte Salt
byte[] salt = {(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
(byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03};
// Iteration count
int iterationCount = 19;
DesEncrypter(String passPhrase)
{
try
{
// Create the key
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
String s = key.getAlgorithm();
System.out.println(s);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
// Create the ciphers
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
}
catch (java.security.InvalidAlgorithmParameterException e) {}
catch (java.security.spec.InvalidKeySpecException e) {}
catch (javax.crypto.NoSuchPaddingException e) {}
catch (java.security.NoSuchAlgorithmException e) {}
catch (java.security.InvalidKeyException e) {}
}
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().encode(enc);
sun.misc.BASE64Encoder encodeStr = new sun.misc.BASE64Encoder();
String retEncString = encodeStr.encode(enc);
return retEncString;
}
catch (javax.crypto.BadPaddingException e) {}
catch (IllegalBlockSizeException e) {}
catch (UnsupportedEncodingException e) {}
catch (java.io.IOException e) {}
return null;
}
public String decrypt(String str)
{
try
{
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
}
catch (javax.crypto.BadPaddingException e) {}
catch (IllegalBlockSizeException e) {}
catch (UnsupportedEncodingException e) {}
catch (java.io.IOException e) {}
return null;
}
}
public class DesEncrypterEx
{
public static void main(String args[])
{
// Here is an example that uses the class
try
{
// Create encrypter/decrypter class
DesEncrypter encrypter = new DesEncrypter("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
// Encrypt
String encrypted = "";
String toencrypt = "Don't tell anybody!";
System.out.println("before encrypt="+toencrypt);
encrypted = encrypter.encrypt(toencrypt);
System.out.println("after encrypted="+encrypted);
// Decrypt
String decrypted = encrypter.decrypt(encrypted);
System.out.println("decrypted="+decrypted);
}
catch (Exception e) {}
}
}
I converted this to a javabean to be used with a JSP file
the complete listing is as given below.
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.*;
public class DesEncrypter
{
private Cipher ecipher;
private Cipher dcipher;
// 8-byte Salt
byte[] salt = {(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
(byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03};
// Iteration count
int iterationCount = 19;
private String passPhrase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String encString = "";
private String retEncString = "";
private String decString = "";
private String retDecString = "";

public DesEncrypter(){}
public void init()
{
try
{
// Create the key
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
// Create the ciphers
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
}
catch (java.security.InvalidAlgorithmParameterException e) {System.out.println(e);}
catch (java.security.spec.InvalidKeySpecException e) {System.out.println(e);}
catch (javax.crypto.NoSuchPaddingException e) {System.out.println(e);}
catch (java.security.NoSuchAlgorithmException e) {System.out.println(e);}
catch (java.security.InvalidKeyException e) {System.out.println(e);}
}
public void encrypt()
{
//retEncString = encString;
try
{
// Encode the string into bytes using utf-8
byte[] utf8 = encString.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
sun.misc.BASE64Encoder encodeStr = new sun.misc.BASE64Encoder();
retEncString = encodeStr.encode(enc);
//retEncString = enc.toString();
}
catch (javax.crypto.BadPaddingException e) {System.out.println(e);}
catch (IllegalBlockSizeException e) {System.out.println(e);}
catch (UnsupportedEncodingException e) {System.out.println(e);}
catch (java.io.IOException e) {System.out.println(e);}
}
public void decrypt()
{
try
{
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(decString);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
retDecString = new String(utf8, "UTF8");
}
catch (javax.crypto.BadPaddingException e) {System.out.println(e);}
catch (IllegalBlockSizeException e) {System.out.println(e);}
catch (UnsupportedEncodingException e) {System.out.println(e);}
catch (java.io.IOException e) {System.out.println(e);}
}
public void setEncString(String encString){this.encString = encString;}
public void setDecString(String decString){this.decString = decString;}
public String getRetEncString(){return this.retEncString;}
public String getRetDecString(){return this.retDecString;}
}
when i call this javabean method iam getting a run time exeption in init() method
at :SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
The exception statement is as given below.
java.security.NoSuchAlgorithmException: Class com.sun.crypto.provider.PBEKeyFactory configured for SecretKeyFactory not a SecretKeyFactory
Please somebody help me solve this.
Regards
Dilip M P
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic