• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

InvalidKeyException

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

I am getting java.security.InvalidKeyException: Wrong key size in my code at statement 1.

Can anyone plz let me know what exactly is the problem.

Regards

Nikhil



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

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

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

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


javax.crypto.Cipher ecipher = null;
javax.crypto.Cipher dcipher = null;

// 8-byte Salt

public static void main(String args[])
{
try {
// Create encrypter/decrypter class
DesEncrypter1 encrypter = new DesEncrypter1("nikhil");

// Encrypt
String encrypted = encrypter.encrypt("grhioregrhgohooihefhoi");

System.out.println("encrypted"+encrypted);

// Decrypt
String decrypted = encrypter.decrypt(encrypted);
System.out.println("decrypted"+decrypted);
} catch (Exception e) {
}

}


DesEncrypter1(String passPhrase) {
try {
// Create the key
byte [] key1 = passPhrase.getBytes(); //Statement 1
System.out.println("key1"+key1.toString());
KeySpec keySpec = new DESedeKeySpec(key1);
System.out.println("keySpec"+keySpec);
SecretKey key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);
System.out.println("key"+key);
ecipher = javax.crypto.Cipher.getInstance("DESede");
System.out.println("ecipher"+ecipher);
dcipher = javax.crypto.Cipher.getInstance(key.getAlgorithm());
System.out.println("dcipher"+dcipher);
// Prepare the parameter to the ciphers
//AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

// Create the ciphers
ecipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key);
dcipher.init(javax.crypto.Cipher.DECRYPT_MODE, key);
} /*catch (java.security.InvalidAlgorithmParameterException e) {
}*/ catch (java.security.spec.InvalidKeySpecException e1) {
System.out.println("InvalidKeySpecException"+e1.toString());
} catch (javax.crypto.NoSuchPaddingException e2) {
System.out.println("NoSuchPaddingException"+e2.toString());
} catch (java.security.NoSuchAlgorithmException e3) {
System.out.println("NoSuchAlgorithmException"+e3.toString());
} catch (java.security.InvalidKeyException e4) {
System.out.println("InvalidKeyException"+e4.toString());
}catch(Exception e5){
System.out.println("Exception"+e5.toString());
}

}

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);
} catch (javax.crypto.BadPaddingException e1) {
System.out.println("BadPaddingException"+e1.toString());
} catch (IllegalBlockSizeException e2) {
System.out.println("IllegalBlockSizeException"+e2.toString());
}/* catch (UnsupportedEncodingException e) {
}*/ catch (java.io.IOException e3) {
System.out.println("IOException"+e3.toString());
} catch (Exception e4) {
System.out.println("Exception"+e4.toString());
}
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;
}




}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to bet that the exeption actually occurs two lines below the one you marked. If you look at the javadocs for DESedeKeySpec you'll see that it needs a 24-byte argument for its constructor.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic