Saturday, September 28, 2024

Blowfish Algorithm in Cryptography

 Blowfish is a symmetric, 64-bit block cipher with changeable length. As a "general-purpose algorithm," it was created by Bruce Schneier in 1993 as a quick, cost-free replacement for the venerable Data Encryption Standard (DES) and International Data Encryption Algorithm (IDEA) encryption techniques.


Blowfish is unpatented, substantially quicker than DES and IDEA, and freely accessible for all purposes. However, because of its short block size, which is seen as unsafe, it couldn't totally replace DES.

The security issue was addressed by Twofish, its successor, who used a higher block size of 128 bits. Even Nevertheless, many cipher suites and encryption solutions on the market today use the Blowfish algorithm since complete Blowfish encryption has never been cracked.

Identifying blowfish

Blowfish features a 64-bit block size with keys that can be 32 bits long or 448 bits long. It features 16 iterations that resemble Feistel and each one operates on a 64-bit block that is partitioned into two 32-bit words. Blowfish uses a single encryption key to encode and decode data.

The two primary parts of the blowfish algorithm are as follows:

Data Encryption: A 16-round Feistel network is used to encrypt data, with each round including a key-dependent permutation and a key- and data-dependent replacement. The replacement approach is used in conjunction with large, key-dependent S-boxes to encrypt data in Blowfish. All encryption processes include add on 32-bit words and XORs, a sort of logic gate.

Key expansion and Sub keys: Maximum size 448-bit keys are expanded into several sub key arrays totaling 4,168 bytes throughout the key expansion procedure. The Blowfish algorithm, which makes extensive use of sub keys, depends on them. Before any encryption or decryption can occur, these sub keys are pre-calculated.

The P-array in Blowfish is made up of four 32-bit S-boxes with 256 entries each, along with 18 32-bit sub keys. Following is the calculation for the sub keys:

  1. A predetermined string of pi's hexadecimal digits is used as the initialization value for the P-array and S-boxes.
  2. Now, the first P-array element (P1) is XORed with the first 32 bits of the key, followed by P2 with the second 32 bits, and so on, until all P-array elements have been XORed with the key bits.
  3. The procedure is applied on all-zero strings in the manner shown in the preceding phases.
  4. Replace the P1 and P2 arrays with the results of step 3 above.
  5. Blowfish is used to encrypt this output while using changed sub keys.
  6. The P-array's P3 and P4 are modified by the output of step 5.
  7. The four S-boxes and all the P-arrays are updated when this operation is completed.

To produce all the sub keys and processes, Blowfish executes 521 times in total, using around 4 kilobytes (KB) of data.

Example of the Blowfish encryption and decryption technique

Assume Blowfish will be used to encrypt the message "Hi world". The procedures are as follows:

  1. The first input string "Hi world" consists of seven characters plus a space, for a total of eight bytes or 64 bits.
  2. It divides the input into 32 bits. Key expansion produces a value termed P1, which is then XORed with the remaining 32 bits, "Hi w," to produce the desired result. (Note: P stands for a prime number, which can only be divided by itself and by one other.)
  3. The 32 bits are then divided into 4 bytes each and delivered to the four S-boxes after P1 has gone via a transformational F-function (F In).
  4. The first two values from the first two S-boxes are combined, and then the third value from the third S-box is XORed with them.
  5. To create 32 bits as the output, this result is added to the fourth S-box's output.
  6. To create output F1', the output of F In is XORed with the right 32 bits of the input message, "orld".
  7. The left half of the message is then replaced by F1', and the right half by P1'.
  8. For a total of 16 rounds, the identical procedure is performed for each new member of the P-array.
  9. The outputs P16' and F16' are finally XORed with the P-array's final two entries, P17 and P18, after 16 cycles. After that, they are merged once again to create the 64-bit cipher text of the input message.

IMAGE

Benefits of Blowfish

Blowfish is one of the quickest and smallest block ciphers currently in use, converting data into cipher text using a symmetric encryption key. Blowfish is still commonly used over three decades after it was initially created because it provides the following benefits:

  • The data encryption process is efficient on large microprocessors
  • Despite the complex initialization phase before encryption and is much faster and more effective than DES and IDEA algorithms.
  • It also provides extensive security for Java-based software and applications, secure access for backup tools.
  • And supports secure user authentication for remote access.

Drawbacks of blowfish

Using Blowfish for encryption has various drawbacks, such as the following:

  • Changes in key speed slow things down.
  • It takes time to complete the main timetable.
  • The technique is susceptible to birthday assaults, a type of brute-force attack, because to the short 64-bit block size.
  • Each new key needs to be preprocessed, taking up 4 KB of text, which slows it down and renders it useless for some applications.

Uses of blowfish

Blowfish is appropriate for a variety of uses, such as the following:

  • Mobile processors
  • Email
  • File or disc encryption
  • Packet encryption
  • Random bit creation
  • Password hashing
  • Data backup
  • Protect Shell

Many well-known products, including CryptoDisk, PasswordWallet, Access Manager, Symantec NetBackup, and SplashID, employ Blowfish. Blowfish is also widely used by e-commerce companies and social media platforms to secure user data.

A Java program that demonstrates Blowfish encryption below:

  1. // Java Program to demonstrate Blowfish encryption  
  2. import java.util.*;  
  3. public class Main {  
  4.   
  5.     // Substitution boxes each string is a 32 bit hexadecimal value.  
  6.     String S[][]  
  7.         = { { "d1310ba6""98dfb5ac""2ffd72db""d01adfb7""b8e1afed",  
  8.             "6a267e96""ba7c9045""f12c7f99""24a19947""b3916cf7",  
  9.             "0801f2e2""858efc16""636920d8""71574e69""a458fea3",  
  10.             "f4933d7e""0d95748f""728eb658""718bcd58""82154aee",  
  11.             "7b54a41d""c25a59b5""9c30d539""2af26013""c5d1b023",  
  12.             "286085f0""ca417918""b8db38ef""8e79dcb0""603a180e",  
  13.             "6c9e0e8b""b01e8a3e""d71577c1""bd314b27""78af2fda",  
  14.             "55605c60""e65525f3""aa55ab94""57489862""63e81440",  
  15.             "55ca396a""2aab10b6""b4cc5c34""1141e8ce""a15486af",  
  16.             "7c72e993""b3ee1411""636fbc2a""2ba9c55d""741831f6",  
  17.             "ce5c3e16""9b87931e""afd6ba33""6c24cf5c""7a325381",  
  18.             "28958677""3b8f4898""6b4bb9af""c4bfe81b""66282193",  
  19.             "61d809cc""fb21a991""487cac60""5dec8032""ef845d5d",  
  20.             "e98575b1""dc262302""eb651b88""23893e81""d396acc5",  
  21.             "0f6d6ff3""83f44239""2e0b4482""a4842004""69c8f04a",  
  22.             "9e1f9b5e""21c66842""f6e96c9a""670c9c61""abd388f0",  
  23.             "6a51a0d2""d8542f68""960fa728""ab5133a3""6eef0b6c",  
  24.             "137a3be4""ba3bf050""7efb2a98""a1f1651d""39af0176",  
  25.             "66ca593e""82430e88""8cee8619""456f9fb4""7d84a5c3",  
  26.             "3b8b5ebe""e06f75d8""85c12073""401a449f""56c16aa6",  
  27.             "4ed3aa62""363f7706""1bfedf72""429b023d""37d0d724",  
  28.             "d00a1248""db0fead3""49f1c09b""075372c9""80991b7b",  
  29.             "25d479d8""f6e8def7""e3fe501a""b6794c3b""976ce0bd",  
  30.             "04c006ba""c1a94fb6""409f60c4""5e5c9ec2""196a2463",  
  31.             "68fb6faf""3e6c53b5""1339b2eb""3b52ec6f""6dfc511f",  
  32.             "9b30952c""cc814544""af5ebd09""bee3d004""de334afd",  
  33.             "660f2807""192e4bb3""c0cba857""45c8740f""d20b5f39",  
  34.             "b9d3fbdb""5579c0bd""1a60320a""d6a100c6""402c7279",  
  35.             "679f25fe""fb1fa3cc""8ea5e9f8""db3222f8""3c7516df",  
  36.             "fd616b15""2f501ec8""ad0552ab""323db5fa""fd238760",  
  37.             "53317b48""3e00df82""9e5c57bb""ca6f8ca0""1a87562e",  
  38.             "df1769db""d542a8f6""287effc3""ac6732c6""8c4f5573",  
  39.             "695b27b0""bbca58c8""e1ffa35d""b8f011a0""10fa3d98",  
  40.             "fd2183b8""4afcb56c""2dd1d35b""9a53e479""b6f84565",  
  41.             "d28e49bc""4bfb9790""e1ddf2da""a4cb7e33""62fb1341",  
  42.             "cee4c6e8""ef20cada""36774c01""d07e9efe""2bf11fb4",  
  43.             "95dbda4d""ae909198""eaad8e71""6b93d5a0""d08ed1d0",  
  44.             "afc725e0""8e3c5b2f""8e7594b7""8ff6e2fb""f2122b64",  
  45.             "8888b812""900df01c""4fad5ea0""688fc31c""d1cff191",  
  46.             "b3a8c1ad""2f2f2218""be0e1777""ea752dfe""8b021fa1",  
  47.             "e5a0cc0f""b56f74e8""18acf3d6""ce89e299""b4a84fe0",  
  48.             "fd13e0b7""7cc43b81""d2ada8d9""165fa266""80957705",  
  49.             "93cc7314""211a1477""e6ad2065""77b5fa86""c75442f5",  
  50.             "fb9d35cf""ebcdaf0c""7b3e89a0""d6411bd3""ae1e7e49",  
  51.             "00250e2d""2071b35e""226800bb""57b8e0af""2464369b",  
  52.             "f009b91e""5563911d""59dfa6aa""78c14389""d95a537f",  
  53.             "207d5ba2""02e5b9c5""83260376""6295cfa9""11c81968",  
  54.             "4e734a41""b3472dca""7b14a94a""1b510052""9a532915",  
  55.             "d60f573f""bc9bc6e4""2b60a476""81e67400""08ba6fb5",  
  56.             "571be91f""f296ec6b""2a0dd915""b6636521""e7b9f9b6",  
  57.             "ff34052e""c5855664""53b02d5d""a99f8fa1""08ba4799",  
  58.             "6e85076a" },  
  59.             { "4b7a70e9""b5b32944""db75092e""c4192623""ad6ea6b0",  
  60.             "49a7df7d""9cee60b8""8fedb266""ecaa8c71""699a17ff",  
  61.             "5664526c""c2b19ee1""193602a5""75094c29""a0591340",  
  62.             "e4183a3e""3f54989a""5b429d65""6b8fe4d6""99f73fd6",  
  63.             "a1d29c07""efe830f5""4d2d38e6""f0255dc1""4cdd2086",  
  64.             "8470eb26""6382e9c6""021ecc5e""09686b3f""3ebaefc9",  
  65.             "3c971814""6b6a70a1""687f3584""52a0e286""b79c5305",  
  66.             "aa500737""3e07841c""7fdeae5c""8e7d44ec""5716f2b8",  
  67.             "b03ada37""f0500c0d""f01c1f04""0200b3ff""ae0cf51a",  
  68.             "3cb574b2""25837a58""dc0921bd""d19113f9""7ca92ff6",  
  69.             "94324773""22f54701""3ae5e581""37c2dadc""c8b57634",  
  70.             "9af3dda7""a9446146""0fd0030e""ecc8c73e""a4751e41",  
  71.             "e238cd99""3bea0e2f""3280bba1""183eb331""4e548b38",  
  72.             "4f6db908""6f420d03""f60a04bf""2cb81290""24977c79",  
  73.             "5679b072""bcaf89af""de9a771f""d9930810""b38bae12",  
  74.             "dccf3f2e""5512721f""2e6b7124""501adde6""9f84cd87",  
  75.             "7a584718""7408da17""bc9f9abc""e94b7d8c""ec7aec3a",  
  76.             "db851dfa""63094366""c464c3d2""ef1c1847""3215d908",  
  77.             "dd433b37""24c2ba16""12a14d43""2a65c451""50940002",  
  78.             "133ae4dd""71dff89e""10314e55""81ac77d6""5f11199b",  
  79.             "043556f1""d7a3c76b""3c11183b""5924a509""f28fe6ed",  
  80.             "97f1fbfa""9ebabf2c""1e153c6e""86e34570""eae96fb1",  
  81.             "860e5e0a""5a3e2ab3""771fe71c""4e3d06fa""2965dcb9",  
  82.             "99e71d0f""803e89d6""5266c825""2e4cc978""9c10b36a",  
  83.             "c6150eba""94e2ea78""a5fc3c53""1e0a2df4""f2f74ea7",  
  84.             "361d2b3d""1939260f""19c27960""5223a708""f71312b6",  
  85.             "ebadfe6e""eac31f66""e3bc4595""a67bc883""b17f37d1",  
  86.             "018cff28""c332ddef""be6c5aa5""65582185""68ab9802",  
  87.             "eecea50f""db2f953b""2aef7dad""5b6e2f84""1521b628",  
  88.             "29076170""ecdd4775""619f1510""13cca830""eb61bd96",  
  89.             "0334fe1e""aa0363cf""b5735c90""4c70a239""d59e9e0b",  
  90.             "cbaade14""eecc86bc""60622ca7""9cab5cab""b2f3846e",  
  91.             "648b1eaf""19bdf0ca""a02369b9""655abb50""40685a32",  
  92.             "3c2ab4b3""319ee9d5""c021b8f7""9b540b19""875fa099",  
  93.             "95f7997e""623d7da8""f837889a""97e32d77""11ed935f",  
  94.             "16681281""0e358829""c7e61fd6""96dedfa1""7858ba99",  
  95.             "57f584a5""1b227263""9b83c3ff""1ac24696""cdb30aeb",  
  96.             "532e3054""8fd948e4""6dbc3128""58ebf2ef""34c6ffea",  
  97.             "fe28ed61""ee7c3c73""5d4a14d9""e864b7e3""42105d14",  
  98.             "203e13e0""45eee2b6""a3aaabea""db6c4f15""facb4fd0",  
  99.             "c742f442""ef6abbb5""654f3b1d""41cd2105""d81e799e",  
  100.             "86854dc7""e44b476a""3d816250""cf62a1f2""5b8d2646",  
  101.             "fc8883a0""c1c7b6a3""7f1524c3""69cb7492""47848a0b",  
  102.             "5692b285""095bbf00""ad19489d""1462b174""23820e00",  
  103.             "58428d2a""0c55f5ea""1dadf43e""233f7061""3372f092",  
  104.             "8d937e41""d65fecf1""6c223bdb""7cde3759""cbee7460",  
  105.             "4085f2a7""ce77326e""a6078084""19f8509e""e8efd855",  
  106.             "61d99735""a969a7aa""c50c06c2""5a04abfc""800bcadc",  
  107.             "9e447a2e""c3453484""fdd56705""0e1e9ec9""db73dbd3",  
  108.             "105588cd""675fda79""e3674340""c5c43465""713e38d8",  
  109.             "3d28f89e""f16dff20""153e21e7""8fb03d4a""e6e39f2b",  
  110.             "db83adf7" },  
  111.             { "e93d5a68""948140f7""f64c261c""94692934""411520f7",  
  112.             "7602d4f7""bcf46b2e""d4a20068""d4082471""3320f46a",  
  113.             "43b7d4b7""500061af""1e39f62e""97244546""14214f74",  
  114.             "bf8b8840""4d95fc1d""96b591af""70f4ddd3""66a02f45",  
  115.             "bfbc09ec""03bd9785""7fac6dd0""31cb8504""96eb27b3",  
  116.             "55fd3941""da2547e6""abca0a9a""28507825""530429f4",  
  117.             "0a2c86da""e9b66dfb""68dc1462""d7486900""680ec0a4",  
  118.             "27a18dee""4f3ffea2""e887ad8c""b58ce006""7af4d6b6",  
  119.             "aace1e7c""d3375fec""ce78a399""406b2a42""20fe9e35",  
  120.             "d9f385b9""ee39d7ab""3b124e8b""1dc9faf7""4b6d1856",  
  121.             "26a36631""eae397b2""3a6efa74""dd5b4332""6841e7f7",  
  122.             "ca7820fb""fb0af54e""d8feb397""454056ac""ba489527",  
  123.             "55533a3a""20838d87""fe6ba9b7""d096954b""55a867bc",  
  124.             "a1159a58""cca92963""99e1db33""a62a4a56""3f3125f9",  
  125.             "5ef47e1c""9029317c""fdf8e802""04272f70""80bb155c",  
  126.             "05282ce3""95c11548""e4c66d22""48c1133f""c70f86dc",  
  127.             "07f9c9ee""41041f0f""404779a4""5d886e17""325f51eb",  
  128.             "d59bc0d1""f2bcc18f""41113564""257b7834""602a9c60",  
  129.             "dff8e8a3""1f636c1b""0e12b4c2""02e1329e""af664fd1",  
  130.             "cad18115""6b2395e0""333e92e1""3b240b62""eebeb922",  
  131.             "85b2a20e""e6ba0d99""de720c8c""2da2f728""d0127845",  
  132.             "95b794fd""647d0862""e7ccf5f0""5449a36f""877d48fa",  
  133.             "c39dfd27""f33e8d1e""0a476341""992eff74""3a6f6eab",  
  134.             "f4f8fd37""a812dc60""a1ebddf8""991be14c""db6e6b0d",  
  135.             "c67b5510""6d672c37""2765d43b""dcd0e804""f1290dc7",  
  136.             "cc00ffa3""b5390f92""690fed0b""667b9ffb""cedb7d9c",  
  137.             "a091cf0b""d9155ea3""bb132f88""515bad24""7b9479bf",  
  138.             "763bd6eb""37392eb3""cc115979""8026e297""f42e312d",  
  139.             "6842ada7""c66a2b3b""12754ccc""782ef11c""6a124237",  
  140.             "b79251e7""06a1bbe6""4bfb6350""1a6b1018""11caedfa",  
  141.             "3d25bdd8""e2e1c3c9""44421659""0a121386""d90cec6e",  
  142.             "d5abea2a""64af674e""da86a85f""bebfe988""64e4c3fe",  
  143.             "9dbc8057""f0f7c086""60787bf8""6003604d""d1fd8346",  
  144.             "f6381fb0""7745ae04""d736fccc""83426b33""f01eab71",  
  145.             "b0804187""3c005e5f""77a057be""bde8ae24""55464299",  
  146.             "bf582e61""4e58f48f""f2ddfda2""f474ef38""8789bdc2",  
  147.             "5366f9c3""c8b38e74""b475f255""46fcd9b9""7aeb2661",  
  148.             "8b1ddf84""846a0e79""915f95e2""466e598e""20b45770",  
  149.             "8cd55591""c902de4c""b90bace1""bb8205d0""11a86248",  
  150.             "7574a99e""b77f19b6""e0a9dc09""662d09a1""c4324633",  
  151.             "e85a1f02""09f0be8c""4a99a025""1d6efe10""1ab93d1d",  
  152.             "0ba5a4df""a186f20f""2868f169""dcb7da83""573906fe",  
  153.             "a1e2ce9b""4fcd7f52""50115e01""a70683fa""a002b5c4",  
  154.             "0de6d027""9af88c27""773f8641""c3604c06""61a806b5",  
  155.             "f0177a28""c0f586e0""006058aa""30dc7d62""11e69ed7",  
  156.             "2338ea63""53c2dd94""c2c21634""bbcbee56""90bcb6de",  
  157.             "ebfc7da1""ce591d76""6f05e409""4b7c0188""39720a3d",  
  158.             "7c927c24""86e3725f""724d9db9""1ac15bb4""d39eb8fc",  
  159.             "ed545578""08fca5b5""d83d7cd3""4dad0fc4""1e50ef5e",  
  160.             "b161e6f8""a28514d9""6c51133c""6fd5c7e7""56e14ec4",  
  161.             "362abfce""ddc6c837""d79a3234""92638212""670efa8e",  
  162.             "406000e0" },  
  163.             { "3a39ce37""d3faf5cf""abc27737""5ac52d1b""5cb0679e",  
  164.             "4fa33742""d3822740""99bc9bbe""d5118e9d""bf0f7315",  
  165.             "d62d1c7e""c700c47b""b78c1b6b""21a19045""b26eb1be",  
  166.             "6a366eb4""5748ab2f""bc946e79""c6a376d2""6549c2c8",  
  167.             "530ff8ee""468dde7d""d5730a1d""4cd04dc6""2939bbdb",  
  168.             "a9ba4650""ac9526e8""be5ee304""a1fad5f0""6a2d519a",  
  169.             "63ef8ce2""9a86ee22""c089c2b8""43242ef6""a51e03aa",  
  170.             "9cf2d0a4""83c061ba""9be96a4d""8fe51550""ba645bd6",  
  171.             "2826a2f9""a73a3ae1""4ba99586""ef5562e9""c72fefd3",  
  172.             "f752f7da""3f046f69""77fa0a59""80e4a915""87b08601",  
  173.             "9b09e6ad""3b3ee593""e990fd5a""9e34d797""2cf0b7d9",  
  174.             "022b8b51""96d5ac3a""017da67d""d1cf3ed6""7c7d2d28",  
  175.             "1f9f25cf""adf2b89b""5ad6b472""5a88f54c""e029ac71",  
  176.             "e019a5e6""47b0acfd""ed93fa9b""e8d3c48d""283b57cc",  
  177.             "f8d56629""79132e28""785f0191""ed756055""f7960e44",  
  178.             "e3d35e8c""15056dd4""88f46dba""03a16125""0564f0bd",  
  179.             "c3eb9e15""3c9057a2""97271aec""a93a072a""1b3f6d9b",  
  180.             "1e6321f5""f59c66fb""26dcf319""7533d928""b155fdf5",  
  181.             "03563482""8aba3cbb""28517711""c20ad9f8""abcc5167",  
  182.             "ccad925f""4de81751""3830dc8e""379d5862""9320f991",  
  183.             "ea7a90c2""fb3e7bce""5121ce64""774fbe32""a8b6e37e",  
  184.             "c3293d46""48de5369""6413e680""a2ae0810""dd6db224",  
  185.             "69852dfd""09072166""b39a460a""6445c0dd""586cdecf",  
  186.             "1c20c8ae""5bbef7dd""1b588d40""ccd2017f""6bb4e3bb",  
  187.             "dda26a7e""3a59ff45""3e350a44""bcb4cdd5""72eacea8",  
  188.             "fa6484bb""8d6612ae""bf3c6f47""d29be463""542f5d9e",  
  189.             "aec2771b""f64e6370""740e0d8d""e75b1357""f8721671",  
  190.             "af537d5d""4040cb08""4eb4e2cc""34d2466a""0115af84",  
  191.             "e1b00428""95983a1d""06b89fb4""ce6ea048""6f3f3b82",  
  192.             "3520ab82""011a1d4b""277227f8""611560b1""e7933fdc",  
  193.             "bb3a792b""344525bd""a08839e1""51ce794b""2f32c9b7",  
  194.             "a01fbac9""e01cc87e""bcc7d1f6""cf0111c3""a1e8aac7",  
  195.             "1a908749""d44fbd9a""d0dadecb""d50ada38""0339c32a",  
  196.             "c6913667""8df9317c""e0b12b4f""f79e59b7""43f5bb3a",  
  197.             "f2d519ff""27d9459c""bf97222c""15e6fc2a""0f91fc71",  
  198.             "9b941525""fae59361""ceb69ceb""c2a86459""12baa8d1",  
  199.             "b6c1075e""e3056a0c""10d25065""cb03a442""e0ec6e0e",  
  200.             "1698db3b""4c98a0be""3278e964""9f1f9532""e0d392df",  
  201.             "d3a0342b""8971f21e""1b0a7441""4ba3348c""c5be7120",  
  202.             "c37632d8""df359f8d""9b992f2e""e60b6f47""0fe3f11d",  
  203.             "e54cda54""1edad891""ce6279cf""cd3e7e6f""1618b166",  
  204.             "fd2c1d05""848fd2c5""f6fb2299""f523f357""a6327623",  
  205.             "93a83531""56cccd02""acf08162""5a75ebb5""6e163697",  
  206.             "88d273cc""de966292""81b949d0""4c50901b""71c65614",  
  207.             "e6c6c7bd""327a140a""45e1d006""c3f27b9a""c9aa53fd",  
  208.             "62a80f00""bb25bfe2""35bdd2f6""71126905""b2040222",  
  209.             "b6cbcf7c""cd769c2b""53113ec0""1640e3d3""38abbd60",  
  210.             "2547adf0""ba38209c""f746ce76""77afa1c5""20756060",  
  211.             "85cbfe4e""8ae88dd8""7aaaf9b0""4cf9aa7e""1948c25c",  
  212.             "02fb8a8c""01c36ae4""d6ebe1f9""90d4f869""a65cdea0",  
  213.             "3f09252d""c208e69f""b74e6132""ce77e25b""578fdfe3",  
  214.             "3ac372e6" } };  
  215.   
  216.     // Subkeys initialisation with digits of pi.  
  217.     String P[] = { "243f6a88""85a308d3""13198a2e""03707344""a4093822",  
  218.                 "299f31d0""082efa98""ec4e6c89""452821e6""38d01377",  
  219.                 "be5466cf""34e90c6c""c0ac29b7""c97c50dd""3f84d5b5",  
  220.                 "b5470917""9216d5d9""8979fb1b" };  
  221.   
  222.     // to store 2^32(for addition modulo 2^32).  
  223.     long modVal = 1;  
  224.   
  225.     // to convert hexadecimal to binary.  
  226.     private String hexToBin(String plainText)  
  227.     {  
  228.         String binary = "";  
  229.         Long num;  
  230.         String binary4B;  
  231.         int n = plainText.length();  
  232.         for (int i = 0; i < n; i++) {  
  233.   
  234.             num = Long.parseUnsignedLong(  
  235.                 plainText.charAt(i) + "", 16);  
  236.             binary4B = Long.toBinaryString(num);  
  237.   
  238.             // each value in hexadecimal is 4 bits in binary.  
  239.             binary4B = "0000" + binary4B;  
  240.   
  241.             binary4B = binary4B.substring(binary4B.length() - 4);  
  242.             binary += binary4B;  
  243.         }  
  244.         return binary;  
  245.     }  
  246.   
  247.     // convert from binary to hexadecimal.  
  248.     private String binToHex(String plainText)  
  249.     {  
  250.   
  251.         long num = Long.parseUnsignedLong(plainText, 2);  
  252.         String hexa = Long.toHexString(num);  
  253.         while (hexa.length() < (plainText.length() / 4))  
  254.   
  255.             // maintain output length same length  
  256.             // as input by appending leading zeroes.  
  257.             hexa = "0" + hexa;  
  258.   
  259.         return hexa;  
  260.     }  
  261.   
  262.     // xor two hexadecimal strings of the same length.  
  263.     private String xor(String a, String b)  
  264.     {  
  265.         a = hexToBin(a);  
  266.         b = hexToBin(b);  
  267.         String ans = "";  
  268.         for (int i = 0; i < a.length(); i++)  
  269.             ans += (char)(((a.charAt(i) - '0')  
  270.                         ^ (b.charAt(i) - '0'))  
  271.                         + '0');  
  272.         ans = binToHex(ans);  
  273.         return ans;  
  274.     }  
  275.   
  276.     // addition modulo 2^32 of two hexadecimal strings.  
  277.     private String addBin(String a, String b)  
  278.     {  
  279.         String ans = "";  
  280.         long n1 = Long.parseUnsignedLong(a, 16);  
  281.         long n2 = Long.parseUnsignedLong(b, 16);  
  282.         n1 = (n1 + n2) % modVal;  
  283.         ans = Long.toHexString(n1);  
  284.         ans = "00000000" + ans;  
  285.         return ans.substring(ans.length() - 8);  
  286.     }  
  287.   
  288.     // function F explained above.  
  289.     private String f(String plainText)  
  290.     {  
  291.         String a[] = new String[4];  
  292.         String ans = "";  
  293.         for (int i = 0; i < 8; i += 2) {  
  294.             // the column number for S-box  
  295.             // is 8-bit value(8*4 = 32 bit plain text)  
  296.             long col  
  297.                 = Long.parseUnsignedLong(  
  298.                     hexToBin(  
  299.                         plainText  
  300.                             .substring(i, i + 2)),  
  301.                     2);  
  302.             a[i / 2] = S[i / 2][(int)col];  
  303.         }  
  304.         ans = addBin(a[0], a[1]);  
  305.         ans = xor(ans, a[2]);  
  306.         ans = addBin(ans, a[3]);  
  307.         return ans;  
  308.     }  
  309.   
  310.     // generate subkeys.  
  311.     private void keyGenerate(String key)  
  312.     {  
  313.         int j = 0;  
  314.         for (int i = 0; i < P.length; i++) {  
  315.   
  316.             // xor-ing 32-bit parts of the key  
  317.             // with initial subkeys.  
  318.             P[i] = xor(P[i], key.substring(j, j + 8));  
  319.   
  320.             System.out.println("subkey "  
  321.                             + (i + 1) + ": "  
  322.                             + P[i]);  
  323.             j = (j + 8) % key.length();  
  324.         }  
  325.     }  
  326.   
  327.     // round function  
  328.     private String round(int time, String plainText)  
  329.     {  
  330.         String left, right;  
  331.         left = plainText.substring(08);  
  332.         right = plainText.substring(816);  
  333.         left = xor(left, P[time]);  
  334.   
  335.         // output from F function  
  336.         String fOut = f(left);  
  337.   
  338.         right = xor(fOut, right);  
  339.   
  340.         System.out.println(  
  341.             "round " + time + ": "  
  342.             + right + left);  
  343.   
  344.         // swap left and right  
  345.         return right + left;  
  346.     }  
  347.   
  348.     // encryption  
  349.     private String encrypt(String plainText)  
  350.     {  
  351.         for (int i = 0; i < 16; i++)  
  352.             plainText = round(i, plainText);  
  353.   
  354.         // postprocessing  
  355.         String right = plainText.substring(08);  
  356.         String left = plainText.substring(816);  
  357.         right = xor(right, P[16]);  
  358.         left = xor(left, P[17]);  
  359.         return left + right;  
  360.     }  
  361.   
  362.     Main()  
  363.     {  
  364.         // storing 2^32 in modVal  
  365.         //(<<1 is equivalent to multiply by 2)  
  366.         for (int i = 0; i < 32; i++)  
  367.             modVal = modVal << 1;  
  368.   
  369.         String plainText = "123456abcd132536";  
  370.         String key = "aabb09182736ccdd";  
  371.   
  372.         keyGenerate(key);  
  373.   
  374.         System.out.println("-----Encryption-----");  
  375.         String cipherText = encrypt(plainText);  
  376.         System.out.println("Cipher Text: " + cipherText);  
  377.     }  
  378.   
  379.     public static void main(String args[])  
  380.     {  
  381.         new Main();  
  382.     }  
  383. }  

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
-----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

Here is a Java program that shows how to decode data:

  1. // Java program to demonstrate  
  2. // Blowfish decryption Algorithm  
  3.   
  4. import java.util.*;  
  5.   
  6. public class Main {  
  7.   
  8.     // Substitution boxes each string is a 32 bit hexadecimal value.  
  9.     String S[][]  
  10.         = { { "d1310ba6""98dfb5ac""2ffd72db""d01adfb7""b8e1afed",  
  11.             "6a267e96""ba7c9045""f12c7f99""24a19947""b3916cf7",  
  12.             "0801f2e2""858efc16""636920d8""71574e69""a458fea3",  
  13.             "f4933d7e""0d95748f""728eb658""718bcd58""82154aee",  
  14.             "7b54a41d""c25a59b5""9c30d539""2af26013""c5d1b023",  
  15.             "286085f0""ca417918""b8db38ef""8e79dcb0""603a180e",  
  16.             "6c9e0e8b""b01e8a3e""d71577c1""bd314b27""78af2fda",  
  17.             "55605c60""e65525f3""aa55ab94""57489862""63e81440",  
  18.             "55ca396a""2aab10b6""b4cc5c34""1141e8ce""a15486af",  
  19.             "7c72e993""b3ee1411""636fbc2a""2ba9c55d""741831f6",  
  20.             "ce5c3e16""9b87931e""afd6ba33""6c24cf5c""7a325381",  
  21.             "28958677""3b8f4898""6b4bb9af""c4bfe81b""66282193",  
  22.             "61d809cc""fb21a991""487cac60""5dec8032""ef845d5d",  
  23.             "e98575b1""dc262302""eb651b88""23893e81""d396acc5",  
  24.             "0f6d6ff3""83f44239""2e0b4482""a4842004""69c8f04a",  
  25.             "9e1f9b5e""21c66842""f6e96c9a""670c9c61""abd388f0",  
  26.             "6a51a0d2""d8542f68""960fa728""ab5133a3""6eef0b6c",  
  27.             "137a3be4""ba3bf050""7efb2a98""a1f1651d""39af0176",  
  28.             "66ca593e""82430e88""8cee8619""456f9fb4""7d84a5c3",  
  29.             "3b8b5ebe""e06f75d8""85c12073""401a449f""56c16aa6",  
  30.             "4ed3aa62""363f7706""1bfedf72""429b023d""37d0d724",  
  31.             "d00a1248""db0fead3""49f1c09b""075372c9""80991b7b",  
  32.             "25d479d8""f6e8def7""e3fe501a""b6794c3b""976ce0bd",  
  33.             "04c006ba""c1a94fb6""409f60c4""5e5c9ec2""196a2463",  
  34.             "68fb6faf""3e6c53b5""1339b2eb""3b52ec6f""6dfc511f",  
  35.             "9b30952c""cc814544""af5ebd09""bee3d004""de334afd",  
  36.             "660f2807""192e4bb3""c0cba857""45c8740f""d20b5f39",  
  37.             "b9d3fbdb""5579c0bd""1a60320a""d6a100c6""402c7279",  
  38.             "679f25fe""fb1fa3cc""8ea5e9f8""db3222f8""3c7516df",  
  39.             "fd616b15""2f501ec8""ad0552ab""323db5fa""fd238760",  
  40.             "53317b48""3e00df82""9e5c57bb""ca6f8ca0""1a87562e",  
  41.             "df1769db""d542a8f6""287effc3""ac6732c6""8c4f5573",  
  42.             "695b27b0""bbca58c8""e1ffa35d""b8f011a0""10fa3d98",  
  43.             "fd2183b8""4afcb56c""2dd1d35b""9a53e479""b6f84565",  
  44.             "d28e49bc""4bfb9790""e1ddf2da""a4cb7e33""62fb1341",  
  45.             "cee4c6e8""ef20cada""36774c01""d07e9efe""2bf11fb4",  
  46.             "95dbda4d""ae909198""eaad8e71""6b93d5a0""d08ed1d0",  
  47.             "afc725e0""8e3c5b2f""8e7594b7""8ff6e2fb""f2122b64",  
  48.             "8888b812""900df01c""4fad5ea0""688fc31c""d1cff191",  
  49.             "b3a8c1ad""2f2f2218""be0e1777""ea752dfe""8b021fa1",  
  50.             "e5a0cc0f""b56f74e8""18acf3d6""ce89e299""b4a84fe0",  
  51.             "fd13e0b7""7cc43b81""d2ada8d9""165fa266""80957705",  
  52.             "93cc7314""211a1477""e6ad2065""77b5fa86""c75442f5",  
  53.             "fb9d35cf""ebcdaf0c""7b3e89a0""d6411bd3""ae1e7e49",  
  54.             "00250e2d""2071b35e""226800bb""57b8e0af""2464369b",  
  55.             "f009b91e""5563911d""59dfa6aa""78c14389""d95a537f",  
  56.             "207d5ba2""02e5b9c5""83260376""6295cfa9""11c81968",  
  57.             "4e734a41""b3472dca""7b14a94a""1b510052""9a532915",  
  58.             "d60f573f""bc9bc6e4""2b60a476""81e67400""08ba6fb5",  
  59.             "571be91f""f296ec6b""2a0dd915""b6636521""e7b9f9b6",  
  60.             "ff34052e""c5855664""53b02d5d""a99f8fa1""08ba4799",  
  61.             "6e85076a" },  
  62.             { "4b7a70e9""b5b32944""db75092e""c4192623""ad6ea6b0",  
  63.             "49a7df7d""9cee60b8""8fedb266""ecaa8c71""699a17ff",  
  64.             "5664526c""c2b19ee1""193602a5""75094c29""a0591340",  
  65.             "e4183a3e""3f54989a""5b429d65""6b8fe4d6""99f73fd6",  
  66.             "a1d29c07""efe830f5""4d2d38e6""f0255dc1""4cdd2086",  
  67.             "8470eb26""6382e9c6""021ecc5e""09686b3f""3ebaefc9",  
  68.             "3c971814""6b6a70a1""687f3584""52a0e286""b79c5305",  
  69.             "aa500737""3e07841c""7fdeae5c""8e7d44ec""5716f2b8",  
  70.             "b03ada37""f0500c0d""f01c1f04""0200b3ff""ae0cf51a",  
  71.             "3cb574b2""25837a58""dc0921bd""d19113f9""7ca92ff6",  
  72.             "94324773""22f54701""3ae5e581""37c2dadc""c8b57634",  
  73.             "9af3dda7""a9446146""0fd0030e""ecc8c73e""a4751e41",  
  74.             "e238cd99""3bea0e2f""3280bba1""183eb331""4e548b38",  
  75.             "4f6db908""6f420d03""f60a04bf""2cb81290""24977c79",  
  76.             "5679b072""bcaf89af""de9a771f""d9930810""b38bae12",  
  77.             "dccf3f2e""5512721f""2e6b7124""501adde6""9f84cd87",  
  78.             "7a584718""7408da17""bc9f9abc""e94b7d8c""ec7aec3a",  
  79.             "db851dfa""63094366""c464c3d2""ef1c1847""3215d908",  
  80.             "dd433b37""24c2ba16""12a14d43""2a65c451""50940002",  
  81.             "133ae4dd""71dff89e""10314e55""81ac77d6""5f11199b",  
  82.             "043556f1""d7a3c76b""3c11183b""5924a509""f28fe6ed",  
  83.             "97f1fbfa""9ebabf2c""1e153c6e""86e34570""eae96fb1",  
  84.             "860e5e0a""5a3e2ab3""771fe71c""4e3d06fa""2965dcb9",  
  85.             "99e71d0f""803e89d6""5266c825""2e4cc978""9c10b36a",  
  86.             "c6150eba""94e2ea78""a5fc3c53""1e0a2df4""f2f74ea7",  
  87.             "361d2b3d""1939260f""19c27960""5223a708""f71312b6",  
  88.             "ebadfe6e""eac31f66""e3bc4595""a67bc883""b17f37d1",  
  89.             "018cff28""c332ddef""be6c5aa5""65582185""68ab9802",  
  90.             "eecea50f""db2f953b""2aef7dad""5b6e2f84""1521b628",  
  91.             "29076170""ecdd4775""619f1510""13cca830""eb61bd96",  
  92.             "0334fe1e""aa0363cf""b5735c90""4c70a239""d59e9e0b",  
  93.             "cbaade14""eecc86bc""60622ca7""9cab5cab""b2f3846e",  
  94.             "648b1eaf""19bdf0ca""a02369b9""655abb50""40685a32",  
  95.             "3c2ab4b3""319ee9d5""c021b8f7""9b540b19""875fa099",  
  96.             "95f7997e""623d7da8""f837889a""97e32d77""11ed935f",  
  97.             "16681281""0e358829""c7e61fd6""96dedfa1""7858ba99",  
  98.             "57f584a5""1b227263""9b83c3ff""1ac24696""cdb30aeb",  
  99.             "532e3054""8fd948e4""6dbc3128""58ebf2ef""34c6ffea",  
  100.             "fe28ed61""ee7c3c73""5d4a14d9""e864b7e3""42105d14",  
  101.             "203e13e0""45eee2b6""a3aaabea""db6c4f15""facb4fd0",  
  102.             "c742f442""ef6abbb5""654f3b1d""41cd2105""d81e799e",  
  103.             "86854dc7""e44b476a""3d816250""cf62a1f2""5b8d2646",  
  104.             "fc8883a0""c1c7b6a3""7f1524c3""69cb7492""47848a0b",  
  105.             "5692b285""095bbf00""ad19489d""1462b174""23820e00",  
  106.             "58428d2a""0c55f5ea""1dadf43e""233f7061""3372f092",  
  107.             "8d937e41""d65fecf1""6c223bdb""7cde3759""cbee7460",  
  108.             "4085f2a7""ce77326e""a6078084""19f8509e""e8efd855",  
  109.             "61d99735""a969a7aa""c50c06c2""5a04abfc""800bcadc",  
  110.             "9e447a2e""c3453484""fdd56705""0e1e9ec9""db73dbd3",  
  111.             "105588cd""675fda79""e3674340""c5c43465""713e38d8",  
  112.             "3d28f89e""f16dff20""153e21e7""8fb03d4a""e6e39f2b",  
  113.             "db83adf7" },  
  114.             { "e93d5a68""948140f7""f64c261c""94692934""411520f7",  
  115.             "7602d4f7""bcf46b2e""d4a20068""d4082471""3320f46a",  
  116.             "43b7d4b7""500061af""1e39f62e""97244546""14214f74",  
  117.             "bf8b8840""4d95fc1d""96b591af""70f4ddd3""66a02f45",  
  118.             "bfbc09ec""03bd9785""7fac6dd0""31cb8504""96eb27b3",  
  119.             "55fd3941""da2547e6""abca0a9a""28507825""530429f4",  
  120.             "0a2c86da""e9b66dfb""68dc1462""d7486900""680ec0a4",  
  121.             "27a18dee""4f3ffea2""e887ad8c""b58ce006""7af4d6b6",  
  122.             "aace1e7c""d3375fec""ce78a399""406b2a42""20fe9e35",  
  123.             "d9f385b9""ee39d7ab""3b124e8b""1dc9faf7""4b6d1856",  
  124.             "26a36631""eae397b2""3a6efa74""dd5b4332""6841e7f7",  
  125.             "ca7820fb""fb0af54e""d8feb397""454056ac""ba489527",  
  126.             "55533a3a""20838d87""fe6ba9b7""d096954b""55a867bc",  
  127.             "a1159a58""cca92963""99e1db33""a62a4a56""3f3125f9",  
  128.             "5ef47e1c""9029317c""fdf8e802""04272f70""80bb155c",  
  129.             "05282ce3""95c11548""e4c66d22""48c1133f""c70f86dc",  
  130.             "07f9c9ee""41041f0f""404779a4""5d886e17""325f51eb",  
  131.             "d59bc0d1""f2bcc18f""41113564""257b7834""602a9c60",  
  132.             "dff8e8a3""1f636c1b""0e12b4c2""02e1329e""af664fd1",  
  133.             "cad18115""6b2395e0""333e92e1""3b240b62""eebeb922",  
  134.             "85b2a20e""e6ba0d99""de720c8c""2da2f728""d0127845",  
  135.             "95b794fd""647d0862""e7ccf5f0""5449a36f""877d48fa",  
  136.             "c39dfd27""f33e8d1e""0a476341""992eff74""3a6f6eab",  
  137.             "f4f8fd37""a812dc60""a1ebddf8""991be14c""db6e6b0d",  
  138.             "c67b5510""6d672c37""2765d43b""dcd0e804""f1290dc7",  
  139.             "cc00ffa3""b5390f92""690fed0b""667b9ffb""cedb7d9c",  
  140.             "a091cf0b""d9155ea3""bb132f88""515bad24""7b9479bf",  
  141.             "763bd6eb""37392eb3""cc115979""8026e297""f42e312d",  
  142.             "6842ada7""c66a2b3b""12754ccc""782ef11c""6a124237",  
  143.             "b79251e7""06a1bbe6""4bfb6350""1a6b1018""11caedfa",  
  144.             "3d25bdd8""e2e1c3c9""44421659""0a121386""d90cec6e",  
  145.             "d5abea2a""64af674e""da86a85f""bebfe988""64e4c3fe",  
  146.             "9dbc8057""f0f7c086""60787bf8""6003604d""d1fd8346",  
  147.             "f6381fb0""7745ae04""d736fccc""83426b33""f01eab71",  
  148.             "b0804187""3c005e5f""77a057be""bde8ae24""55464299",  
  149.             "bf582e61""4e58f48f""f2ddfda2""f474ef38""8789bdc2",  
  150.             "5366f9c3""c8b38e74""b475f255""46fcd9b9""7aeb2661",  
  151.             "8b1ddf84""846a0e79""915f95e2""466e598e""20b45770",  
  152.             "8cd55591""c902de4c""b90bace1""bb8205d0""11a86248",  
  153.             "7574a99e""b77f19b6""e0a9dc09""662d09a1""c4324633",  
  154.             "e85a1f02""09f0be8c""4a99a025""1d6efe10""1ab93d1d",  
  155.             "0ba5a4df""a186f20f""2868f169""dcb7da83""573906fe",  
  156.             "a1e2ce9b""4fcd7f52""50115e01""a70683fa""a002b5c4",  
  157.             "0de6d027""9af88c27""773f8641""c3604c06""61a806b5",  
  158.             "f0177a28""c0f586e0""006058aa""30dc7d62""11e69ed7",  
  159.             "2338ea63""53c2dd94""c2c21634""bbcbee56""90bcb6de",  
  160.             "ebfc7da1""ce591d76""6f05e409""4b7c0188""39720a3d",  
  161.             "7c927c24""86e3725f""724d9db9""1ac15bb4""d39eb8fc",  
  162.             "ed545578""08fca5b5""d83d7cd3""4dad0fc4""1e50ef5e",  
  163.             "b161e6f8""a28514d9""6c51133c""6fd5c7e7""56e14ec4",  
  164.             "362abfce""ddc6c837""d79a3234""92638212""670efa8e",  
  165.             "406000e0" },  
  166.             { "3a39ce37""d3faf5cf""abc27737""5ac52d1b""5cb0679e",  
  167.             "4fa33742""d3822740""99bc9bbe""d5118e9d""bf0f7315",  
  168.             "d62d1c7e""c700c47b""b78c1b6b""21a19045""b26eb1be",  
  169.             "6a366eb4""5748ab2f""bc946e79""c6a376d2""6549c2c8",  
  170.             "530ff8ee""468dde7d""d5730a1d""4cd04dc6""2939bbdb",  
  171.             "a9ba4650""ac9526e8""be5ee304""a1fad5f0""6a2d519a",  
  172.             "63ef8ce2""9a86ee22""c089c2b8""43242ef6""a51e03aa",  
  173.             "9cf2d0a4""83c061ba""9be96a4d""8fe51550""ba645bd6",  
  174.             "2826a2f9""a73a3ae1""4ba99586""ef5562e9""c72fefd3",  
  175.             "f752f7da""3f046f69""77fa0a59""80e4a915""87b08601",  
  176.             "9b09e6ad""3b3ee593""e990fd5a""9e34d797""2cf0b7d9",  
  177.             "022b8b51""96d5ac3a""017da67d""d1cf3ed6""7c7d2d28",  
  178.             "1f9f25cf""adf2b89b""5ad6b472""5a88f54c""e029ac71",  
  179.             "e019a5e6""47b0acfd""ed93fa9b""e8d3c48d""283b57cc",  
  180.             "f8d56629""79132e28""785f0191""ed756055""f7960e44",  
  181.             "e3d35e8c""15056dd4""88f46dba""03a16125""0564f0bd",  
  182.             "c3eb9e15""3c9057a2""97271aec""a93a072a""1b3f6d9b",  
  183.             "1e6321f5""f59c66fb""26dcf319""7533d928""b155fdf5",  
  184.             "03563482""8aba3cbb""28517711""c20ad9f8""abcc5167",  
  185.             "ccad925f""4de81751""3830dc8e""379d5862""9320f991",  
  186.             "ea7a90c2""fb3e7bce""5121ce64""774fbe32""a8b6e37e",  
  187.             "c3293d46""48de5369""6413e680""a2ae0810""dd6db224",  
  188.             "69852dfd""09072166""b39a460a""6445c0dd""586cdecf",  
  189.             "1c20c8ae""5bbef7dd""1b588d40""ccd2017f""6bb4e3bb",  
  190.             "dda26a7e""3a59ff45""3e350a44""bcb4cdd5""72eacea8",  
  191.             "fa6484bb""8d6612ae""bf3c6f47""d29be463""542f5d9e",  
  192.             "aec2771b""f64e6370""740e0d8d""e75b1357""f8721671",  
  193.             "af537d5d""4040cb08""4eb4e2cc""34d2466a""0115af84",  
  194.             "e1b00428""95983a1d""06b89fb4""ce6ea048""6f3f3b82",  
  195.             "3520ab82""011a1d4b""277227f8""611560b1""e7933fdc",  
  196.             "bb3a792b""344525bd""a08839e1""51ce794b""2f32c9b7",  
  197.             "a01fbac9""e01cc87e""bcc7d1f6""cf0111c3""a1e8aac7",  
  198.             "1a908749""d44fbd9a""d0dadecb""d50ada38""0339c32a",  
  199.             "c6913667""8df9317c""e0b12b4f""f79e59b7""43f5bb3a",  
  200.             "f2d519ff""27d9459c""bf97222c""15e6fc2a""0f91fc71",  
  201.             "9b941525""fae59361""ceb69ceb""c2a86459""12baa8d1",  
  202.             "b6c1075e""e3056a0c""10d25065""cb03a442""e0ec6e0e",  
  203.             "1698db3b""4c98a0be""3278e964""9f1f9532""e0d392df",  
  204.             "d3a0342b""8971f21e""1b0a7441""4ba3348c""c5be7120",  
  205.             "c37632d8""df359f8d""9b992f2e""e60b6f47""0fe3f11d",  
  206.             "e54cda54""1edad891""ce6279cf""cd3e7e6f""1618b166",  
  207.             "fd2c1d05""848fd2c5""f6fb2299""f523f357""a6327623",  
  208.             "93a83531""56cccd02""acf08162""5a75ebb5""6e163697",  
  209.             "88d273cc""de966292""81b949d0""4c50901b""71c65614",  
  210.             "e6c6c7bd""327a140a""45e1d006""c3f27b9a""c9aa53fd",  
  211.             "62a80f00""bb25bfe2""35bdd2f6""71126905""b2040222",  
  212.             "b6cbcf7c""cd769c2b""53113ec0""1640e3d3""38abbd60",  
  213.             "2547adf0""ba38209c""f746ce76""77afa1c5""20756060",  
  214.             "85cbfe4e""8ae88dd8""7aaaf9b0""4cf9aa7e""1948c25c",  
  215.             "02fb8a8c""01c36ae4""d6ebe1f9""90d4f869""a65cdea0",  
  216.             "3f09252d""c208e69f""b74e6132""ce77e25b""578fdfe3",  
  217.             "3ac372e6" } };  
  218.   
  219.     // Subkeys initialisation with digits of pi.  
  220.     String P[] = { "243f6a88""85a308d3""13198a2e""03707344""a4093822",  
  221.                 "299f31d0""082efa98""ec4e6c89""452821e6""38d01377",  
  222.                 "be5466cf""34e90c6c""c0ac29b7""c97c50dd""3f84d5b5",  
  223.                 "b5470917""9216d5d9""8979fb1b" };  
  224.   
  225.     // to store 2^32(for addition modulo 2^32).  
  226.     long modVal = 1;  
  227.   
  228.     // to convert hexadecimal to binary.  
  229.     private String hexToBin(String plainText)  
  230.     {  
  231.         String binary = "";  
  232.         Long num;  
  233.         String binary4B;  
  234.         int n = plainText.length();  
  235.         for (int i = 0; i < n; i++) {  
  236.             num  
  237.                 = Long.parseUnsignedLong(  
  238.                     plainText.charAt(i) + "", 16);  
  239.             binary4B = Long.toBinaryString(num);  
  240.   
  241.             // each value in hexadecimal  
  242.             // is 4 bits in binary.  
  243.             binary4B = "0000" + binary4B;  
  244.             binary4B  
  245.                 = binary4B.substring(  
  246.                     binary4B.length() - 4);  
  247.             binary += binary4B;  
  248.         }  
  249.         return binary;  
  250.     }  
  251.   
  252.     // convert from binary to hexadecimal.  
  253.     private String binToHex(String plainText)  
  254.     {  
  255.         long num = Long.parseUnsignedLong(plainText, 2);  
  256.         String hexa = Long.toHexString(num);  
  257.         while (hexa.length() < (plainText.length() / 4))  
  258.   
  259.             // maintain output length same length  
  260.             // as input by appending leading zeroes.  
  261.             hexa = "0" + hexa;  
  262.         return hexa;  
  263.     }  
  264.   
  265.     // xor two hexadecimal strings of same length.  
  266.     private String xor(String a, String b)  
  267.     {  
  268.         a = hexToBin(a);  
  269.         b = hexToBin(b);  
  270.         String ans = "";  
  271.         for (int i = 0; i < a.length(); i++)  
  272.             ans += (char)(((a.charAt(i) - '0')  
  273.                         ^ (b.charAt(i) - '0'))  
  274.                         + '0');  
  275.         ans = binToHex(ans);  
  276.         return ans;  
  277.     }  
  278.   
  279.     // addition modulo 2^32 of two hexadecimal strings.  
  280.     private String addBin(String a, String b)  
  281.     {  
  282.         String ans = "";  
  283.         long n1 = Long.parseUnsignedLong(a, 16);  
  284.         long n2 = Long.parseUnsignedLong(b, 16);  
  285.         n1 = (n1 + n2) % modVal;  
  286.         ans = Long.toHexString(n1);  
  287.         ans = "00000000" + ans;  
  288.         return ans.substring(ans.length() - 8);  
  289.     }  
  290.   
  291.     // function F explained above.  
  292.     private String f(String plainText)  
  293.     {  
  294.         String a[] = new String[4];  
  295.         String ans = "";  
  296.         for (int i = 0; i < 8; i += 2) {  
  297.             // the column number for S-box  
  298.             // is 8-bit value(8*4 = 32 bit plain text)  
  299.             long col  
  300.                 = Long.parseUnsignedLong(  
  301.                     hexToBin(  
  302.                         plainText.substring(i, i + 2)),  
  303.                     2);  
  304.             a[i / 2] = S[i / 2][(int)col];  
  305.         }  
  306.         ans = addBin(a[0], a[1]);  
  307.         ans = xor(ans, a[2]);  
  308.         ans = addBin(ans, a[3]);  
  309.         return ans;  
  310.     }  
  311.   
  312.     // generate subkeys.  
  313.     private void keyGenerate(String key)  
  314.     {  
  315.         int j = 0;  
  316.         for (int i = 0; i < P.length; i++) {  
  317.             P[i] = xor(P[i], key.substring(j, j + 8));  
  318.             System.out.println("subkey "  
  319.                             + (i + 1) + ": "  
  320.                             + P[i]);  
  321.             j = (j + 8) % key.length();  
  322.         }  
  323.     }  
  324.   
  325.     // round function  
  326.     private String round(int time, String plainText)  
  327.     {  
  328.         String left, right;  
  329.         left = plainText.substring(08);  
  330.         right = plainText.substring(816);  
  331.         left = xor(left, P[time]);  
  332.         String fOut = f(left); // output from F function  
  333.         right = xor(fOut, right);  
  334.   
  335.         System.out.println("round " + time + ": "  
  336.                         + right + left);  
  337.   
  338.         // swap left and right  
  339.         return right + left;  
  340.     }  
  341.   
  342.     // decryption  
  343.     private String decrypt(String plainText)  
  344.     {  
  345.         for (int i = 17; i > 1; i--)  
  346.             plainText = round(i, plainText);  
  347.   
  348.         // postprocessing  
  349.         String right = plainText.substring(08);  
  350.         String left = plainText.substring(816);  
  351.         right = xor(right, P[1]);  
  352.         left = xor(left, P[0]);  
  353.         return left + right;  
  354.     }  
  355.   
  356.     Main()  
  357.     {  
  358.         // storing 2^32 in modVal  
  359.         //(<<1 is equivalent to multiply by 2)  
  360.         for (int i = 0; i < 32; i++)  
  361.             modVal = modVal << 1;  
  362.         String cipherText = "d748ec383d3405f7";  
  363.         String key = "aabb09182736ccdd";  
  364.   
  365.         keyGenerate(key);  
  366.   
  367.         System.out.println("-----Decryption-----");  
  368.         String plainText = decrypt(cipherText);  
  369.         System.out.println("Plain Text: "  
  370.                         + plainText);  
  371.     }  
  372.   
  373.     public static void main(String args[])  
  374.     {  
  375.         new Main();  
  376.     }  
  377. }  

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
Plain Text: 123456abcd132536

Benefits and Drawbacks of the Blowfish Algorithm

  • Blowfish is a quick block cypher, barring key changes. Pre-processing needed for each new key is equal to 4KB of text.
  • Compared to DES Encryption, it is significantly quicker and superior.
  • Due to its usage of a 64-bit block size, blowfish is susceptible to birthday attacks.
  • Blowingfish with fewer rounds has been shown to be vulnerable to well-known plain text assaults (2nd order differential attacks - 4 rounds).

Uses for the Blowfish Algorithm

  • Bulk Encryption
  • Password hash
  • Packet Encryption (ATM packets)

No comments:

Post a Comment