forked from kasundezoysa/javacrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecrypt.java
67 lines (48 loc) · 1.29 KB
/
Decrypt.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
/*
Kasun De Zoysa @ UCSC
*/
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class Decrypt {
public static void main(String args[]) {
if(args.length<1) {
System.out.println("Usage: Decrypt ciphertext plaintext");
return;
}
try {
//Read a password
System.out.print("Enter Your Password :");
char [] ch = System.console().readPassword();
String s= new String(ch);
//Generate a key
SecureRandom sr1 = SecureRandom.getInstance("SHA1PRNG");
sr1.setSeed(s.getBytes());
byte[] k = new byte[128/8];
sr1.nextBytes(k);
SecretKeySpec decKey = new SecretKeySpec(k,"AES");
// init encryption
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, decKey);
// Open input file
FileInputStream fis = new FileInputStream(args[0]);
CipherInputStream cIn = new CipherInputStream(fis,cipher);
// Open output file
FileOutputStream fos = new FileOutputStream(args[1]);
byte[] b = new byte[8];
int i = cIn.read(b);
while (i != -1) {
fos.write(b, 0, i);
i = cIn.read(b);
}
fos.close();
//Delete the cipher text file
File f=new File(args[0]);
f.delete();
} catch (Exception e) {
System.out.println(e);
}
System.out.println("Decrypted!");
}
}