# Updating Zencrypt Databases

### **Updating Zencrypt Databases:**

* Store keys and user information in a SQL or NoSQL database (MySQL, SQLite, MongoDB, PostgreSQL, etc.)
* Integrate key expiry and key rotation logs in a secure database table
* Implement user authentication and roles for key usage (which ties directly into data defense)
* Possibly demonstrate how to store encrypted data objects within the database and decrypt on retrieval

With this final release of Zencrypt v5 I am planning on integrating database functionality for storing keys, user credentials, logs, analytics, and even encrypted data. This diagram focuses specifically on the database enhancement portion of Zencrypt and shows the major steps for connecting, authenticating, and performing database operations securely.

```
                  ┌──────────────────────────────────┐
                  │         START - DB FLOW          │
                  └──────────────────────────────────┘
                                │
                                ▼
   ┌─────────────────────────────────────────────────────────┐
   │ 1) Load DB Config & Env. Vars                           │
   │    - Retrieve database credentials, connection strings  │
   │      from config file or environment variables          │
   └─────────────────────────────────────────────────────────┘
                                │
                                ▼
   ┌────────────────────────────────────────────────────────┐
   │ 2) Initialize Database Connection                      │
   │    - Connect to SQL/NoSQL DB (MySQL, PostgreSQL,       │
   │      MongoDB, etc.)                                    │
   │    - Handle exceptions if DB is unreachable            │
   └────────────────────────────────────────────────────────┘
                                │
                                ▼
   ┌─────────────────────────────────────────────────────────┐
   │ 3) Check/Perform User Authentication                    │
   │    - If user not logged in: prompt login/signup         │
   │      (salt + hash password, verify in DB)               │
   │    - Generate or validate JWT on successful login       │
   │    - Retrieve any user roles/permissions                │
   └─────────────────────────────────────────────────────────┘
                                │
                                │
                          ┌─────┴───────────────────┐
                          │ User Authenticated?     │
                          │ (JWT Valid?)            │
                          └─────────────────────────┘
                                │
                              │
                   ┌───────────┴────────────┐
                   │           NO           │
                   │  (Auth fails or token  │
                   │   invalid)             │
                   ▼                        │
        ┌─────────────────────────┐         │
        │  Return Error /         │         │
        │  Prompt Re-Login        │         │
        └─────────────────────────┘         │
                                            │ YES
                                            ▼
                       ┌────────────────────────────────────────┐
                       │ 4) Present DB-Related Actions          │
                       │    A) Store/Retrieve Keys in DB        │
                       │    B) Store/Retrieve Encrypted Data    │
                       │    C) Log Usage (encryption events)    │
                       │    D) Insert Access / Audit Records    │
                       │    E) Key Expiry & Rotation Checks     │
                       └────────────────────────────────────────┘
                                           │
                                           ▼
                ┌────────────────────────────────────────────────┐
                │ 5) Execute Chosen DB Operation                 │
                │    - "Insert new key," "Get user’s key,"  │
                │      "Write encryption log entry," etc.        │
                │    - Enforce user permissions/roles            │
                │    - Handle success/errors gracefully          │
                └────────────────────────────────────────────────┘
                                           │
                                           ▼
         ┌──────────────────────────────────────────────────────────┐
         │ 6) Return Response / Results to Zencrypt Main App        │
         │    - Encryption key retrieved? Next step is encrypt.     │
         │    - Log entry stored? Display success message.          │
         │    - Access denied? Inform user with error/log details.  │
         └──────────────────────────────────────────────────────────┘
                                           │
                                │
                                           ▼
             ┌─────────────────────────────────────────────────────┐
             │ 7) Continue Zencrypt Workflow or Exit               │
             │    - If more actions needed, loop back (Step 4)     │
             │    - Otherwise finalize/close DB connection if done │
             └─────────────────────────────────────────────────────┘
                                           │
                                │
                                           ▼
                          ┌───────────────────────────────────┐
                          │          END - DB FLOW            │
                          └───────────────────────────────────┘
```
