-
Notifications
You must be signed in to change notification settings - Fork 0
/
Password.java
90 lines (74 loc) · 2.85 KB
/
Password.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
import java.util.Base64;
import java.security.SecureRandom;
public class Password{
public int key;
public String account;
public String username;
private String password;
private String salt; // random passcode
private byte[] pepper; // random IV
public String getPassword() throws Exception{
AESCrypto aes = new AESCrypto(this.salt, this.pepper);
String ret = aes.decrypt(this.password);
byte[] bSalt = new byte[24];
new SecureRandom().nextBytes(bSalt);
new SecureRandom().nextBytes(this.pepper);
this.salt = Base64.getEncoder().encodeToString(bSalt);
aes = new AESCrypto(this.salt, this.pepper);
this.password = aes.encrypt(ret);
return ret;
}
public void changePassword(String sPassword) throws Exception{
byte[] bSalt = new byte[24];
new SecureRandom().nextBytes(bSalt);
new SecureRandom().nextBytes(this.pepper);
this.salt = Base64.getEncoder().encodeToString(bSalt);
AESCrypto aes = new AESCrypto(this.salt, this.pepper);
this.password = aes.encrypt(sPassword);
}
public boolean setPassword(String sPassword) throws Exception{
byte[] bSalt = new byte[24];
this.pepper = new byte[16];
new SecureRandom().nextBytes(bSalt);
new SecureRandom().nextBytes(this.pepper);
this.salt = Base64.getEncoder().encodeToString(bSalt);
AESCrypto aes = new AESCrypto(this.salt, this.pepper);
this.password = aes.encrypt(sPassword);
return true;
}
public void setPassword(String sPassword, boolean bool){
this.password = sPassword;
}
public void setSalt(String sSalt){
this.salt = sSalt;
}
public void setPepper(byte[] sPepper){
this.pepper = sPepper;
}
//deprecated -- intended for development purposes
public void debug() throws Exception{
AESCrypto aes = new AESCrypto(this.salt, this.pepper);
String plainpwd = aes.decrypt(this.password);
String base64pepper = Base64.getEncoder().encodeToString(this.pepper);
System.out.println("Key: " + this.key);
System.out.println("Account: " + this.account);
System.out.println("Username: " + this.username);
System.out.println("Password: " + this.password);
System.out.println("Plaintext Password: " + plainpwd);
System.out.println("Salt: " + this.salt);
System.out.println("Pepper: " + base64pepper);
System.out.println("------------------------------------\n");
}
private String b64estr(String plaintext)throws Exception{
return Base64.getEncoder().encodeToString(plaintext.getBytes("UTF-8"));
}
public String get_xml() throws Exception{
String key = String.valueOf(this.key);
String act = b64estr(this.account);
String usr = b64estr(this.username);
String pwd = this.password;
String slt = this.salt;
String pep = Base64.getEncoder().encodeToString(this.pepper);
return "<password key=\"" + key + "\" account=\"" + act + "\" username=\"" + usr + "\" password=\"" + pwd +"\" salt=\"" + slt + "\" pepper=\"" + pep + "\"></password>";
}
}