0% found this document useful (0 votes)
4 views1 page

Blowfish

The document is a Java program that demonstrates encryption and decryption using the Blowfish algorithm. It includes methods to encrypt a string with a specified key and to decrypt the encrypted data back to its original form. The main method showcases the functionality by encrypting and then decrypting a sample message.

Uploaded by

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

Blowfish

The document is a Java program that demonstrates encryption and decryption using the Blowfish algorithm. It includes methods to encrypt a string with a specified key and to decrypt the encrypted data back to its original form. The main method showcases the functionality by encrypting and then decrypting a sample message.

Uploaded by

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

import javax.crypto.

Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Main {

private static final String ALGORITHM = "Blowfish";

public static byte[] encrypt(String key, String data) throws Exception {


SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(data.getBytes());
}

public static String decrypt(String key, byte[] encryptedData) throws Exception


{
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedData = cipher.doFinal(encryptedData);
return new String(decryptedData);
}

public static void main(String[] args) {


try {
String key = "Javacodegeeks";
String data = "Hello, world!";

// Encrypt data
byte[] encryptedData =Main.encrypt(key, data);
System.out.println("Encrypted data: " + new String(encryptedData));

// Decrypt data
String decryptedData = Main.decrypt(key, encryptedData);
System.out.println("Decrypted data: " + decryptedData);
} catch (Exception e) {
}
}
}

You might also like