-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPBEsDecryptionMode.java
More file actions
60 lines (49 loc) · 3.38 KB
/
Copy pathPBEsDecryptionMode.java
File metadata and controls
60 lines (49 loc) · 3.38 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
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.security.Key;
import java.util.Arrays;
//implementation by kelechi wisdom
public class PBEsDecryptionMode {
public static void main(String[] args) throws Exception {
long startTimeOfDecryption = System.nanoTime();
PBEKeySpec keySpecification;
PBEParameterSpec parameterSpecification;
SecretKeyFactory factoryForKeyGen;
/* Given that this is the decryption version of PBEs.java
* im hardcoding the same salt, iteration count and password */
byte[] salt = {(byte) 0xc7, (byte) 0x73, (byte) 0x21,
(byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99};
int hashCount = 2048;
/*specifying and bundling the ingredients (Salt and hashcount)
* that will go into making the key, as one object called "parameterSpecification" */
parameterSpecification = new PBEParameterSpec(salt, hashCount);
//re-hardcoding the same password again (alternatively i could ask the user for it)
char[] passwordToMakeKey = "%O^t#2Fv0JUjVdRV2RW%".toCharArray();
keySpecification = new PBEKeySpec(passwordToMakeKey);
//initializing the key factory that will generate a key from this password, and specifying that its a PBE With MD5 and DES algorithm
factoryForKeyGen = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
Key encryptionKey = factoryForKeyGen.generateSecret(keySpecification);
long timeTakenToGenerateKey = System.nanoTime();
System.out.println("Key Generated! Time taken to generate key: " + (timeTakenToGenerateKey-startTimeOfDecryption) + " nanoseconds.");
/* initializing a cipher object and specifying what cipher im using to decrypt the ciphertext
* (has to be same as the algo used in the encryption algorithm) */
Cipher pbeDecryptionCipher = Cipher.getInstance("PBEWithMD5AndDES");
/* Initialize PBE Cipher with key and parameter -
* Just initializes a cipher object by specifying that it is encrypting with a key, salt that is added to the key before being hashed
* and iteration count for how many times it will be hashed to make the ciphertext */
pbeDecryptionCipher.init(Cipher.DECRYPT_MODE, encryptionKey, parameterSpecification);
//note cipher result in PBEs.java was: 546267e085c78b62797a6a9f4c64a2d2d78d2c9dc980e22f
//byte[] cipherText = Utils.toByteArray("546267e085c78b62797a6a9f4c64a2d2d78d2c9dc980e22f");
/* for some reason my toByteArray isn't working so im slightly confused so ill just directly make the byte array its encrypted version
of the plaintext from the PBEs.java file in here, as opposed to directly converting it*/
byte[] cipherText = {-49, 5, -19, -26, -58, 88, 4, -103, -109, -118, -106, -59, -21, 100, -110, -117, 60, -95, -68, 22, 19, -27, -40, -124};
byte[] plainText = pbeDecryptionCipher.doFinal(cipherText);
long endTimeOfDecryption = System.nanoTime();
//System.out.println("plainText : " + Utils.toHex(plainText));
System.out.println("plainText: " + new String(plainText));
System.out.println("This decryption took " + (endTimeOfDecryption - startTimeOfDecryption) + " nanoseconds!");
//measuring the running time of decryption
}
}