2017-01-06 9 views
0

画像の内容を暗号化して、画像ビューアで暗号化されているのがわかります。AESアルゴリズムを使用した画像暗号化

これはイメージのピクセル値を暗号化しようとしましたが、RGB値は意味します。

だから私がやったことはすべて、次のとおりです。

1-画像から全てのRGB値を取得します。

2-ストア全てのRGB値AES入力暗号化のためのバイト配列に整数アレイ

3-コンバート整数配列に変換します。

4-暗号化とカバレッジの出力を整数配列にします。

5-新しいRGB値を新しい整数配列から設定します。

しかし、AESアルゴリズムの出力値が大きすぎるため、出力イメージが表示されませんでした.255より大きく、RGB値は0-255の間でなければなりません。

public class img { 
     static String IV = "AAAAAAAAAAAAAAAA"; 
     static String encryptionKey = "abcdef"; 

static public void main(String args[]) throws Exception { 
    try { 
     BufferedImage image; 
     int width; 
     int height; 

     File input = new File("C:\\Users\\AKRAM\\Desktop\\sample.jpg"); 
     image = ImageIO.read(input); 
     width = image.getWidth(); 
     height = image.getHeight(); 

     int[] t = new int[width * height * 3]; 
     int k = 0; 
     int kk = 0; 

     // fill the table t with RGB values; 
     for (int i = 0; i < height; i++) { 

      for (int j = 0; j < width; j++) { 

       Color c = new Color(image.getRGB(j, i)); 
       int r = c.getRed(); 
       int g = c.getGreen(); 
       int b = c.getBlue(); 

       t[k] = r; 
       k++; 
       t[k] = g; 
       k++; 
       t[k] = b; 
       k++; 

      } 
     } 

     // convert table of RGB values into byte Array for the Encryption 
     byte[] bb = integersToBytes(t); 

     /* AES Encryption */ 
     byte[] cipher = encrypt(bb, encryptionKey); 

     t = convertByte2Int(cipher); 

     // create image with table RGB values; 
     for (int i = 0; i < height; i++) { 

      for (int j = 0; j < width; j++) { 

       int r = t[kk]; 
       kk++; 
       int g = t[kk]; 
       kk++; 
       int b = t[kk]; 
       kk++; 

       Color newColor = new Color(r, g, b); 
       image.setRGB(j, i, newColor.getRGB()); 

      } 
     } 
     //write the output image 
     File ouptut = new File("C:\\Users\\AKRAM\\Desktop\\output.jpg"); 
     ImageIO.write(image, "jpg", ouptut); 

    } catch (Exception e) { 
    } 
}// end main 

public static byte[] encrypt(byte[] plainText, String encryptionKey) throws Exception { 
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE"); 
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES"); 
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8"))); 
    return cipher.doFinal(plainText); 
} 

public static byte[] integersToBytes(int[] values) throws IOException { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    DataOutputStream dos = new DataOutputStream(baos); 
    for (int i = 0; i < values.length; ++i) { 
     dos.writeInt(values[i]); 
    } 

    return baos.toByteArray(); 
} 

public static int[] convertByte2Int(byte buf[]) { 
    int intArr[] = new int[buf.length/4]; 
    int offset = 0; 
    for (int i = 0; i < intArr.length; i++) { 
     intArr[i] = (buf[3 + offset] & 0xFF) | ((buf[2 + offset] & 0xFF) << 8) | ((buf[1 + offset] & 0xFF) << 16) 
       | ((buf[0 + offset] & 0xFF) << 24); 
     offset += 4; 
    } 
    return intArr; 
    } 

}

+0

これをやって、JPGファイル全体をバイト配列に読み込んで暗号化しないのはなぜですか?また、整数としてRGBを格納し、バイトに変換するあなたの方法はawfullです。 getRed()などをバイトに直接変換してみませんか?返される値はドキュメントの0-255に従っていますので、このintを直接バイトに変換することができます。 – kulatamicuda

+0

あなたは何をしようとしているのか分かりません。 **あなたは**暗号化された**画像を見るつもりですか?データは図形的に表現できません – pedrofb

+0

@kulatamicuda私は画像全体ではなくコンテンツ画像を暗号化したい、私は画像ビューアでそれを見たい、会話について私はプログラミングの世界に初心者ですので、簡単にそれを取ってください! THX –

答えて

2

これはあなたに、私は願って少しのに役立ちます。それはすべてを(私たちはあなたの学校の宿題を作るためにここにはない)行いませんが、あなたが立ち往生したものであなたを助けます。元のコードと比較して、どこでミスをしたのかを理解してください(複数ある)。

package kulatamicuda.aesimage.core; 

import java.awt.Color; 
import java.awt.image.BufferedImage; 
import java.io.File; 

import javax.imageio.ImageIO; 

/** 
* Sample class for Stacko. 
* 
* @author kulatamicuda 
* 
*/ 
public final class Img { 

    /** 
    * RGB SIZE IS 3 (RED, GREEN, BLUE). 
    */ 
    private static final int RGB_SIZE = 3; 

    /** 
    * Byte shifter for SIGNED->UNSIGNED. 
    */ 
    private static final int BSHIFT = 0xFF; 

    /** 
    * Solution sample in main. 
    * 
    * @param args 
    *   ignored args 
    */ 
    public static void main(String[] args) { 
    try { 
     BufferedImage image; 
     int width; 
     int height; 

     File input = new File("sample.jpg"); 
     image = ImageIO.read(input); 
     width = image.getWidth(); 
     height = image.getHeight(); 

     byte[] t = new byte[width * height * RGB_SIZE]; 
     int index = 0; 

     // fill the table t with RGB values; 
     for (int i = 0; i < height; i++) { 

     for (int j = 0; j < width; j++) { 

      Color c = new Color(image.getRGB(j, i)); 

      // As byte is SIGNED in Java overflow will occur for values > 127 
      byte r = (byte) c.getRed(); 
      byte g = (byte) c.getGreen(); 
      byte b = (byte) c.getBlue(); 

      t[index++] = r; 
      t[index++] = g; 
      t[index++] = b; 
     } 
     } 

     // Re-create image with table-encrypted RGB values 
     BufferedImage newImage = new BufferedImage(width, height, 
      BufferedImage.TYPE_3BYTE_BGR); 
     index = 0; 
     for (int i = 0; i < height; i++) { 

     for (int j = 0; j < width; j++) { 

      // Need to deal with values < 0 so binary AND with 0xFF 
      // Java 8 provides Byte.toUnsignedInt but I am from the old school ;-) 
      int r = t[index++] & BSHIFT; 
      int g = t[index++] & BSHIFT; 
      int b = t[index++] & BSHIFT; 

      Color newColor = new Color(r, g, b); 
      newImage.setRGB(j, i, newColor.getRGB()); 

     } 
     } 
     // write the output image 
     File output = new File("output.jpg"); 
     ImageIO.write(newImage, "jpg", output); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 

} 
+0

あなたの助けを借りて、暗号化と解読が成功しました。 画像のサイズが変わったのはなぜですか?私はオリジナルの画像が44 Koだったことを意味し、暗号化と復号化の後、画像サイズはちょうど26 Koに減少しましたか?どちらの画像も同じように見えます... –

+0

@AkramKaram質問の最後のコメントをもう一度お読みください。 – kulatamicuda

+0

申し訳ありませんが、私は完全にそれを手に入れる原則(キス):D –

関連する問題