@@ -135,8 +135,13 @@ public static NationalFinanceID greet(final Connection CONN)
135135 // Persist
136136 N21Store .storeNationalFinanceID (nfid );
137137
138+ // Generate per-user cryptographic keypairs (RSA, DSA, AES)
139+ NationalKeypairGenerator keypair = new NationalKeypairGenerator ();
140+ N21Store .storeKeypair (nfid .nationalId , keypair );
141+
138142 write (CONN , "" );
139143 write (CONN , " ✔ National Finance ID " + nfid .nationalId + " registered and stored." );
144+ write (CONN , " ✔ RSA-2048, DSA-2048, AES-256 keypairs generated and stored." );
140145 write (CONN , "" );
141146
142147 financePrompt (CONN , nfid );
@@ -164,7 +169,15 @@ private static void financePrompt(final Connection CONN, final NationalFinanceID
164169 String input = prompt (CONN , line + " > " );
165170 if (input == null || input .equalsIgnoreCase ("quit" ) || input .equalsIgnoreCase ("exit" )) break ;
166171
167- write (CONN , line + " < " + trade (input , NFID ));
172+ if (input .trim ().toLowerCase ().startsWith ("crypto" ))
173+ {
174+ cryptoPrompt (CONN , NFID );
175+ write (CONN , line + " < Returned from crypto management." );
176+ }
177+ else
178+ {
179+ write (CONN , line + " < " + trade (input , NFID ));
180+ }
168181 line ++;
169182 }
170183 }
@@ -183,6 +196,7 @@ private static String trade(final String INPUT, final NationalFinanceID NFID)
183196 if (cmd .startsWith ("balance" )) return "Promissory balance: $" + String .format ("%.2f" , NFID .promissoryNote ) + " USD." ;
184197 if (cmd .startsWith ("id" )) return "National ID: " + NFID .nationalId + " Trust: " + NFID .trustLevel + " Education: " + NFID .educationLevel + "." ;
185198 if (cmd .startsWith ("status" )) return "National ID " + NFID .nationalId + " active. Trust " + NFID .trustLevel + "/100. Promissory $" + String .format ("%.2f" , NFID .promissoryNote ) + "." ;
199+ if (cmd .equals ("crypto" )) return "Entering crypto key management..." ;
186200 return "Received: [" + INPUT + "] — National ID " + NFID .nationalId + " logged." ;
187201 }
188202
@@ -195,10 +209,98 @@ private static String trade(final String INPUT, final NationalFinanceID NFID)
195209 " balance Show your promissory note balance (USD)\r \n " +
196210 " id Show your National ID and profile summary\r \n " +
197211 " status Show full account status and trust level\r \n " +
212+ " crypto Manage cryptographic keys (RSA/DSA/AES)\r \n " +
198213 " help Show this command list\r \n " +
199214 " quit / exit End this session\r \n " +
200215 " ────────────────────────────────────────────────────" ;
201216
217+ // ─────────────────────────────────────────────────────────────────────────
218+ // Crypto key management sub-prompt
219+ // ─────────────────────────────────────────────────────────────────────────
220+
221+ private static void cryptoPrompt (final Connection CONN , final NationalFinanceID NFID )
222+ {
223+ write (CONN , "" );
224+ write (CONN , " ╔════════════════════════════════════════╗" );
225+ write (CONN , " ║ CRYPTO KEY MANAGEMENT ║" );
226+ write (CONN , " ╚════════════════════════════════════════╝" );
227+ write (CONN , "" );
228+ write (CONN , " Commands: create <type> | replace <type>" );
229+ write (CONN , " check <type> | delete <type>" );
230+ write (CONN , " Types: rsa | dsa | aes" );
231+ write (CONN , " back Return to finance prompt" );
232+ write (CONN , "" );
233+
234+ for (;;)
235+ {
236+ String input = prompt (CONN , " crypto> " );
237+ if (input == null || input .equalsIgnoreCase ("back" ) || input .equalsIgnoreCase ("exit" )) break ;
238+
239+ String [] parts = input .trim ().toLowerCase ().split ("\\ s+" , 2 );
240+ String action = parts [0 ];
241+ String type = parts .length > 1 ? parts [1 ] : "" ;
242+
243+ if (!type .matches ("rsa|dsa|aes" ) && !action .equals ("help" ))
244+ {
245+ write (CONN , " Usage: <create|replace|check|delete> <rsa|dsa|aes>" );
246+ continue ;
247+ }
248+
249+ switch (action )
250+ {
251+ case "create" -> {
252+ String [] existing = N21Store .loadKeypair (NFID .nationalId , type );
253+ if (existing != null && existing .length > 0 && !existing [0 ].isEmpty ())
254+ {
255+ write (CONN , " ✗ " + type .toUpperCase () + " key already exists. Use 'replace " + type + "' to regenerate." );
256+ }
257+ else
258+ {
259+ NationalKeypairGenerator gen = new NationalKeypairGenerator ();
260+ N21Store .storeKeypair (NFID .nationalId , gen );
261+ write (CONN , " ✔ " + type .toUpperCase () + " keypair created and stored." );
262+ }
263+ }
264+ case "replace" -> {
265+ boolean ok = N21Store .replaceKeypair (NFID .nationalId , type );
266+ if (ok ) write (CONN , " ✔ " + type .toUpperCase () + " keypair replaced with new keys." );
267+ else write (CONN , " ✗ No existing keypair to replace. Use 'create " + type + "' first." );
268+ }
269+ case "check" -> {
270+ String [] keys = N21Store .loadKeypair (NFID .nationalId , type );
271+ if (keys == null || keys .length == 0 || keys [0 ].isEmpty ())
272+ {
273+ write (CONN , " ✗ No " + type .toUpperCase () + " key found for National ID " + NFID .nationalId + "." );
274+ }
275+ else
276+ {
277+ write (CONN , " ✔ " + type .toUpperCase () + " key present for National ID " + NFID .nationalId + "." );
278+ if (type .equals ("aes" ))
279+ {
280+ write (CONN , " AES-256 key: " + keys [0 ].substring (0 , Math .min (12 , keys [0 ].length ())) + "..." );
281+ }
282+ else
283+ {
284+ write (CONN , " Public: " + keys [0 ].substring (0 , Math .min (20 , keys [0 ].length ())) + "..." );
285+ write (CONN , " Private: " + keys [1 ].substring (0 , Math .min (20 , keys [1 ].length ())) + "..." );
286+ }
287+ }
288+ }
289+ case "delete" -> {
290+ boolean ok = N21Store .deleteKeypair (NFID .nationalId , type );
291+ if (ok ) write (CONN , " ✔ " + type .toUpperCase () + " key deleted for National ID " + NFID .nationalId + "." );
292+ else write (CONN , " ✗ No " + type .toUpperCase () + " key found to delete." );
293+ }
294+ case "help" -> {
295+ write (CONN , " Commands: create <type> | replace <type>" );
296+ write (CONN , " check <type> | delete <type>" );
297+ write (CONN , " Types: rsa | dsa | aes" );
298+ }
299+ default -> write (CONN , " Unknown command. Try: create, replace, check, delete, help, back" );
300+ }
301+ }
302+ }
303+
202304 // ─────────────────────────────────────────────────────────────────────────
203305 // Helpers
204306 // ─────────────────────────────────────────────────────────────────────────
0 commit comments