-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPBEs.java
More file actions
114 lines (86 loc) · 4.41 KB
/
Copy pathPBEs.java
File metadata and controls
114 lines (86 loc) · 4.41 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.security.Key;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* Example of using Password-based encryption
* Password based encryption is using a password to derive a cryptographic key
* with which one can use to encrypt plain text
*/
public class PBEs {
public static void main(String[] args)
throws Exception {
long startTimeOfProgram = System.nanoTime();
PBEKeySpec pbeKeySpec;
PBEParameterSpec pbeParamSpec;
SecretKeyFactory keyFac;
// Salt
/* This is fixed, and instead of being generated each time, the same salt
* is appended to the password before it is hashed, and a key is derived from the hashing
* it goes: password + salt → hashed many times → derived key → used to encrypt plaintext
*
*/
byte[] salt = {(byte) 0xc7, (byte) 0x73, (byte) 0x21,
(byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99};
// Iteration count.
/* This is also fixed, and its a measure of how many times a hashing function is used for a password
* The more hashes are used for a password, the harder/longer it takes to be brute-forced
*/
int count = 5000;
// Create PBE parameter set
/* Bundles the salt and iteration count into a parameter that will later be
* passed onto the cipher
*/
pbeParamSpec = new PBEParameterSpec(salt, count);
//Initialization of the password
/* The entire password (as a character array) is put into a key derivation function
* char is used simply because strings are immutable, and chars can be wiped from memory after use
*/
char[] password = "%O^t#2Fv0JUjVdRV2RW%".toCharArray();
//Create parameter for key generation
/* It tells the secretKeyFactory: "Here is the raw password material you must turn into a cryptographic key"
* basically the input spec for key generation
* Raw password → PBEKeySpec → SecretKeyFactory → actual encryption key
*/
pbeKeySpec = new PBEKeySpec(password);
// Create instance of SecretKeyFactory for password-based encryption using DES and MD5
/* Generates a DES key from the password by performing the following:
->password + salt + iteration count
→ hash repeatedly (MD5 here)
→ derive DES key
*/
keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
// Generate a real DES key derived from the password, measure time taken for it to be generated
Key pbeKey = keyFac.generateSecret(pbeKeySpec);
long timeTakenToGenerateKey = System.nanoTime();
System.out.println("Key Generated! Time taken to generate key = "
+ (timeTakenToGenerateKey-startTimeOfProgram)
+ " nanoseconds!");
// Create PBE Cipher
/* instantiates a cipher object that will be used as the algo to encrypt the plaintext
*/
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
// Initialize PBE Cipher with key and parameters
// Just initialises a cipher object by specifying that it is encrypting with a key, salt and iteration count
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
// Our plaintext
byte[] cleartext = "This is another example".getBytes();
// Encrypt the plaintext
//actually does the encryption, with the cipher that was earlier specified and assigned to variable pbeCipher
byte[] ciphertext = pbeCipher.doFinal(cleartext);
long endTimeOfProgram = System.nanoTime();
System.out.println("cipher : " + Utils.toHex(ciphertext));
/* it converts the byte array to a readable hexadecimal string
* String to byte conversion already happened in line 93
* byte[] cleartext = "This is another example".getBytes();"
*/
System.out.println("byte version is " + Arrays.toString(ciphertext));
//measuring the running time of encryption
System.out.println("Entire encryption program took " + (endTimeOfProgram - startTimeOfProgram) + " nanoseconds!");
}
}