-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlowfish.java
More file actions
73 lines (53 loc) · 2.3 KB
/
Blowfish.java
File metadata and controls
73 lines (53 loc) · 2.3 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Blowfish {
public void Blowfish_Cipher(File inputFile, File outputFile, String key) throws Exception {
try {
BlowfishdoCrypto(Cipher.ENCRYPT_MODE, inputFile, outputFile, key);// Select Mode=ENCRYPT
System.out.println("Blowfish Cipher applied Succesfully");
} catch (Exception e) {
System.out.println("\n Error at reading file from original file in Blowfish Cipher");
e.printStackTrace();
}
}
public void Blowfish_Decipher(File inputFile, File outputFile, String key) throws Exception {
try {
BlowfishdoCrypto(Cipher.DECRYPT_MODE, inputFile, outputFile, key);// Select Mode=DECRYPT
System.out.println("Blowfish Decipher applied Succesfully");
} catch (Exception e) {
System.out.println("\n Error at reading file from encrypted file in Blowfish Decipher");
e.printStackTrace();
}
}
public static void BlowfishdoCrypto(int cipherMode, File inputFile, File outputFile, String keyString)
throws Exception {
String ALGORITHM = "Blowfish";// It's used in secret key
Key secretKey = new SecretKeySpec(keyString.getBytes(), ALGORITHM);// Making S blocks
Cipher cipher = Cipher.getInstance(ALGORITHM);// getting them in p arrays
cipher.init(cipherMode, secretKey);// recreating the cipher or randomly set the IV yourself for each subsequent
// message
try {
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
// reading data in input file
byte[] outputBytes = cipher.doFinal(inputBytes);
// do final reset the internal state to the same IV you started with
// We do encryption based on bytes in Blowfish
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
// we are writing the data in the file
System.out.println(
"\n Also Blowfish cipher as been written to encrypted.txt & Decipher has been written to decrypted.txt");
inputStream.close();
outputStream.close();
} catch (Exception e) {
System.out.println("\n Error at writing file in Blowfish Cipher / Decipher");
e.printStackTrace();
}
}
}