-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandardPassword.java
More file actions
165 lines (134 loc) · 4.72 KB
/
StandardPassword.java
File metadata and controls
165 lines (134 loc) · 4.72 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
public class StandardPassword extends Password implements Encryptable, Decryptable {
private static final long serialVersionUID = 1L;
private SecretKeySpec key;
public static void main(String[] args) {
// test the class
StandardPassword pass = new StandardPassword("TestPassword1");
// should print "TestPassword1"
System.out.println(pass.getPassword());
// change the password
pass.setPassword("TestPassword2");
// should print "TestPassword2"
System.out.println(pass.getPassword());
// generate new password
pass.generatePass(15);
// print new password
System.out.println(pass.getPassword());
} // end main
public StandardPassword(String pass) {
// create random salt
SecureRandom rand = new SecureRandom();
this.salt = new byte[32];
rand.nextBytes(this.salt);
// create key
boolean keyCreated = false;
try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
byte[] keyBytes = keyGen.generateKey().getEncoded();
this.key = new SecretKeySpec(keyBytes, "AES");
keyCreated = true;
} catch (Exception e) {
System.out.println(e.getMessage());
// TODO write to error log
} // end try catch
if (keyCreated) {
// set dateSet to current date
this.dateSet = new Date();
// salt and encrypt the password
this.password = this.encrypt(pass);
} else {
System.out.println("Something went wrong while creating this account");
} // end if else
} // end constructor
public Date getDate() {
return this.dateSet;
} // end getDate
public void setPassword(String newPass) {
// set dateSet to current date
this.dateSet = new Date();
// salt and hash the password
this.password = this.encrypt(newPass);
} // end setPassword
public String getPassword() {
return this.decrypt(this.password);
} // end getPassword
public void generatePass(int length) {
PasswordGenerator generator = new PasswordGenerator();
String newPass = generator.generate(length);
System.out.println("Generated Password:");
System.out.println(newPass);
Scanner scanner = new Scanner(System.in);
boolean keepGoing = true;
while (keepGoing) {
System.out.println("\nWould you like to set this as your password? (y/n)");
String response = scanner.nextLine();
if (response.toUpperCase().equals("Y")) {
this.setPassword(newPass);
keepGoing = false;
} else if (response.toUpperCase().equals("N")) {
System.out.println("\nPassword change cancelled");
keepGoing = false;
} else {
System.out.println("\nError: Please enter either 'y' or 'n'");
} // end if else
} // end while
} // end generatePass
public byte[] encrypt(String pass) {
// create byte array
byte[] passBytes = pass.getBytes();
int length = passBytes.length + this.salt.length;
byte[] newPass = new byte[length];
// fill byte array with salt and password
int count = 0;
for (int i = 0; i < this.salt.length; i++) {
newPass[i] = this.salt[i];
count++;
} // end for
for (int i = 0; i < passBytes.length; i++) {
newPass[count++] = passBytes[i];
} // end for
// encrypt the password
byte[] encBytes;
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, this.key);
encBytes = cipher.doFinal(newPass);
} catch (Exception e) {
System.out.println("Error: Something went wrong while encrypting the password");
System.out.println(e.getMessage());
// TODO write to error log
encBytes = null;
} // end try catch
return encBytes;
} // end encrypt
public String decrypt(byte[] encBytes) {
String decPass;
// decrypt the password
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, this.key);
byte[] decBytes = cipher.doFinal(encBytes);
String saltPass = new String(decBytes);
String salt = new String(this.salt);
decPass = "";
for (int i = salt.length(); i < saltPass.length(); i++) {
decPass += saltPass.charAt(i);
} // end for
} catch (Exception e) {
System.out.println("Error: Something went wrong while decrypting the password");
System.out.println(e.getMessage());
// TODO write to error log
decPass = null;
}
return decPass;
} // end decrypt
} // end class