-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlowfish.java
More file actions
94 lines (78 loc) · 3.4 KB
/
Blowfish.java
File metadata and controls
94 lines (78 loc) · 3.4 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.lang.*;
import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.SecureRandom;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
class Blowfish
{
private static Cipher encryptCipher;
private static Cipher decryptCipher;
public static void main(String[] args) throws Exception {
String clearTextFile = args[0];
String cipherTextFile = args[1];
String clearTextNewFile = args[2];
KeyGenerator gen = KeyGenerator.getInstance("Blowfish");
gen.init(56);
SecretKey secret_key = gen.generateKey();
// Cipher ciph = Cipher.getInstance("AES")
// String keytext = "mykeyis";
// SecretKey secret_key = new SecretKeySpec(keytext.getBytes(), "Blowfish");
// System.out.println(secret_key.toString());
// KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
// SecureRandom secureRandom = new SecureRandom();
// int keyBitSize = 56;
// keyGenerator.init(keyBitSize,secureRandom);
// SecretKey secret_key = keyGenerator.generateKey();
// System.out.println(secret_key);
encryptCipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, secret_key);
blowfishEncrypt(new FileInputStream(clearTextFile), new FileOutputStream(cipherTextFile));
// System.out.println(encryptCipher.getBlockSize());
decryptCipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE, secret_key);
blowfishDecrypt(new FileInputStream(cipherTextFile) , new FileOutputStream(clearTextNewFile));
}
public static void blowfishEncrypt(InputStream is, OutputStream os) throws IOException {
long startTime = System.nanoTime();
// create CipherOutputStream to encrypt the data using encryptCipher
//CipherOutputStream c
os = new CipherOutputStream(os, encryptCipher);
writeData(is, os);
long endTime = System.nanoTime();
long totalTime = (endTime - startTime)/1000000;
System.out.println(totalTime);
}
public static void blowfishDecrypt(InputStream is, OutputStream os) throws IOException {
long startTime = System.nanoTime();
//CipherInputStream c
is = new CipherInputStream(is, decryptCipher);
writeData(is,os);
//writeDecryptData(cis, os);
long endTime = System.nanoTime();
long totalTime = (endTime - startTime)/1000000;
System.out.println(totalTime);
}
// utility method to read data from input stream and write to output stream
private static void writeData(InputStream is, OutputStream os) throws IOException {
byte[] buf = new byte[1024];
int numRead = 0;
// read and write operation
while ((numRead = is.read(buf)) >= 0) {
os.write(buf, 0, numRead);
}
os.close();
is.close();
}
/*private static void writeDecryptData(CipherInputStream is, OutputStream os) throws IOException {
byte[] buf = new byte[1024];
int numRead = 0;
// read and write operation
while ((numRead = is.read(buf)) >= 0) {
os.write(buf, 0, numRead);
}
os.close();
is.close();
}*/
}