i want to create a very simple encrypt/decrypt project. but at first i want to read jpg file and write it to a file with a given password then read that file again and check the password in the file and the provided password but i get:
Exception in thread "main" java.lang.IllegalArgumentException: im == null!
at javax.imageio.ImageIO.write(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)
at GSM.AES.deccryption(AES.java:105)
at GSM.AES.main(AES.java:27)
My codes:
public static void main(String args[])
{
myWrite();
String encryptedFilePath = System.getProperty("user.dir")+ "\\"+"Encrypted"+".mmlooloo";
String destinationFilePath = System.getProperty("user.dir") + "\\";
try {
myRead(encryptedFilePath,destinationFilePath,"123456");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return;
}
My encrypt :
public static void myWrite() {
try {
System.out.println("Plesase Enter Number Of Pages !!!!!");
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
int numberOfPage = Integer.valueOf(bufferRead.readLine().toString());
String dirName= System.getProperty("user.dir")+"\\";
byte[] base64StringEnc;
ByteArrayOutputStream baos=new ByteArrayOutputStream(1000);
FileOutputStream myMatLabFileEnc = null;
String filePath = System.getProperty("user.dir")+ "\\"+"Encrypted"+".mmlooloo";
myMatLabFileEnc = new FileOutputStream (filePath);
String imagFileName;
String imgPathString;
String password = "123456";
myMatLabFileEnc.write(password.getBytes());
myMatLabFileEnc.write("\n".getBytes());
for(int i = 1 ; i<=numberOfPage ;i++)
{
imagFileName = Integer.toString(i) +".jpg";
BufferedImage img=ImageIO.read(new File(dirName,imagFileName));
ImageIO.write(img, "jpg", baos);
baos.flush();
myMatLabFileEnc.write(baos.toByteArray());
myMatLabFileEnc.write("\n".getBytes());
baos.reset();
imgPathString = dirName + imagFileName;
File f = new File(imgPathString);
f.delete();
}
myMatLabFileEnc.close();
baos.close();
return;
} catch (FileNotFoundException ex) {
System.out.println(ex.toString());
}catch(IOException ex){
System.out.println(ex.toString());
}
}
and my decrypt:
public static int myRead(String encryptedfilePath,String encryptedFileDir,String inputPassword) throws FileNotFoundException, IOException{
FileReader encryptedFile=new FileReader(encryptedfilePath);
BufferedReader reader = new BufferedReader(encryptedFile);
String encryptedImag;
String encryptedSavesdPassword = reader.readLine();
byte []encryptedInputPassword = inputPassword.getBytes();
byte []temp = encryptedSavesdPassword.getBytes();
if(!Arrays.equals(temp,encryptedInputPassword)){
return -1;
}
int i = 1;
while((encryptedImag = reader.readLine()) != null){
byte[] bytearray = encryptedImag.getBytes();
BufferedImage imagRecover=ImageIO.read(new ByteArrayInputStream(bytearray));
String outputRecoverdFileName = Integer.toString(i)+"_recoverd.jpg";
ImageIO.write(imagRecover, "jpg", new File(encryptedFileDir,outputRecoverdFileName));
++i;
}
return 1;
}
and AES.java:105 is:
ImageIO.write(imagRecover, "jpg", new File(encryptedFileDir,outputRecoverdFileName));
i checked imagRecover
is null but i do not know why? i think you can try it just name your image files like 1.jpg, 2.jpg and so on ...