2014-10-29 2 views
0

Java를 사용하여 이미지를 암호화해야합니다. 제대로 이미지를 암호화하지만 이미지를 너무 열거 나 파일이 손상되었다고 알려주므로 파일을 시각화하는 방법을 모르겠습니다. 사진 본문 및 메타 데이터없이 작업하려면 어떻게해야합니까?JAVA : 이미지를 암호화하고 보는 방법

감사합니다.

// Scanner to read the user's password. The Java cryptography 
    // architecture points out that strong passwords in strings is a 
    // bad idea, but we'll let it go for this assignment. 
    Scanner scanner = new Scanner(System.in); 
    // Arbitrary salt data, used to make guessing attacks against the 
    // password more difficult to pull off. 
    byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, 
      (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 }; 

    { 
     File inputFile = new File("C:/Users/Julio/Documents/charmander.png"); 
     BufferedImage input = ImageIO.read(inputFile); 
     Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
     SecretKeyFactory keyFac = SecretKeyFactory 
       .getInstance("PBEWithMD5AndDES"); 
     // Get a password from the user. 
     System.out.print("Password: "); 
     System.out.flush(); 
     PBEKeySpec pbeKeySpec = new PBEKeySpec(scanner.nextLine() 
       .toCharArray()); 
     // Set up other parameters to be used by the password-based 
     // encryption. 
     PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20); 
     SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); 
     // Make a PBE Cyhper object and initialize it to encrypt using 
     // the given password. 
     Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); 
     pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); 
     FileOutputStream output = new FileOutputStream(
       "C:/Users/Julio/Documents/imgen.png"); 
     CipherOutputStream cos = new CipherOutputStream(output, pbeCipher); 
     // File outputFile = new File("image.png"); 
     ImageIO.write(input, "PNG", cos); 
     cos.close(); 

    } 
} 
+0

출력 비트를 새로운'.png' 파일에 직접 쓰고 있습니다. 아마도 올바른 메타 데이터를 작성해야하며 그런 다음 암호화 된 데이터를 작성해야합니까? '.png'에는 [여기] (http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html)에서 볼 수있는 표준이 있습니다. – christopher

+0

각 픽셀의 색상 코드를 단순히 바이트로 지정하는 원시 이미지의 경우이 방법을 사용하는 것이 더 쉽습니다 (예 : [.bmp 형식] (http://en.wikipedia.org/wiki/BMP_file_format). –

답변

1

전체 출력 파일을 암호화하고 있습니다. 물론 암호화 된 형식은 재구성 가능한 이미지 파일 형식이 아닙니다. 암호화 된 것이라면 조금 무의미합니다.

이미지 데이터를 암호화하고 올바른 이미지로 쓰는 것이 좋습니다. 그러면 이미지에 임의의 픽셀 만 표시됩니다. 원칙적으로 이것은 훨씬 복잡하지 않습니다. 읽은 이미지의 픽셀에 암호화를 적용한 다음 해당 이미지를 정상적으로 작성하십시오.

실제로는 사이퍼가 스트림을 사용하도록 설계 되었기 때문에 이미지가 그래픽 처리에 적합한 API를 갖기 때문에 실제로는 간단하지 않습니다. 사물을 좀 더 복잡하게 만들려면 다양한 유형의 이미지를 표현하기 위해 내부적으로 사용되는 다양한 표현이 필요합니다.

그래서 번역 할 코드를 써야합니다. 이미지에서 픽셀을 가져 오는 것 (예 : getRGB (x, y) 메서드 사용), 픽셀 데이터를 바이트로 분해합니다. 그것을 암호 자로 먹여라. 암호화 된 데이터를 다시 픽셀로 변환합니다 (예 : setRGB (x, y, rgb) 사용).

+0

즉, DES와 MD5를 사용하면 헤더를 해독하기 위해 암호를 빠르게 무차별 적으로 실행할 수 있습니다. P –

관련 문제