Enkripsi Citra menggunakan javax.crypto


kali ini kita akan mencoba mengenkripsi citra menggunakan javax.crypto, beberapa library lainnya juga kita gunakan untuk menunjanng agar data berupa image dapat di enkripsi. Salah satu source code yang tersedia di internet adalah sebagai berikut http://www.theinsanetechie.in/2011/09/java-data-encryption-standard-des.html

package dataencryptionstandart;

import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.security.InvalidKeyException;  
import java.security.NoSuchAlgorithmException;  
import javax.crypto.BadPaddingException;  
import javax.crypto.Cipher;  
import javax.crypto.IllegalBlockSizeException;  
import javax.crypto.KeyGenerator;  
import javax.crypto.NoSuchPaddingException;  
import javax.crypto.SecretKey;  
  
/** 
 * 
 * @author dhanoopbhaskar 
 */  
public class desimage {  
  
    KeyGenerator keyGenerator = null;  
    SecretKey secretKey = null;  
    Cipher cipher = null;  
  
    public desimage() {  
        try {   
            keyGenerator = KeyGenerator.getInstance("DES");  
            secretKey = keyGenerator.generateKey();  
            cipher = Cipher.getInstance("DES");  
        } catch (NoSuchPaddingException ex) {  
            System.out.println(ex);  
        } catch (NoSuchAlgorithmException ex) {  
            System.out.println(ex);  
        }  
    }  
  
    public static void main(String[] args) {  
        String fileToEncrypt = "5.png";  
        String encryptedFile = "encryptedFile.png";  
        String decryptedFile = "decryptedFile.png";  
        String directoryPath = "/media/ratjoen/javaFiles/Izhobrazhenie_KriptografiyV3.0L/";  
        desimage encryptFile = new desimage();  
        System.out.println("Starting Encryption...");  
        encryptFile.encrypt(directoryPath + fileToEncrypt,  
                directoryPath + encryptedFile);  
        System.out.println("Encryption completed...");  
        System.out.println("Starting Decryption...");  
        encryptFile.decrypt(directoryPath + encryptedFile,  
                directoryPath + decryptedFile);  
        System.out.println("Decryption completed...");  
    }  
  
    /** 
     *  
     * @param srcPath 
     * @param destPath 
     * 
     * Encrypts the file in srcPath and creates a file in destPath 
     */  
    private void encrypt(String srcPath, String destPath) {  
        File rawFile = new File(srcPath);  
        File encryptedFile = new File(destPath);  
        InputStream inStream = null;  
        OutputStream outStream = null;  
        try {  
            /** 
             * Initialize the cipher for encryption 
             */  
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);  
            /** 
             * Initialize input and output streams 
             */  
            inStream = new FileInputStream(rawFile);  
            outStream = new FileOutputStream(encryptedFile);  
            byte[] buffer = new byte[1024];  
            int len;  
            while ((len = inStream.read(buffer)) > 0) {  
                outStream.write(cipher.update(buffer, 0, len));  
                outStream.flush();  
            }  
            outStream.write(cipher.doFinal());  
            inStream.close();  
            outStream.close();  
        } catch (IllegalBlockSizeException ex) {  
            System.out.println(ex);  
        } catch (BadPaddingException ex) {  
            System.out.println(ex);  
        } catch (InvalidKeyException ex) {  
            System.out.println(ex);  
        } catch (FileNotFoundException ex) {  
            System.out.println(ex);  
        } catch (IOException ex) {  
            System.out.println(ex);  
        }  
    }  
  
    /** 
     *  
     * @param srcPath 
     * @param destPath 
     * 
     * Decrypts the file in srcPath and creates a file in destPath 
     */  
    private void decrypt(String srcPath, String destPath) {  
        File encryptedFile = new File(srcPath);  
        File decryptedFile = new File(destPath);  
        InputStream inStream = null;  
        OutputStream outStream = null;  
        try {  
            /** 
             * Initialize the cipher for decryption 
             */  
            cipher.init(Cipher.DECRYPT_MODE, secretKey);  
            /** 
             * Initialize input and output streams 
             */  
            inStream = new FileInputStream(encryptedFile);  
            outStream = new FileOutputStream(decryptedFile);  
            byte[] buffer = new byte[1024];  
            int len;  
            while ((len = inStream.read(buffer)) > 0) {  
                outStream.write(cipher.update(buffer, 0, len));  
                outStream.flush();  
            }  
            outStream.write(cipher.doFinal());  
            inStream.close();  
            outStream.close();  
        } catch (IllegalBlockSizeException ex) {  
            System.out.println(ex);  
        } catch (BadPaddingException ex) {  
            System.out.println(ex);  
        } catch (InvalidKeyException ex) {  
            System.out.println(ex);  
        } catch (FileNotFoundException ex) {  
            System.out.println(ex);  
        } catch (IOException ex) {  
            System.out.println(ex);  
        }  
    }  
}  

namun hasil yang diharapkan kurang sesuai dengan yang saya inginkan dikarenakan hasil enkripsi tidak dapat dibuka
Screenshot from 2016-01-21 15:55:16

mengapa hal tersebut terjadi, kita bisa lihat detailnya dalam hex menggunakan xxd pada linux command

File asli:
Screenshot from 2016-01-21 15:56:37

File terenkripsi
Screenshot from 2016-01-21 15:58:09

dari capture hex tersebut kita bisa lihat di baris pertama bahwa 8950 yang merupakan extensi dari .PNG juga ikut terenkripsi menjadi 0eda hal tersebut menyebabkan tidak dapat nya komputer mendisplay citra yang telah terenkripsi. namun saat sudah file yang telah terdekripsi dapat terdisplay karena hex kembali seperti semula

bagaimana caranya agar kita tidak mengenkripsi file format header, tetapi hanya nilai pixel nya?

Advertisements

Author: ridhobustami

orang ngak jelas

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s