aspose file tools
The moose likes Security and the fly likes InvalidKeyException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » Security
Reply Bookmark "InvalidKeyException" Watch "InvalidKeyException" New topic
Author

InvalidKeyException

Nikhil Bansal
Ranch Hand

Joined: Jan 24, 2005
Posts: 60
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;
}




}


ban$al
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35232
    
    7
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.


Android appsImageJ pluginsJava web charts
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InvalidKeyException
 
Similar Threads
Encrypting data using my own key
Encryption Decryption issue with DES3
Problem Urgent: Crypto using jsp - javabean
Encryption in JSP
Encryption: Runtime Exception when run as JavaBean