blowfish_with_usrgenkey.java
32 linesjava
DOWNLOAD
1// Aim: Program to perform Blowfish encryption with user-generated key (Blowfish with User-Generated Key).
2import javax.crypto.spec.*;
3import javax.crypto.*;
4import java.util.*;
5
6public class blowfish_with_usrgenkey {
7    public static void main(String[] args)throws Exception {
8        Scanner sc = new Scanner(System.in);
9        String text = "INFORMATION";
10        System.out.println("msg given :"+text);
11        System.out.print("Enter the Key :");
12        String usrkey = sc.nextLine();
13
14        byte[] keyBytes = Arrays.copyOf(usrkey.getBytes("UTF-8"),16);
15        SecretKey key = new SecretKeySpec(keyBytes, "Blowfish");
16
17        Cipher cpr = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
18        cpr.init(Cipher.ENCRYPT_MODE,key);
19        byte[] encrypted = cpr.doFinal(text.getBytes("UTF-8"));
20        String enctext = Base64.getEncoder().encodeToString(encrypted);
21
22        cpr.init(Cipher.DECRYPT_MODE,key);
23        byte[] decrypted = cpr.doFinal(Base64.getDecoder().decode(enctext));
24        String dectext = new String(decrypted,"UTF-8");
25
26        System.out.println("Blowfish Key : "+Base64.getEncoder().encodeToString(keyBytes));
27        System.out.println("Encrypted : "+enctext);
28        System.out.println("Decrypted : "+dectext);
29
30    }
31}
32