1
public class Symmetric1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception{
        // TODO code application logic here
        KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(new SecureRandom());
    SecretKey secretKey = kg.generateKey();

    FileInputStream inFile = new FileInputStream("C:/Users/Administrator/Desktop/original.bmp");

    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);

    FileOutputStream outFile = new FileOutputStream("C:/Users/Administrator/Desktop/ECB_original.bmp");

    byte[] input = new byte[64];
    int bytesRead;
    while((bytesRead = inFile.read(input)) != -1){
        byte[] output = cipher.update(input,0,bytesRead);
        if(output != null)
            outFile.write(output);
    }

    byte[] output = cipher.doFinal();
    if(output != null)
        outFile.write(output);

    inFile.close();
    outFile.flush();
    outFile.close();
    }
}

i learned that the ecb encryption make the picture a little misty... but my code make the original picture quite encrypted like CBC Encryption. what's the matter?

Here is cbc encrypt code too.

public class Symmetric2 {

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub

        FileInputStream inFile = new FileInputStream("C:/Users/Administrator/Desktop/original.bmp");
        FileOutputStream outFile = new FileOutputStream("C:/Users/Administrator/Desktop/CBC_original.bmp");

        KeyGenerator kg = KeyGenerator.getInstance("DES");
        kg.init(new SecureRandom());

        SecretKey sk = kg.generateKey();

        Cipher cp = Cipher.getInstance("DES/CBC/PKCS5Padding");

        byte[] ivBytes = new byte[]{
                0x00, 0x01,0x02, 0x03, 0x00, 0x00, 0x00, 0x01       };
        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

        cp.init(Cipher.ENCRYPT_MODE, sk, ivSpec); //CBC방식이므로 인자가 3개이다.

        byte[] input = new byte[64]; //getBytes() 
        int bytesRead;
        while((bytesRead = inFile.read(input)) != -1){
            byte[] output = cp.update(input,0,bytesRead);
            if(output != null)
                outFile.write(output);
        }
        byte[] output = cp.doFinal();
        if(output != null)
            outFile.write(output);

        inFile.close();
        outFile.flush();
        outFile.close();
    }

}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
ByungWuun
  • 11
  • 1
  • Can you give some examples? – Artjom B. Mar 26 '17 at 10:32
  • 2
    CBC with a fixed IV gives results very similar to ECB for the first block. More differences will appear in the second block. And DES is obsolete now, use AES instead. – rossum Mar 26 '17 at 10:58

1 Answers1

2

The commonly known pinguin picture kinda plays dirty. It only encrypts the bitmap itself, while the BMP file also contains meta data. Furthermore it relies on the right alignment of the data.

It certainly isn't the case that you get the perfect result you'd expect if you see these examples. You should see such a picture as an example to prove the point.

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263