0% found this document useful (0 votes)
7 views2 pages

Exp-07 CNS

The document provides a Java program that demonstrates how to encrypt and decrypt the text 'Hello World' using the Blowfish encryption algorithm. It includes methods for both encryption and decryption, utilizing a specified key. The output shows the encrypted and decrypted text results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Exp-07 CNS

The document provides a Java program that demonstrates how to encrypt and decrypt the text 'Hello World' using the Blowfish encryption algorithm. It includes methods for both encryption and decryption, utilizing a specified key. The output shows the encrypted and decrypted text results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

EXP-07: Using Java Cryptography, encrypt the text “Hello world” using Blow Fish.

Create your
own key using Java key tool.

package cns;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class BlowfishEncryption {

public static String encryptBlowfish(String plainText, String key) throws Exception {


SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrypted = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
}

public static String decryptBlowfish(String encryptedText, String key) throws Exception {


SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decrypted);
}

public static void main(String[] args) {


try {
String key = "451017189"; // Blowfish key must be 8+ bytes
String plainText = "Hello World";

String encryptedText = encryptBlowfish(plainText, key);


System.out.println("Blowfish Encrypted Text: " + encryptedText);

String decryptedText = decryptBlowfish(encryptedText, key);


System.out.println("Blowfish Decrypted Text: " + decryptedText);

} catch (Exception e) {
e.printStackTrace();
}
}
}

OUTPUT:
Blowfish Encrypted Text: wGa34Bdoe+23zAUyd4eWvQ==
Blowfish Decrypted Text: Hello World

You might also like