forked from developerfromjokela/abitti-decrypter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
115 lines (95 loc) · 4.35 KB
/
Main.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
110
111
112
113
114
115
//package com.developerfromjokela.opdecoder;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
public class Main {
public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA1";
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Arguments missing!");
System.out.println("for example \"palvoja jemma nuori tulli\" exam.xml.bin exam.xml");
return;
}
String pass = args[0];
pass = pass.trim();
pass = pass.replaceAll("\\s", "");
try {
System.out.println("Reading file");
byte[] fileToDecrypt = Files.readAllBytes(new File(args[1]).toPath());
byte[] generated_pbkdf2 = rawPbkdf2(pass.toCharArray(), pass.getBytes(), 2000, 32+16);
System.out.println("Calculating keys");
System.out.println("Password: " + pass);
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(generated_pbkdf2));
byte[] encKey = new byte[32];
byte[] ivArray = new byte[16];
inputStream.read(encKey, 0, 32);
inputStream.read(ivArray, 0, 16);
System.out.println("Done!");
System.out.println("Decrypting");
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(encKey, "AES"), new IvParameterSpec(ivArray));
byte[] decryptedResult = cipher.doFinal(fileToDecrypt);
System.out.println("File decrypted!");
System.out.println("Writing to file");
FileOutputStream fileOutputStream = new FileOutputStream(new File(args[2]));
fileOutputStream.write(decryptedResult);
fileOutputStream.close();
System.out.println("Done!");
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}
/**
* Computes the PBKDF2 hash of a password.
*
* @param password the password to hash.
* @param salt the salt
* @param iterations the iteration count (slowness factor)
* @param bytes the length of the hash to compute in bytes
* @return the PBDKF2 hash of the password
*/
private static byte[] pbkdf2(char[] password, byte[] salt, int iterations, int bytes)
throws InvalidKeySpecException, NoSuchAlgorithmException {
PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bytes * 8);
SecretKeyFactory skf = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);
return skf.generateSecret(spec).getEncoded();
}
/**
* Computes the PBKDF2 hash of a password.
*
* @param password the password to hash.
* @param salt the salt
* @param iterations the iteration count (slowness factor)
* @param bytes the length of the hash to compute in bytes
* @return the PBDKF2 hash of the password
*/
private static byte[] rawPbkdf2(char[] password, byte[] salt, int iterations, int bytes)
throws InvalidKeySpecException, NoSuchAlgorithmException {
PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bytes * 8);
SecretKeyFactory skf = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);
return skf.generateSecret(spec).getEncoded();
}
}