Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

java.security.InvalidKeyException and iv explanation

843811Apr 16 2007 — edited Apr 16 2007
hi guys, i am encrypting with a 128bit AES key and it seems to have worked ok, but when i decrypt i get the following error.

[read outjavax.crypto.spec.SecretKeySpec@fffe8055
Exception: java.security.InvalidKeyException: Parameters missing
java.security.InvalidKeyException: Parameters missing
at com.sun.crypto.provider.SunJCE_f.a(DashoA13*..)
at com.sun.crypto.provider.AESCipher.engineInit(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at Securitytest.decrypt(Securitytest.java:170)
at Securitytest.byteConvert(Securitytest.java:116)
at Securitytest.main(Securitytest.java:227)
[/b]

i have have read about IV parameters being needed for AES but can someone please explain to me what this is and why it is needed, also how and where i would add it into the following code?

cheers
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import javax.imageio.stream.FileImageInputStream;
import java.security.*;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.*;
import java.lang.*;
 
 
 
public class Securitytest{
 
public static void imageConvert( ) throws Exception {
try{
System.out.println("please select the file to encrypt");
BufferedReader fileInput = new BufferedReader(new InputStreamReader(System.in));
String filetoread = null;
filetoread = fileInput.readLine();
 
System.out.println("please select the file to save to");
BufferedReader fileOutput = new BufferedReader(new InputStreamReader(System.in));
String filetostore = null;
filetostore = fileOutput.readLine();
 
FileImageInputStream input = new FileImageInputStream(new File(filetoread));
ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(output);
byte[] buffer = new byte[1024];
byte[] data = null;
 
int numberBytesRead = 0;
while ((numberBytesRead = input.read(buffer)) != -1)
{
output.write(buffer, 0, numberBytesRead);
}
 
data = output.toByteArray();
 
System.out.println("encrytped" + buffer);
System.out.println("going to encrypt");
byte[] imageOutput = encrypt(data);
System.out.println("back to imageconvert");
System.out.println("encrytped" + buffer);
 
FileOutputStream imageOut = new FileOutputStream(new File(filetostore));
imageOut.write(imageOutput,0,imageOutput.length);
imageOut.close();
 
BASE64Encoder encoder = new BASE64Encoder();
String enc = encoder.encodeBuffer(imageOutput);
FileWriter fw = new FileWriter(filetostore);
fw.write(enc);
fw.close();
 
System.out.println("sent to file");
output.close();
input.close();
 
} catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}


public static void byteConvert()throws Exception{
    try{

System.out.println("please select the file to decrypt");
BufferedReader fileInput = new BufferedReader(new InputStreamReader(System.in));
String filetoread = null;
filetoread = fileInput.readLine();

System.out.println("please select the file to save to");
BufferedReader fileOutput = new BufferedReader(new InputStreamReader(System.in));
String filetostore = null;
filetostore = fileOutput.readLine();

FileInputStream fis = new FileInputStream(filetoread);
byte[] decrypt = new byte[fis.available()];
fis.read(decrypt);
fis.close();

String dec = new String(decrypt);

BASE64Decoder decoder = new BASE64Decoder();
byte[] byteSeed = decoder.decodeBuffer(dec);


// FileInputStream input = new FileInputStream(new File(filetoread));
// ByteArrayOutputStream inStream = new ByteArrayOutputStream();
// PrintStream stream = new PrintStream(inStream);
// PrintStream stream = new PrintStream(input);

// BASE64Decoder decoder = new BASE64Decoder();
// String dec = decoder.decodeBuffer(stream);

// String dec = new String(stream);

// byte[] decryptionBytes = inStream;
// byte[] s = decrypt(dec);
// decryptionBytes =(byte [])stream.readStream();
// stream.close();

// for(int i=0; i < decryptionBytes.length; i++)
    // System.out.print(decryptionBytes);


byte[] imageOutput = decrypt(byteSeed);
//byte[] imageOutput = decrypt(decrypt);

FileOutputStream imageOut = new FileOutputStream(new File(filetostore));
imageOut.write(imageOutput,0,imageOutput.length);
imageOut.close();

    }catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}





 
//new being used for AES
public static byte[] encrypt(byte[] imageBytes) throws Exception {
 
    
byte[] encrypte = new byte[imageBytes.length];
//System.out.println("Imagebyte length: " + imageBytes.length);


System.out.println("in encrypt");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKey secretKeySpec = AESkeyGeneration();
//IvParameterSpec ivSpec = new IvParameterSpec();
//Cipher.getIV()
System.out.println("read out" + secretKeySpec);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
//byte[] encrypted = cipher.doFinal(imageBytes);
return cipher.doFinal(imageBytes);
//return encrypted;
}
 



public static byte[] decrypt(byte[] bytesIn) throws Exception{

System.out.println("in decrypt");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
System.out.println("cipher instance");
SecretKey secretKeySpec = AESkeyGeneration();
System.out.println("keyRead in");
    
    

//PrivateKey prvk = loadPrvkey();

System.out.println("read out" + secretKeySpec);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
System.out.println("Decrypt mode");

return cipher.doFinal(bytesIn);

//byte[] decodeData= cipher.doFinal(bytesIn);
//return decodeData;
}








private static SecretKeySpec AESkeyGeneration() {
 SecretKeySpec secretKeySpec = null; 
try {
   
 KeyGenerator kgen = KeyGenerator.getInstance("AES");
 kgen.init(128);
 SecretKey skey = kgen.generateKey();
 byte[] key = skey.getEncoded();
 secretKeySpec = new SecretKeySpec(key, "AES");
 
}catch(Exception e) {
  System.out.println("error in keygen = "+e);
}
  return secretKeySpec;
}
 
public static void main (String[] unused) throws Exception {
 
System.out.println("***********************************************");
System.out.println("***    Biometric Encrytion Program          ***");
System.out.println("***********************************************");
 
System.out.println("Please select a process");
System.out.println("e for encryption ");
System.out.println("d for decrytion");
 
boolean choose = false;
 
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
String choice = null;
choice = userInput.readLine();
        
if (choose = choice.equals("e")){
    
imageConvert();
    
System.out.println("Encryption");
}
else 
System.out.println("Decryption"); 
    
byteConvert();
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 14 2007
Added on Apr 16 2007
1 comment
899 views