0% found this document useful (0 votes)
355 views3 pages

BlowFish - Program

This document provides code for a Java program that implements the Blowfish encryption algorithm. The program defines encrypt and decrypt methods that take in a password string and key to encrypt or decrypt the password using Blowfish cipher. It encodes the encrypted text to base64 format. The main method demonstrates encrypting a sample password, printing the encrypted text, decrypting it and printing the original password. The output shows the password is successfully encrypted and decrypted back to the original.

Uploaded by

Akshitha Sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
355 views3 pages

BlowFish - Program

This document provides code for a Java program that implements the Blowfish encryption algorithm. The program defines encrypt and decrypt methods that take in a password string and key to encrypt or decrypt the password using Blowfish cipher. It encodes the encrypted text to base64 format. The main method demonstrates encrypting a sample password, printing the encrypted text, decrypting it and printing the original password. The output shows the password is successfully encrypted and decrypted back to the original.

Uploaded by

Akshitha Sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Cryptography & Network Security Lab MRIET

EXPERIMENT – 5:
Write a C/JAVA program to implement the Blowfish algorithm logic.

PROGRAM:
import java.io.UnsupportedEncodingException;

import java.nio.charset.Charset;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import java.util.Base64;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.spec.SecretKeySpec;

/**
* This program demonstrates how to encrypt/decrypt
* input using the Blowfish
* Cipher with the Java Cryptography.
*/
public class BlowfishDemo

public String encrypt(String password, String key) throws

NoSuchAlgorithmException, NoSuchPaddingException,

InvalidKeyException, IllegalBlockSizeException,

BadPaddingException,UnsupportedEncodingException

byte[] KeyData = key.getBytes();

JNTUH (R-18)
Cryptography & Network Security Lab MRIET

SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");

Cipher cipher = Cipher.getInstance("Blowfish");

cipher.init(Cipher.ENCRYPT_MODE, KS);

String encryptedtext =
Base64.getEncoder().encodeToString(cipher.doFinal(password.getBytes("UTF-
8")));

return encryptedtext;

public String decrypt(String encryptedtext, String key)

throws NoSuchAlgorithmException, NoSuchPaddingException,

InvalidKeyException, IllegalBlockSizeException,

BadPaddingException

byte[] KeyData = key.getBytes();

SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");

byte[] ecryptedtexttobytes =
Base64.getDecoder().decode(encryptedtext);

Cipher cipher = Cipher.getInstance("Blowfish");

cipher.init(Cipher.DECRYPT_MODE, KS);

byte[] decrypted = cipher.doFinal(ecryptedtexttobytes);

String decryptedString = new String(decrypted,


Charset.forName("UTF-8"));

return decryptedString;

public static void main(String[] args) throws Exception {

JNTUH (R-18)
Cryptography & Network Security Lab MRIET

final String password = "BlowFish@123";

final String key = "knowledgefactory";

System.out.println("Password: " + password);

BlowfishDemo obj = new BlowfishDemo();

String enc_output = obj.encrypt(password, key);

System.out.println("Encrypted text: " + enc_output);

String dec_output = obj.decrypt(enc_output, key);

System.out.println("Decrypted text: " + dec_output);

OUTPUT:
Password: BlowFish@123

Encrypted text: ss3l6WWw3VpcwVrtxD6l4w==

Decrypted text: BlowFish@123

JNTUH (R-18)

You might also like