Results 1 to 5 of 5
  1. #1
    nikhil2050 is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Lightbulb Image Encryption Using RC5

    Hi friends. I've implemented :
    1. "RC5 code for encoding 2 bytes in Java" and other
    2. "Blowfish Image Encryption".
    Can anyone please help me for implementing "Image Encryption using RC5" ?
    I'm attaching both codes txt file...
    Attached Files Attached Files
    Last edited by nikhil2050; 11-23-2013 at 11:38 AM.

  2. #2
    nikhil2050 is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Image Encryption Using RC5

    Hi friends. I've implemented :
    1. "RC5 code for encoding 2 bytes in Java" and other
    2. "Blowfish Image Encryption".
    Can anyone please help me for implementing "Image Encryption using RC5" ? I'm attaching both codes txt file...
    Attached Files Attached Files

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Image Encryption Using RC5

    Please post any code you have questions about here on the forum.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    nikhil2050 is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Re: Image Encryption Using RC5

    I want to encrypt image file instead of integers using RC5 algorithm.
    I know its simple but not getting it.

    Java Code:
    /*************************RC5 2 bytes Encryption Decryption**********************************/
    
    package b_MainRC5;
    
    class Main_RC5{
        public static final int w = 32;
        public static final int r = 12;
        public static final int b = 16;
        public static final int c = 4;
        public static final int t = 26;
        
        public int[] S = new int[t]; // expanded key table
        public int P = (int)0xb7e15163; // magic constants
        public int Q = (int)0x9e3779b9;
        
        void RC5_ENCRYPT(int[] pt, int[] ct){ // 2 WORD input pt/output ct
            int i;
            int A = pt[0] + S[0];
            int B = pt[1] + S[1];
            for (i = 1; i <= r; i++){
                A = (((A ^ B) << (B & (w - 1))) | ((A ^ B)>>>(w - (B & (w - 1))))) + S[2 * i];
                B = (((B ^ A) << (A & (w - 1))) | ((B ^ A)>>>(w - (A & (w - 1))))) + S[2 * i + 1];
            }
            ct[0] = A;
            ct[1] = B;
        }
    
        void RC5_DECRYPT(int[] ct, int[] pt){ // 2 WORD input ct/output pt
            int i;
            int B = ct[1];
            int A = ct[0];
            for (i = r; i > 0; i--){
                B = (((B - S[2 * i + 1])>>>(A & (w - 1))) | ((B - S[2 * i + 1]) << (w - (A & (w - 1))))) ^ A;
                A = (((A - S[2 * i])>>>(B & (w - 1))) | ((A - S[2 * i]) << (w - (B & (w - 1))))) ^ B;
            }
            pt[1] = B - S[1];
            pt[0] = A - S[0];
        }
    
        void RC5_SETUP(byte[] K){ // secret input key K[0...b-1]
            int i;
            int j;
            int k;
            int u = w / 8;
            int A;
            int B;
            int[] L = new int[c];
            /* Initialize L, then S, then mix key into S */
            for (i = b - 1,L[c - 1] = 0; i != -1; i--){
                L[i / u] = (L[i / u] << 8) + K[i];
            }
            for (S[0] = P,i = 1; i < t; i++) {
                S[i] = S[i - 1] + Q;
            }
            for (A = B = i = j = k = 0; k < 3 * t; k++,i = (i + 1) % t,j = (j + 1) % c) {// 3*t > 3*c
                A = S[i] = (((S[i] + (A + B)) << (3 & (w - 1))) | ((S[i] + (A + B))>>>(w - (3 & (w - 1)))));
                B = L[j] = (((L[j] + (A + B)) << ((A + B) & (w - 1))) | ((L[j] + (A + B))>>>(w - ((A + B) & (w - 1)))));
            }
        }
        
        public static String hex(int n) {
            // call toUpperCase() if that's required
            //return String.format("0x%8s", Integer.toHexString(n)).replace(' ', '0');
              return String.format("0x%s", Integer.toHexString(n)).replace(' ', '0');
        }
    
    //    byte[] myEncrypt(int n){
    //        int i;
    //        int j;
    //        int[] pt1 = new int[2];        //*
    //        int[] pt2 = new int[2];        //*
    //        int[] ct = {0,0};            //*
    //        byte[] key = new byte[b];    //*
    //        
    //          pt1[0] = 0;//ct[0];
    //          pt1[1] = n;//ct[1];
    //
    //          for (j = 0;j < b;j++){
    ////              key[j] = (byte) (ct[0] % (255 - j));
    //              key[j] = (byte) (ct[0] % (255 - 199));
    //          }
    //          /* Setup, encrypt, and decrypt */
    //          RC5_SETUP(key);
    //          RC5_ENCRYPT(pt1, ct);
    //          RC5_DECRYPT(ct, pt2);
    //
    //          /* Print out results, checking for decryption failure */
    ////          System.out.printf("\n%d. key = ",i);
    //          System.out.printf("\n key = ");
    //          for (j = 0; j < b; j++){
    //              System.out.print(hex(key[j])+" ");
    //          }
    //          System.out.println("\n   plaintext:"  + pt1[1] +   " --->" +  "ciphertext:" + ct[0]+ct[1]);
    //          System.out.println("   Decypted:"  + pt2[1] );
    //          //System.out.printf("\n   Decrypted plaintext: (%.8lX %.8lX) \n",pt2[0], pt2[1]);
    //
    //          if (pt1[0] != pt2[0] || pt1[1] != pt2[1]){
    //              System.out.print("Decryption Error!");
    //          }
    //        //time (&t0);
    //            //RC5_ENCRYPT(ct, ct);
    //          return (key);            
    //    }
    //    
    //    void myDecrypt(byte[] key){
    ////        int[] pt2 = new int[2];        //*
    ////    RC5_SETUP(key);
    ////        RC5_DECRYPT(ct, pt2);
    //
    //    }
        
        
        public static void main(String[] args){
            int i;
            int j;
            int[] pt1 = new int[2];
            int[] pt2 = new int[2];
            int[] ct = {9,7};
            byte[] key = new byte[b];
            
            //time_t t0, t1;
            Main_RC5 h=new  Main_RC5();
            
            System.out.print("RC5-32/12/16 examples:\n");
            for (i = 1;i < 6;i++){ // Initialize pt1 and key pseudorandomly based on previous ct
              pt1[0] = ct[0];
              pt1[1] = ct[1];
    
              for (j = 0;j < b;j++){
                  key[j] = (byte) (ct[0] % (255 - j));
              }
              /* Setup, encrypt, and decrypt */
              h.RC5_SETUP(key);
              h.RC5_ENCRYPT(pt1, ct);
              h.RC5_DECRYPT(ct, pt2);
    
              /* Print out results, checking for decryption failure */
              System.out.printf("\n%d. key = ",i);
              for (j = 0; j < b; j++){
                  System.out.print((key[j])+" ");
              }
              System.out.println("\n   Plaintext:"  + pt1[0]  + pt1[1] +   " --->" +  "ciphertext:" + ct[0]+ct[1]);
              System.out.println("   Decypted:"  + pt2[0]  + pt2[1] );
    
              if (pt1[0] != pt2[0] || pt1[1] != pt2[1]){
                  System.out.print("Decryption Error!");
              }
            }
        }
    }



    Java Code:
    /***************BlowFish Image Encryption*****************/
    
    package b_pkg;
    /**
     * package;
     * package com.java.blowfish;  
    **/
    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 Nikhil Kshirsagar*/  
    public class MyImageEncryption {  
    	
    	KeyGenerator keyGenerator = null;  
    	SecretKey secretKey = null;  
    	Cipher cipher = null;  
    	
    	public MyImageEncryption() {  
    	     try {  
    	         keyGenerator = KeyGenerator.getInstance("Blowfish");  
    	         secretKey = keyGenerator.generateKey();  
    	
    	         cipher = Cipher.getInstance("Blowfish");  
    	     } catch (NoSuchPaddingException ex) {  
    	         System.out.println(ex);  
    	     } catch (NoSuchAlgorithmException ex) {  
    	         System.out.println(ex);  
    	     }  
    	 }  
    		
    	 /** @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 */  			//main Encryption part
    	         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 */  			//main Decryption part
    	         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);  
    	     }  
    	 }  
    	 
    	 public static void main(String[] args) {  
    		 String fileToEncrypt = "team.jpg";  
    		 String encryptedFile = "enc.jpg";  
    		 String decryptedFile = "dec.jpg";  
    		 String directoryPath = "D:/TEMP/";  
    		 
    		 MyImageEncryption encryptFile = new MyImageEncryption();  
    		 System.out.println("Starting Encryption...");  
    		 encryptFile.encrypt(directoryPath + fileToEncrypt,directoryPath + encryptedFile);  
    		 System.out.println("Starting Decryption...");  
    		 encryptFile.decrypt(directoryPath + encryptedFile,directoryPath + decryptedFile);  
    
    		 System.out.println("Program end...");  
    	 }  
    }
    Last edited by nikhil2050; 11-24-2013 at 01:17 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Image Encryption Using RC5

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    What questions do you have about the code?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. selective image encryption
    By anjalirag in forum New To Java
    Replies: 1
    Last Post: 04-20-2013, 06:28 AM
  2. AES encryption
    By syst3mboot in forum Advanced Java
    Replies: 0
    Last Post: 09-05-2011, 01:52 AM
  3. Encryption
    By Martin1512 in forum Advanced Java
    Replies: 0
    Last Post: 05-02-2011, 07:25 PM
  4. Reg: Image Encryption and Decryption in PGP
    By Deepa in forum New To Java
    Replies: 0
    Last Post: 07-13-2009, 11:13 AM
  5. Image Encryption
    By bugmenot in forum New To Java
    Replies: 3
    Last Post: 04-20-2009, 03:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •