Cryptography Lesson - Basic Blowfish Algorithm using Java

in #utopian-io7 years ago (edited)

What is The Purpose of This Lesson?

  • How to implement Blowfish Algorithm using Java.

Requirements

  • Netbeans IDE 8.1 with Java JDK.

Difficulty

  • Basic

Preliminary Words

Blowfish is a symmetric block cipher cryptographic algorithm with a fixed block length of 64 bits long. The algorithm also applies key techniques of any size. The key size that can be received by blowfish is between 32 to 448 bits, with a standard size of 128 bits. Blowfish utilizes bit manipulation techniques and repeat play and key rotation techniques performed 16 times. The main algorithm is divided into two main sub-algorithms, namely the key expansion part and the data encryption-decryption section.

blowfishtopology.png

Key expansions are done at the beginning with a key input of 32 to 448 bits long, and the output is a subkey array with a total of 4168 bytes. The data encryption-decryption section takes place by utilizing 16 times looping against the feistel network. Each loop consists of a permutation with inputs being key, and data substitution. All operations are performed by utilizing xor and additions. The addition operation is performed on four lookup rows that are performed each round.

So Lets Do It.

Step by Step

1. The first step to do is to open the Netbeans IDE application because this project is created using that application.
open netbeans.png

2. Once open, click file and select New Project.
newproject.png

3. Then the New Project window will appear. In the Categories box, select Java. In the Projects box, select Java Application. When finished click Next.
newprojectwindow.png

4. Then go to the New Java Application window. In the Project Name box, fill in the name of the project we created under the name "BlowfishAlgorithm". In the Project Location box and Project Folder just leave and do not need to be changed because the project will be stored in the Documents folder in directory C. When finished click Finish.
newjavaapp.png

5. After the project is created, the project list will appear named (BlowFishAlgoritma) along with the editor page to type the program code later. Keep in mind, the program code that we create later in the Java Class named BlowfishAlgorithm.java that exist in this project.
mainpage.png

6. Next, type the program code below first and ignore if there is error later.

public static void main(String[] args) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("blowfish");
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("blowfish");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
Scanner sc = new Scanner(System.in);
System.out.print("Enter the words want you to Encrypt : ");
String inputText = sc.nextLine();
byte[] encrypt = cipher.doFinal(inputText.getBytes());
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decrypt = cipher.doFinal(encrypt);
System.out.println("Words After Encryption : " + new String(encrypt));
System.out.println("Words After Decryption : " + new String(decrypt));
}

Then the temporary view as below.
code.png

7. The program code is still error because there is a light sign with red dots on the program number line. Move the cursor until a comment appears from the error symbol.
error.png

8. Then click on the symbol to see what description the program line error requested.

  • On the 6th line is required additional import javax.crypto.KeyGenerator.
    baris6.png

  • On the 7th line is required additional import javax.crypto.SecretKey.
    baris7.png

  • On the 10th line is required additional import javax.crypto.Chiper.
    baris10.png

  • On the 11th line is required additional import javax.crypto.Chiper.
    baris11.png

  • On the 12th line is required additional import java.util.Scanner.
    baris12.png

  • On the 18th line is required additional import javax.crypto.Chiper.
    baris18.png

After knowing how to remove errors in the program code, do the import additions for all errors.

9. Add all necessary imports so that errors in the program code will be lost. Import will appear at the top.
import.png

10. After all the errors disappear, run the program by right click on java class BlowfishAlgorithm.java and select Run File or can also press Shift + F6.
run.png

11. Then the output window will appear at the bottom. The first command is to tell us to enter the word you want to encrypt. After the word is inserted, press Enter then the text encryption process takes place that appears strange characters. Continued also by the process of decryption automatically where the word is in the encryption was returned to the original word.
result.png

Curriculum

This is the first tutorial i'm contributing using Java.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Nice report brother . I support you @simpleawesome

Your contribution cannot be approved because it does not follow the Utopian Rules.

Trivial on-screen installation processes are not acceptable as valid tutorials.

You can't really call this a tutorial, you just show how to create a project, give the code and then remove the import errors.

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.030
BTC 68608.97
ETH 3280.67
USDT 1.00
SBD 2.74