Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Image Encryption Using RC5

Er.NikKsagarNov 23 2013

Hi everyone. 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...


/*************************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!");

   }

  }

  }

}

Blowfish Image Encryption 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..."); 

  } 

}

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 21 2013
Added on Nov 23 2013
0 comments
1,430 views