-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlowfish.java
More file actions
59 lines (44 loc) · 1.43 KB
/
Blowfish.java
File metadata and controls
59 lines (44 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*H****************************************************************
* FILENAME : Blowfish.java
*
* DESCRIPTION :
* Encrypts and decrpyts text using Blowfish cipher
*
* Copyright 2019, Jacob Wilkins. All rights reserved.
*
* AUTHOR : Jacob Wilkins START DATE : 5 Jun 19
*
*H*/
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;
import java.util.Base64;
public class Blowfish {
public static String Blowfish_encrypt(String text, String key) {
try {
SecretKeySpec sks = new SecretKeySpec(key.getBytes("UTF-8"), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, sks);
byte[] enc = cipher.doFinal(text.getBytes());
return Base64.getEncoder().encodeToString(enc);
} catch (Exception e) {
System.out.println("Error encrypting Blowfish.\nExiting...");
e.printStackTrace();
System.exit(0);
}
return null;
}
public static String Blowfish_decrypt(String text, String key) {
try {
SecretKeySpec sks = new SecretKeySpec(key.getBytes("UTF-8"), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, sks);
byte[] dec = cipher.doFinal(Base64.getDecoder().decode(text));
return new String(dec);
} catch (Exception e) {
System.out.println("Error Decrypting Blowfish.\nExiting...");
e.printStackTrace();
System.exit(0);
}
return null;
}
}