hi guys
please can you help me with the following error that i am getting.
i appear to be encyrpting ok as image a am readin in and the file reading out are the same size.
but i am getting this pading error. been at it all day and cant find the answer to this error.
Exception: javax.crypto.BadPaddingException: Given final block not properly padd
ed
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at Securitytest.decrypt(Securitytest.java:208)
at Securitytest.byteConvert(Securitytest.java:117)
at Securitytest.main(Securitytest.java:265)
the code i am using is the following. please help me i am going mad! the iv is the same for reading in and out and the secret key appears to be the same.
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);
FileOutputStream fos = new FileOutputStream(filetostore);
fos.write(imageOutput);
fos.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(decrypt);
//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[] iv = new byte[] {(byte)0x2A, (byte)0x98, (byte)0xB2, (byte)0xBA, (byte)0x99, (byte)0xC0,
(byte)0xF1, (byte)0x22, (byte)0x72, (byte)0x54, (byte)0xBC,
(byte)0xB5, (byte)0x34, (byte)0x2F, (byte)0xBA, (byte)0xC4};
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);
//IvParameterSpec iv = new IvParameterSpec(IV_BYTE_ARRAY);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec (iv));
//byte[] encrypted = cipher.doFinal(imageBytes);
FileOutputStream fos = new FileOutputStream("c:/text.txt");
CipherOutputStream cOut = new CipherOutputStream(fos, cipher);
// Your plaintext MUST be multiple of 16 bytes because NoPadding requires your input text
// to be a multiple of block size.
cOut.write(imageBytes);
cOut.close();
return cipher.doFinal(imageBytes);
//return encrypted;
}
public static byte[] decrypt(byte[] bytesIn) throws Exception{
byte[] iv = new byte[] {(byte)0x2A, (byte)0x98, (byte)0xB2, (byte)0xBA, (byte)0x99, (byte)0xC0,
(byte)0xF1, (byte)0x22, (byte)0x72, (byte)0x54, (byte)0xBC,
(byte)0xB5, (byte)0x34, (byte)0x2F, (byte)0xBA, (byte)0xC4};
System.out.println("in decrypt");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
System.out.println("cipher instance");
SecretKey secretKeySpec = AESkeyGeneration();
System.out.println("key" + secretKeySpec);
System.out.println("keyRead in");
//PrivateKey prvk = loadPrvkey();
System.out.println("read out" + secretKeySpec);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec (iv));
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");
FileOutputStream fosKey = new FileOutputStream ("c:/AES.key");
fosKey.write (key);
fosKey.close();
}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();
}
}