我想創建一個非常簡單的加密/解密項目。但起初,我想讀的JPG文件,並將其寫入與給定的密碼文件,然後再次讀取該文件與文件和所提供的密碼檢查密碼,但我得到:圖像讀寫java.lang.IllegalArgumentException
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)
我的代碼:
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;
}
我的加密:
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());
}
}
和我的解密:
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;
}
和AES.java:105是:
ImageIO.write(imagRecover, "jpg", new File(encryptedFileDir,outputRecoverdFileName));
我檢查imagRecover
爲空,但我不知道爲什麼?我認爲你可以嘗試它只是命名您的圖像文件,如1.jpg,2.jpg等...
你寫binnary數據。你不能保證圖像的二元組沒有結束。可能有很多carre返回和線內行結束......你期望的每個linea是完全conatructed圖像使imageio.read失敗並返回null,如javadoc中針對該方法所述。在寫入之前,我要求你對base64進行編碼,並將這個文件作爲文本文件與printwriter對待。否則,你應該嘗試一種類似thoae的WAD文件在good'ol厄運,節省每個文件的poaition和長度。 – eduyayo 2014-09-04 22:06:44
Btw ...你有沒有讀過java.util.zip? – eduyayo 2014-09-04 22:07:17
@eduyayo謝謝它的作品,但爲什麼我會失去質量?例如我的原始圖像是551kB,但我的恢復是181KB。我用Baase64編碼解碼方法來寫和讀。 – mmlooloo 2014-09-05 02:36:37