IMAGE ENCRYPTION AND DECRYPTION USING BLOWFISH
ALGORITHM
A project report submitted to
DR. ANITA X
SCHOOL OF COMPUTER SCIENCE and
ENGINEERING
is a partial fulfillment for the course
CSE1029 –NETWORK SECURITY
Name Reg. No.
JEEVESH NANDAN 20MIA1116
UPADHYAY
CHETAN SRIVASTAVA 20MIA1122
ABSTRACT
With the progress in data exchange by electronic system, the need of
information security has become a necessity. Due to growth of multimedia
application, security becomes an important issue of communication and
storage of images. This report is about encryption and decryption of images
using a secret-key block cipher called 64-bits Blowfish designed to increase
security and to improve performance. This algorithm will be used as a variable
key size up to 448 bits. It employs Feistel network which iterates simple
function 16 times. The blowfish algorithm is safe against unauthorized attack
and runs faster than the popular existing algorithms. The proposed algorithm is
designed and realized using Java.
Keywords:
Block Cipher
Cryptography
Feistel Network
Image Decryption
Image Encryption
INTRODUCTION
Because of the demand for information security, image encryption decryption
has come an important exploration area and it has broad operation prospects.
Image security is of utmost concern as web attacks have come more and more
serious. Image encryption decryption has operations in internet
communication, multimedia systems, medical imaging, telemedicine, military
communication, etc. To make the data secure from colorful attacks and for the
integrity of data we must cipher the data before it's transmitted or stored.
Government, service, fiscal institution, hospitals and private business deals
with nonpublic images about their case( in Hospitals), geographical areas( in
exploration), adversary positions( in defense), product,fiscal status. utmost of
this information is now collected and stored on electronic computers and
transmitted across network to othercomputer.However, case and geographical
If these nonpublic images about adversarypositions.areas fall into the wrong
hands, than such a breach of security could lead to declination of war, wrong
treatment etc. guarding nonpublic images is an ethical and legal demand.
numerous image content encryption algorithms have been proposed similar as
DES, 3DES, blowfish, AES, etc. Blowfish algorithm is largely secured because it
has longer crucial length( more no of crucial size). The main end behind the
design of this offer is to get the stylish security/ performance tradeoff over
exisiting ciphers. To achieve this result we're going to compare different
parameters similar as processing time, bandwidth, correlation, entropy etc of
over mentioned algorithms.
LITERATURE SURVEY
After the survey of various methods used for image encryption we came
through a few of these like Image encryption using AES, DES
DES algorithm using Transportation Cryptography Techniques
Data encryption standard (DES) is a private key cryptography system that
provides the security in communication system but now a days the
advancement in the computational power the DES seems to be weak against
the brute force attacks. To improve the security of DES algorithm the
transposition technique is added before the DES algorithm to perform its
process. If the transposition technique is used before the original DES
algorithm then the intruder required first to break the original DES algorithm
and then transposition technique. So the security is approximately double as
compared to a simple DES algorithm.
Image Encryption Using Block-Based Transformation Algorithm
Here a block-based transformation algorithm based on the combination of
image transformation and a well known encryption and decryption algorithm
called Blowfish is used. The original image was divided into blocks, which were
rearranged into a transformed image using a transformation algorithm
presented here, and then the transformed image was encrypted using the
Blowfish algorithm. The results showed that the correlation between image
elements was significantly decreased by using the proposed technique.
BLOCK DIAGRAM
BLOWFISH ALGORITHM
• Blowfish was designed in 1993 by Bruce Scheier as a fast, alternative to
existing encryption algorithms.
• Blowfish is a symmetric block encryption algorithm designed in
consideration with
1. Fast: it encrypts data on large 32-bit microprocessors at a rate of 26
clock cycles per byte
2. Compact: it can run in less than 5K of memory
3. Simple: it uses addition, XOR, lookup table with 32-bit operands
4. Secure: the key length is variable, it can be in the range of 32~448 bits:
default 128 bits key length
5. It is suitable for applications where the key does not change often, like
communication link or an automatic file encrypter.
6. Unpatented and royalty-free.
7. Symmetric algorithms: use the same key for encryption and decryption
I.METHODOLOGY
I.I DESCRIPTION OF ALGORITHM
Blowfish symmetric block cipher algorithm encrypts block data of 64-bits at a
time. It will follow the Feistel network and this algorithm is divided into two
parts.
i. Key-expansion: It will converts a key of at most 448 bits into several sub key
arrays totaling 4168 bytes
ii. Data-Encryption: It is having a function to iterate 16 times of network. Each
round consists of a keydependent permutation, and a key- and data-
dependent substitution. All operations are XORs and additions on 32-bit words.
The only additional operations are four indexed array data lookup tables for
each round.
I.II Key Generation
• Blowfish uses large number of sub keys. These keys are generating earlier to
any data encryption or decryption.
• The p-array consists of 18, 32-bit sub keys: P1,P2,………….,P18
• Four 32-bit S-Boxes consists of 256 entries each: S1,0, S1,1,………. S1,255 S2,0,
S2,1,……….. S2,255 S3,0, S3,1,……….. S3,255 S4,0, S4,1,………... S4,255
Steps to Generate Sub Keys
1) Initialize first the P-array and then the four S-boxes, in order, with a fixed
string. This string consists of the hexadecimal digits of pi (less the initial 3) .
2) XOR P1 with the first 32 bits of the key, XOR P2 with the second 32-bits of
the key, and so on for all bits of the key (possibly up to P14). Repeatedly cycle
through the key bits until the entire P-array has been XORed with key bits. (For
every short key, there is at least one equivalent longer key; For example, if A is
a 64-bit key, then AA, AAA, etc., are equivalent keys.)
ARCHITECTURE DIAGRAM
Blowfish is a Feistel network consisting of 16 rounds.
The input is a 64-bit data element, x.
Divide x into two 32-bit halves:
xL, xR
For i = 1to 16:
xL = XL XOR Pi
xR = F(XL) XOR xR
Swap XL and Xr
Swap XL and xR (Undo the last swap.)
xR = xR XOR P17
xL = xL XOR P18
Recombine xL and xR
RESULTS
CODE
package encryptfile;
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 dhanoopbhaskar
*/
public class EncryptFile {
KeyGenerator keyGenerator = null;
SecretKey secretKey = null;
Cipher cipher = null;
public EncryptFile() {
try {
/**
* Create a Blowfish key
*/
keyGenerator = KeyGenerator.getInstance("Blowfish");
secretKey = keyGenerator.generateKey();
/**
* Create an instance of cipher mentioning the name of algorithm
* - Blowfish
*/
cipher = Cipher.getInstance("Blowfish");
} catch (NoSuchPaddingException ex) {
System.out.println(ex);
} catch (NoSuchAlgorithmException ex) {
System.out.println(ex);
}
}
public static void main(String[] args) {
String fileToEncrypt = "encryption.jpg";
String encryptedFile = "encryptedFile.jpg";
String decryptedFile = "decryptedFile.jpg";
String directoryPath = "\"D:\\encryption.";
EncryptFile encryptFile = new EncryptFile();
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("\"D:\\encryption.jpg\"");
File encryptedFile = new File("\"D:\\encryption.jpg\"");
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);
}
}
}
CONCLUSION
In this report we have used the blowfish algorithm for encrypting an image for
secure transmission over the internet. To achieve this result to be true, we are
comparing different parameters such as processing time, bandwidth,
correlation, entropy etc of above mentioned algorithms with the other
algorithms such as AES, DES. Blowfish cannot be broken until an attacker tries
28r+1 combinations where r is the number of rounds. Hence if the number of
rounds are been increased then the blowfish algorithm becomes stronger.
Since Blowfish has not any known security weak points so far it can be
considered as an excellent standard encryption algorithm. It takes much less
time for processing than any other encryption techniques. Also all types of
image sizes & format can be encrypted (.jpg, .bmp). By using this algorithm,
lower correlation & higher entropy can also be achieved.
REFERENCES
[1] Network Security and Cryptography, Bernard Menezes, IIT Bombay, Powai
Mumbai.
[2] Sombir Singh , Sunil K. Maakar, Dr.Sudesh Kumar, International Journal of
Advanced Research in Computer Science and Software Engineering, Volume 3,
Issue 6, June 2013.
[3] DES, AES and Blowfish: Symmetric Key Cryptography Algorithms Simulation
Based Performance Analysis, International Journal of Emerging Technology and
Advanced Engineering.
[4] Mohammad Ali Bani Younes and Aman Jantan ,Image Encryption Using
Block-Based Transformation Algorithm, IAENG International Journal of
Computer Science, 35:1, IJCS_35_1_03.
[5] Nadeem, Aamer; "A Performance Comparison of Data Encryption
Algorithms", IEEE 2005.
[6] P. Dang and P. M. Chau, "Image Encryption for Secure Internet Multimedia
Applications', IEEE Transactions on Consumer Electronics, voI.46,no.3,pp.395-
403, Aug.2000.
[7] Ketu File white papers, “Symmetric vs Asymmetric Encryption”, a division of
Midwest Research Corporation.
[8] Tingyuan Nie and Teng Zhang,“ A Study of DES and Blowfish Encryption
Algorithm”, IEEE, 2009 [9] I. Ozturk, I.Sogukpinar, "Analysis and comparison of
image encryption algorithm," Journal of transactions on engineering,
computing and technology December, vol. 3, 2004.
[10] Aloha Sinha, Kehar Singh, "A technique for image encryption using digital
signature", Optics Communications, Vol-2 I 8 (2203), 229-234.