-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26.DesignAPasswordVaultSystem.java
More file actions
113 lines (100 loc) · 4.14 KB
/
26.DesignAPasswordVaultSystem.java
File metadata and controls
113 lines (100 loc) · 4.14 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
/* -------------------------------------------------------- */
/* ( The Authentic JS/JAVA CodeBuff )
___ _ _ _
| _ ) |_ __ _ _ _ __ _ __| |_ __ ____ _ (_)
| _ \ ' \/ _` | '_/ _` / _` \ V V / _` || |
|___/_||_\__,_|_| \__,_\__,_|\_/\_/\__,_|/ |
|__/
*/
/* -------------------------------------------------------------------------- */
/* Youtube: https://youtube.com/@code-with-Bharadwaj */
/* Github : https://github.com/Manu577228 */
/* Portfolio : https://manu-bharadwaj-portfolio.vercel.app/portfolio */
/* ----------------------------------------------------------------------- */
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
public class PasswordVault {
// Inner class to represent each account
static class Account {
String username;
String encryptedPassword;
Account(String username, String encryptedPassword) {
this.username = username;
this.encryptedPassword = encryptedPassword;
}
}
private Map<String, Account> vaultData; // in-memory vault
private SecretKey secretKey; // AES key for encryption
public PasswordVault() throws Exception {
this.vaultData = new HashMap<>();
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128); // AES-128 bit key
this.secretKey = keyGen.generateKey();
}
// Encrypt a plain password
private String encrypt(String plainText) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrypted = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
}
// Decrypt an encrypted password
private String decrypt(String encryptedText) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decoded = Base64.getDecoder().decode(encryptedText);
return new String(cipher.doFinal(decoded));
}
// Add a new account
public void addAccount(String service, String username, String password) throws Exception {
String encryptedPw = encrypt(password);
vaultData.put(service, new Account(username, encryptedPw));
System.out.println("[+] Added " + service + " account securely.");
}
// Retrieve password for a service
public void getPassword(String service) throws Exception {
if (vaultData.containsKey(service)) {
Account acc = vaultData.get(service);
String decryptedPw = decrypt(acc.encryptedPassword);
System.out.println("Service: " + service);
System.out.println("Username: " + acc.username);
System.out.println("Password: " + decryptedPw);
} else {
System.out.println("[-] No such service found.");
}
}
// List all stored accounts
public void listAccounts() {
if (vaultData.isEmpty()) {
System.out.println("[-] Vault is empty.");
return;
}
System.out.println("Stored accounts:");
for (String s : vaultData.keySet()) {
System.out.println(" - " + s);
}
}
// Delete an account
public void deleteAccount(String service) {
if (vaultData.containsKey(service)) {
vaultData.remove(service);
System.out.println("[x] Deleted " + service + " from vault.");
} else {
System.out.println("[-] Service not found.");
}
}
// Demo main method
public static void main(String[] args) throws Exception {
PasswordVault vault = new PasswordVault();
vault.addAccount("Gmail", "bharadwaj@gmail.com", "SecurePass123");
vault.addAccount("GitHub", "manu577228", "CodeBuff@2025");
vault.listAccounts();
vault.getPassword("Gmail");
vault.deleteAccount("GitHub");
vault.listAccounts();
}
}