Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.4K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 546 SQLcl
- 4K SQL Developer Data Modeler
- 187.1K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 442 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Image Encryption Using RC5

Er.NikKsagar
Member Posts: 1
in Cryptography
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 & (w - 1))) | ((A ^
>>>(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 +
) << (3 & (w - 1))) | ((S[i] + (A +
)>>>(w - (3 & (w - 1))))); B = L[j] = (((L[j] + (A +
) << ((A +
& (w - 1))) | ((L[j] + (A +
)>>>(w - ((A +
& (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..."); } }
Tagged:
This discussion has been closed.