-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryptFile.java
109 lines (96 loc) · 3.73 KB
/
EncryptFile.java
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.lang.*;
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class EncryptFile {
public Cipher cipher = null;
public static SecretKey secretKey;
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException {
Scanner sc=new Scanner(System.in);
String fileToEncrypt = args[0];
String directoryPath = "C:/xampp/htdocs/BlowTransfer/new/";
String outdirectoryPath = "c:/xampp/htdocs/BlowTransfer/uploads/";
//String directoryPath1 = "C:/Users/ypila/Desktop/blow/abc.png";
// C:/Users/ypila/Desktop/blow
//C:\Users\ypila\Desktop\blow
//String ss= new File(directoryPath1).getName();
// System.out.println(ss);
//String ext = ss.substring(ss.lastIndexOf(".") + 1);
// System.out.println("<br>"+ext);
EncryptFile encryptFile = new EncryptFile();
// System.out.println("Starting Encryption...");
// System.out.println("Enter the secret key");
String key="Shifa";
SecretKey s=encryptFile.secret(key);
encryptFile.encrypt(directoryPath + fileToEncrypt, outdirectoryPath + fileToEncrypt , s);
System.out.print("Encryption completed...");
}
public SecretKey secret(String k) throws NoSuchAlgorithmException, NoSuchPaddingException
{
String Key = k;
byte[] KeyData = Key.getBytes();
SecretKey secretKey = new SecretKeySpec(KeyData, "Blowfish");
// System.out.println("this is secret key" +secretKey.toString());
return secretKey;
}
/**
*
* @param srcPath
* @param destPath
*
* Encrypts the file in srcPath and creates a file in destPath
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
*/
private void encrypt(String srcPath, String destPath, SecretKey s) throws NoSuchAlgorithmException, NoSuchPaddingException {
File rawFile = new File(srcPath);
File encryptedFile = new File(destPath);
InputStream inStream = null;
OutputStream outStream = null;
try {
/**
* Initialize the cipher for encryption
*/
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, s);
/**
* Initialize input and output streams
*/
inStream = new FileInputStream(rawFile);
outStream = new FileOutputStream(encryptedFile);
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) > 0) {
outStream.write(cipher.update(buffer, 0, len));
outStream.flush();
}
outStream.write(cipher.doFinal());
inStream.close();
outStream.close();
} catch (IllegalBlockSizeException ex) {
System.out.println(ex);
} catch (BadPaddingException ex) {
System.out.println(ex);
} catch (InvalidKeyException ex) {
System.out.println(ex);
} catch (FileNotFoundException ex) {
System.out.println(ex);
} catch (IOException ex) {
System.out.println(ex);
}
}
}