# Received Key Processor Application

## Overview

**User and System Administration Documentation**

This document provides user guidance and system administration information. For technical implementation details, tag structures, and cryptographic algorithms, see [DEVELOPER.md](DEVELOPER.md).

---

The Received Key Processor Application is a C# WPF desktop application (.NET 8.0) designed to receive, process, and manage cryptographic keys and mission data through serial port communication and SQLite database storage. The application has been restructured to follow standard layered architecture principles for better maintainability and testability.

## 🆕 New Features

### Send Keys Page - Prepend FDU Option

The Send Keys page now includes a **Prepend FDU (0x01B0)** checkbox option that automatically adds the `-fdu 01B0` option when executing, regardless of the Mode selection. This is useful for prepending FDU headers to raw data files without manually setting the Mode.

- **Default:** Enabled (ON)
- **Behavior:**
  - Mode=None + Prepend FDU=ON → `-fdu 01B0` sent
  - Mode=None + Prepend FDU=OFF → No FDU argument
  - Mode=Fdu01B0/Fdu02B0 → Uses selected FDU value
- **Settings:** Auto-saved and persist across application restarts

**Documentation:** See [Send_Keys_Page_Documentation.md](Send_Keys_Page_Documentation.md) for complete guidance.

### EKMS2.exe Integration

The application now includes **complete EKMS2.exe integration** for processing 8727 binary files:

- **Supported Formats**: .8727, .8287, .bin, .key, .xml, .json
- **Processing**: Binary → EKMS2.exe → XML → Database
- **Security**: Secure temporary file handling with zeroization
- **Performance**: Async processing with proper error handling

### DS-100-1 TrKEK Support

The application now includes **complete DS-100-1 TrKEK (Transfer Key Encryption Key) support**:

- Automatic DS-100-1 tag generation and validation
- Multiple key generation modes (Random, KnownValue, CustomRepeat, FileBased)
- Short title format validation with warnings
- File-based key generation with per-segment or global assignment options
- Automatic key length calculation from files

**Documentation**: See [DEVELOPER.md](DEVELOPER.md) for technical implementation details.

### MIDS JTRS Tag Support

The application now includes **complete MIDS JTRS (Joint Tactical Radio System) tag support**:

- Automatic tag validation and decoding
- Complete UI for viewing and editing all 23 MIDS JTRS tag fields
- CRC field protection with auto-calculation
- ECU KEK ID calculation and validation
- Red Key support (No ECU KEK)
- Tag validity checking for multiple radio types (KOR-8/9/9X, KOR-25/26A)

**Documentation**: See [DEVELOPER.md](DEVELOPER.md) for technical implementation details.

### Recent Fixes (v1.0.106)

- **Database Seeding Fixes**: Fixed NOT NULL constraint errors for Segment table by using empty strings instead of null for SegmentEffectiveDate, SegmentExpirationDate, and SegmentData fields
- **Improved Error Messages**: "Add Test Data" now shows actual underlying error instead of generic "failed to seed database" message
- **Secure Delete Enhancements**: Improved error handling with better logging and diagnostic messages
- **Hierarchical View Test**: Added UI functional test for Hierarchical View database page

## Architecture

This application follows a 5-layer architecture pattern:

### 1. Presentation Layer (UI)
- **Location**: `1. Presentation/` & root `ReceivedKeyProcessorApp/`
- **Purpose**: WPF Views, ViewModels, and UI logic
- **Key components**: Views/, ViewModels/, Contracts/, Resources/, Activation/

### 2. Application Layer (Business Logic)
- **Location**: `2. Application/`
- **Purpose**: Business services, application orchestration, DTOs
- **Key components**: Services/, Contracts/, DTOs/

### 3. Domain Layer (Business Models)
- **Location**: `3. Domain/`
- **Purpose**: Domain models, interfaces, business entities
- **Key components**: Models/, Contracts/, Enums/

### 4. Infrastructure Layer (Data Access)
- **Location**: `4. Infrastructure/`
- **Purpose**: Database context, repositories, external services
- **Key components**: Data/, Services/, Repositories/

### 5. CrossCutting Layer (Shared Utilities)
- **Location**: `5. CrossCutting/`
- **Purpose**: Common utilities, helpers, security
- **Key components**: Common/, Security/, Extensions/

## Prerequisites

### Development Environment
- **Visual Studio 2022** (recommended)
- **.NET 8.0 SDK** (for new layered projects)
- **.NET Framework 4.8** (for main WPF application)

### Runtime Requirements
- **Windows 10/11** (WPF dependency)
- **.NET 8.0 Windows Desktop Runtime**
- **SQLite** (included as NuGet package)

### External Dependencies
- Serial port access (for COM port communication)
- Write permissions for database file creation

## Installation

### Step 1: Clone or Copy Repository
1. Copy the entire `receivekeyprocessorapp` folder to your desired location
2. Ensure the folder structure remains intact

### Step 2: Open in Visual Studio
1. Launch Visual Studio 2022
2. Open the solution file: `receivekeyprocessorapp\ReceivedKeyProcessorApp.sln`
3. Wait for Visual Studio to restore NuGet packages

### Step 3: Build Solution
1. In Solution Explorer, right-click the solution
2. Select "Build Solution" or press `Ctrl+Shift+B`
3. Resolve any build errors (see troubleshooting section)

### Step 4: Configure (Optional)
- Modify `appsettings.json` if needed for custom configurations
- Ensure serial port permissions are set correctly

## Running the Application

### Method 1: From Visual Studio
1. Set `ReceivedKeyProcessorApp` as the startup project
2. Press `F5` or click "Start Debugging"
3. The application should launch automatically

### Method 2: Compiled Executable
1. Navigate to the bin output folder:
   ```
   receivekeyprocessorapp\ReceivedKeyProcessorApp\bin\Debug\net8.0-windows10.0.19041.0\
   ```
2. Run `ReceivedKeyProcessorApp.exe`

## Testing

### Unit Testing
1. Run all tests in Test Explorer (Test → Test Explorer)
2. Alternatively, use command line:
   ```
   dotnet test
   ```
3. Focus on testing:
   - Domain model validation
   - Service layer business logic
   - Data access operations

### Integration Testing
1. Test serial port communication with actual hardware
2. Verify database operations:
   - Key insertion/retrieval
   - Mission plan management
   - Data integrity

### Manual Testing Checklist
- [ ] Application launches without errors
- [ ] Database file creates automatically
- [ ] Serial port connection works (with hardware)
- [ ] Key processing functions correctly
- [ ] Database operations persist data
- [ ] UI navigation responsive
- [ ] No memory leaks during extended use

## User Interface

The application provides the following main screens:

### Main Window
- Task selection interface
- Real-time key processing status
- System configuration options

### Database Window
- View and manage stored keys
- Search and filter functionality
- Data export capabilities

### Mission Plan Window
- Mission configuration
- Key assignment to missions
- Mission plan validation

## Key Features

- **Serial Port Communication**: Receive key data via COM port
- **XML Processing**: Parse and validate Tier3 XML payloads
- **Database Storage**: SQLite database for persistent storage
- **Mission Planning**: Create and manage mission plans
- **Key Management**: Organize and process cryptographic keys
- **Duplicate Prevention**: Hash-based duplicate detection
- **Data Export**: Export processed data in various formats

## Troubleshooting

### Common Issues

#### Build Errors
1. **Missing NuGet packages**: Right-click solution → "Restore NuGet Packages"
2. **Framework mismatch**: Ensure .NET 8.0 and .NET Framework 4.8 are installed
3. **Path issues**: Verify solution file paths are correct

#### Runtime Errors
1. **Database access denied**: Run as administrator or modify folder permissions
2. **Serial port issues**: Ensure correct COM port and permissions
3. **Missing DLLs**: Verify all dependencies are in output folder

### Performance Issues
1. **Database slowness**: Consider vacuuming SQLite database
2. **Memory usage**: Monitor for large XML files causing memory issues
3. **UI freezing**: Use async operations for long-running tasks

## Development Guidelines

### Code Standards
- Follow C# naming conventions
- Use dependency injection throughout
- Implement interfaces for all services
- Include XML documentation for public APIs

### Adding New Features
1. Define domain models in the Domain layer
2. Create service interfaces in Application layer
3. Implement services in Application/Infrastructure layers
4. Add UI components in Presentation layer
5. Write appropriate unit tests

## Support

For issues or questions:
1. Check this README first
2. Review error logs in the application
3. Consult Visual Studio output window for build issues
4. Ensure all prerequisites are met

## Version History

- **v1.0.106**: Database seeding fixes, improved error messages, secure delete enhancements, hierarchical view test
- **v1.0.105**: Initial release with layered architecture
- **v1.0.104**: Basic key processing functionality
- **v1.0.47**: Core domain models established

## License

(Add appropriate license information here)

---

**Note**: This application processes sensitive cryptographic data. Ensure appropriate security measures are in place when running in production environments.
