Blowfish Algorithm with Examples - GeeksforGeeks
Blowfish Algorithm with Examples - GeeksforGeeks
1. blockSize: 64-bits
2. keySize: 32-bits to 448-bits variable size
3. number of subkeys: 18 [P-array]
4. number of rounds: 16
5. number of substitution boxes: 4 [each having 512 entries of 32-bits
each]
We use cookies to ensure you have the best browsing experience on our website. By using our
Lets see each step one by one: Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Step1: Generation of subkeys:
P[0] = "243f6a88"
P[1] = "85a308d3"
.
.
.
P[17] = "8979fb1b"
Now each of the subkey is changed with respect to the input key as:
The resultant P-array holds 18 subkeys that is used during the entire
encryption process
Step3: Encryption:
We use cookies to ensure you have the best browsing experience on our website. By using our
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Here the function “add” is addition modulo 2^32.
Java
import java.util.*;
num = Long.parseUnsignedLong(
plainText.charAt(i) + "", 16);
binary4B = Long.toBinaryString(num);
return hexa;
}
// generate subkeys.
private void keyGenerate(String key)
{
int j = 0;
for (int i = 0; i < P.length; i++) {
System.out.println("subkey "
+ (i + 1) + ": "
+ P[i]);
j = (j + 8) % key.length();
}
}
// round function
private String round(int time, String plainText)
{
String left, right;
left = plainText.substring(0, 8);
right = plainText.substring(8, 16);
left = xor(left, P[time]);
System.out.println(
"round " + time + ": "
+ right + left);
// encryption
private String encrypt(String plainText)
{
for (int i = 0; i < 16; i++)
plainText = round(i, plainText);
// postprocessing
String right = plainText.substring(0, 8);
String left = plainText.substring(8, 16);
right = xor(right, P[16]);
left = xor(left, P[17]);
return left + right;
}
Main()
{
// storing 2^32 in modVal
//(<<1 is equivalent to multiply by 2)
for (int i = 0; i < 32; i++)
modVal = modVal << 1;
keyGenerate(key);
System.out.println("-----Encryption-----");
String cipherText = encrypt(plainText);
System.out.println("Cipher Text: " + cipherText);
}
We use cookies to ensure you have the best browsing experience on our website. By using our
site, youOutput
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
subkey 1: 8e846390
subkey 2: a295c40e
subkey 3: b9a28336
subkey 4: 2446bf99
subkey 5: 0eb2313a
subkey 6: 0ea9fd0d
subkey 7: a295f380
subkey 8: cb78a054
subkey 9: ef9328fe
subkey 10: 1fe6dfaa
subkey 11: 14ef6fd7
subkey 12: 13dfc0b1
subkey 13: 6a1720af
subkey 14: ee4a9c00
subkey 15: 953fdcad
subkey 16: 9271c5ca
subkey 17: 38addcc1
subkey 18: ae4f37c6
-----Encryption-----
round 0: 77b3ba639cb0353b
round 1: 0cc7d63fd5267e6d
round 2: c799728ab5655509
round 3: 69612395e3dfcd13
round 4: f3f5b74b67d312af
round 5: 52023d4efd5c4a46
round 6: 5b785180f097cece
round 7: cc946d119000f1d4
round 8: 6af47a4b230745ef
round 9: 9fb82cc57512a5e1
round 10: 1106c1ab8b574312
round 11: 7d7a616502d9011a
round 12: 81e9ce71176d41ca
round 13: 9727e50a6fa35271
round 14: eb761e34021839a7
round 15: 0599d9367907dbfe
Cipher Text: d748ec383d3405f7
Decryption
The decryption process is similar to that of encryption and the subkeys are
We use cookies to ensure you have the best browsing experience on our website. By using our
used in reverse{P[17] – P[0]}. The entire decryption process can be
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
elaborated as:
P[0] = "243f6a88"
P[1] = "85a308d3"
.
.
.
P[17] = "8979fb1b"
Now each of the subkeys is changed with respect to the input key as:
The resultant P-array holds 18 subkeys that is used during the entire
encryption process
Step3: Decryption:
Javato ensure you have the best browsing experience on our website. By using our
We use cookies
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
// Java program to demonstrate
// Blowfish decryption Algorithm
import java.util.*;
// generate subkeys.
private void keyGenerate(String key)
{
int j = 0;
for (int i = 0; i < P.length; i++) {
P[i] = xor(P[i], key.substring(j, j + 8));
System.out.println("subkey "
+ (i + 1) + ": "
+ P[i]);
j = (j + 8) % key.length();
We use cookies to ensure you
} have the best browsing experience on our website. By using our
site, you acknowledge}that you have read and understood our Cookie Policy & Privacy Policy
// round function
private String round(int time, String plainText)
{
String left, right;
left = plainText.substring(0, 8);
right = plainText.substring(8, 16);
left = xor(left, P[time]);
String fOut = f(left); // output from F function
right = xor(fOut, right);
// decryption
private String decrypt(String plainText)
{
for (int i = 17; i > 1; i--)
plainText = round(i, plainText);
// postprocessing
String right = plainText.substring(0, 8);
String left = plainText.substring(8, 16);
right = xor(right, P[1]);
left = xor(left, P[0]);
return left + right;
}
Main()
{
// storing 2^32 in modVal
//(<<1 is equivalent to multiply by 2)
for (int i = 0; i < 32; i++)
modVal = modVal << 1;
String cipherText = "d748ec383d3405f7";
String key = "aabb09182736ccdd";
keyGenerate(key);
System.out.println("-----Decryption-----");
String plainText = decrypt(cipherText);
System.out.println("Plain Text: "
+ plainText);
}
Output
subkey 1: 8e846390
subkey 2: a295c40e
subkey 3: b9a28336
subkey 4: 2446bf99
subkey 5: 0eb2313a
subkey 6: 0ea9fd0d
subkey 7: a295f380
subkey 8: cb78a054
subkey 9: ef9328fe
subkey 10: 1fe6dfaa
subkey 11: 14ef6fd7
subkey 12: 13dfc0b1
subkey 13: 6a1720af
subkey 14: ee4a9c00
subkey 15: 953fdcad
subkey 16: 9271c5ca
subkey 17: 38addcc1
subkey 18: ae4f37c6
-----Decryption-----
round 17: 3ab5e5667907dbfe
round 16: fdd297bb021839a7
round 15: 82529d676fa35271
round 14: ec939d1a176d41ca
round 13: e14063bd02d9011a
round 12: 66cd65508b574312
round 11: 37e82a387512a5e1
round 10: 8fe62e7e230745ef
round 9: 1f04e6309000f1d4
round 8: 3624ea12f097cece
round 7: c546e12ffd5c4a46
round 6: ed76301e67d312af
round 5: bbd76433e3dfcd13
round 4: f160c1f4b5655509
round 3: 2512b60dd5267e6d
round 2: 6f86e1389cb0353b
We use cookies to ensure you have the best browsing experience on our website. By using our
Plain Text: 123456abcd132536
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Advantages and Disadvantages of Blowfish Algorithm:
Blowfish is a fast block cipher except when changing keys. Each new key
requires a pre-processing equivalent to 4KB of text.
It is faster and much better than DES Encryption.
Blowfish uses a 64-bit block size which makes it vulnerable to birthday
attacks.
A reduced round variant of blowfish is known to be susceptible to known
plain text attacks(2nd order differential attacks – 4 rounds).
Master Data Structures and Algorithms at your own pace with our DSA
Self-Paced course. In just 90 days, you’ll cover core concepts, solve real-
world problems, and sharpen your problem-solving skills. Take the
Three 90 Challenge: complete 90% of the course in 90 days and get a
90% refund. Stay motivated, track progress, and achieve DSA mastery.
Start today!
Similar Reads
Edge Relaxation Property for Dijkstra’s Algorithm and Bellman Ford's…
In the field of graph theory, various shortest path algorithms especially
Dijkstra’s
We use cookies algorithm
to ensure and
you have the Bellmann-Ford’s
best algorithm
browsing experience on our website.repeatedly
By using our employ the us…
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
4 min read
Karatsuba algorithm for fast multiplication using Divide and Conquer…
Given two binary strings that represent value of two integers, find the product
of two strings. For example, if the first bit string is "1100" and second bit strin…
13 min read
3 min read
2 min read
3 min read
6 min read
Peterson's Algorithm for Mutual Exclusion | Set 2 (CPU Cycles and Memor…
Problem: Given 2 process i and j, you need to write a program that can
guarantee mutual exclusion between the two without any additional hardwar…
11 min read
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305
Advertise with us
Databases Preparation Competitive More Tutorials Free Online Write & Earn
SQL Corner Exams Software Tools Write an Article
MYSQL Company-Wise JEE Advanced Development Typing Test Improve an Article
PostgreSQL Recruitment UGC NET Software Testing Image Editor Pick Topics to
PL/SQL Process UPSC Product Code Formatters Write
MongoDB Resume Templates SSC CGL Management Code Converters Share your
Aptitude SBI PO Project Currency Experiences
Preparation SBI Clerk Management Converter Internships
Puzzles IBPS PO Linux Random Number
Company-Wise IBPS Clerk Excel Generator
Preparation All Cheat Sheets Random Password
Companies Recent Articles Generator
Colleges
We use cookies to ensure you have the best browsing experience on our website. By using our
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy