Enkripsi Citra menggunakan javax.crypto bagian 2


Setelah kita mencoba membuat enkripsi citra http://wp.me/prw8r-1cR, namun hasil enkripsi tidak dapat dilihat dikarenakan perubahan pada header citra digital yang menyebabkan komputer tidak dapat mendeteksi type file dll.

setidaknya ada 2 alternatif untuk mengatasi masalah tersebut, alternatif pertama adalah enkripsi dilakukan hanya pada byte nilai pixel dan juga enkripsi tidak dilakukan pada file-file header. Cara tersebut sangat cocok bagi type file uncompressed seperti .bmp dikarenakan niai-nilai byte pada pixel berkisar antara 0-255 dan memiliki letak posisi yang jelas pada posisi format byte. sebagai contoh berikut

Screenshot from 2016-01-27 09:13:52

perhatikan pada posisi byte ke 10 dengan nilai 0x36 dalam format .bmp kita dapat mendapatkan posisi awal dari nilai pixel yaitu pada byte ke 36, detailnya bisa dilihat di https://en.wikipedia.org/wiki/BMP_file_format dengan begitu kita dapat mengenkripsi hanya pada nilai pixel nya tanpa mengenkripsi headernya atau informasi lainnya.


import imageEncryption.ImageBMP;
import java.awt.image.BufferedImage;
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;
import javax.imageio.ImageIO;

	public class Des_imagebezheader {
		KeyGenerator keyGenerator = null;  
	    SecretKey secretKey = null;  
	    Cipher cipher = null;  
	  
	    public Des_imagebezheader() {  
	        try {   
	            keyGenerator = KeyGenerator.getInstance("DES");  
	            secretKey = keyGenerator.generateKey();  
	            
	            
	            cipher = Cipher.getInstance("DES/ECB/NoPadding");  
	        } catch (NoSuchPaddingException ex) {  
	            System.out.println(ex);  
	        } catch (NoSuchAlgorithmException ex) {  
	            System.out.println(ex);  
	        }  
	    }  
	    
	    public static void main(String[] args) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException {  
	        String fileToEncrypt = "XING_B24.BMP";  
	        String encryptedFile = "encryptXING_B24.bmp";  
	        String decryptedFile = "decryptXING_B24.bmp";    
	        String directoryPath = "/media/ratjoen/javaFiles/Izhobrazhenie_KriptografiyV3.0L/";  
	        Des_imagebezheader encryptFile = new Des_imagebezheader();  
	        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...");
	    }  
	    
	    private void encrypt(String srcPath, String destPath) throws InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {  
	        File rawFile = new File(srcPath);  
	        File encryptedFile = new File(destPath);  
	        InputStream inStream = null;  
	        OutputStream outStream = null;  
	     
	        cipher.init(Cipher.ENCRYPT_MODE, secretKey);  
	        
	        try {
	        inStream = new FileInputStream(rawFile);
	        outStream = new FileOutputStream(encryptedFile);  

	        int lengfile = (int) rawFile.length() ;
	       
	        byte[] buffer = new byte[lengfile]; 
	        inStream.read(buffer);
	        
	        System.out.println(buffer.length);
	        int startP = buffer[10];//for bmp format
	        System.out.println(startP);
	        
	        int lengthE = lengfile-startP;
	       
	        
	        byte[] bufferE = new byte[lengthE]; 
	       
	        for (int i = 0; i < bufferE.length ; i++){
	        	bufferE[i]=buffer[i+startP];
	        }
	        
	        byte[] bufferP = new byte[lengthE]; 
	        
	        cipher.update(bufferE, 0, lengthE , bufferP, 0);
	        cipher.doFinal(bufferP);  
	        
	        for (int i = 0; i < bufferE.length ; i++){
	        	buffer[i+startP]=bufferP[i];
	        }
	        
	        outStream.write(buffer);  
            outStream.flush();
	       
	        }catch(Exception e){
	        	e.printStackTrace();
	        }
	    }
	           	 
	          
	    private void decrypt(String srcPath, String destPath) throws InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {  
	        File rawFile = new File(srcPath);  
	        File encryptedFile = new File(destPath);  
	        InputStream inStream = null;  
	        OutputStream outStream = null;  
	     
	        cipher.init(Cipher.DECRYPT_MODE, secretKey);  
	        
	        try {
	        inStream = new FileInputStream(rawFile);
	        outStream = new FileOutputStream(encryptedFile);  

	        int lengfile = (int) rawFile.length() ;
	        String Format = rawFile.getName();
	        byte[] buffer = new byte[lengfile]; 
	        inStream.read(buffer);
	        
	        System.out.println(buffer.length);
	        int startP = buffer[10];//for bmp format
	        System.out.println(startP);
	        
	        int lengthE = lengfile-startP;
	       
	        
	        byte[] bufferE = new byte[lengthE]; 
	       
	        for (int i = 0; i < bufferE.length ; i++){
	        	bufferE[i]=buffer[i+startP];
	        }
	        
	        byte[] bufferP = new byte[lengthE]; 
	        
	        cipher.update(bufferE, 0, lengthE , bufferP, 0);
	        cipher.doFinal(bufferP);  
	        
	        for (int i = 0; i < bufferE.length ; i++){
	        	buffer[i+startP]=bufferP[i];
	        }
	        
	        outStream.write(buffer);  
            outStream.flush();
	       
	        }catch(Exception e){
	        	e.printStackTrace();
	        }
	    }        
	}

Screenshot from 2016-01-27 09:34:08

Screenshot from 2016-01-27 09:34:00

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