Encryption & Decryption¶
The Encryption & Decryption feature allows users to encrypt and decrypt text using generated encryption keys.
Supported Algorithms¶
- AES: Advanced Encryption Standard (128, 192, 256-bit)
- ChaCha20: ChaCha20 stream cipher (256-bit)
Features¶
Encryption¶
- Select a saved encryption key
- Enter plain text to encrypt
- Click “Encrypt” button
- Encrypted text is displayed in Base64 format
- IV (Initialization Vector) is generated automatically and displayed
Decryption¶
- Select the same encryption key used for encryption
- Enter encrypted text (Base64 format)
- Enter IV (if custom IV was used)
- Click “Decrypt” button
- Decrypted plain text is displayed
IV Management¶
- Automatic IV: Generated automatically during encryption
- Custom IV: Can be provided for decryption
- IV Display: IV is shown in Base64 format for easy copying
Usage¶
Encrypting Text¶
// In ViewModel
fun encryptText() {
viewModelScope.launch {
encryptCommand.handle(
AesEncryptTextCommand(
text = uiState.inputText,
keyId = uiState.selectedKeyId,
algorithm = uiState.selectedAlgorithm
)
).fold(
onSuccess = { encryptedView ->
// Update UI with encrypted text and IV
},
onFailure = { error ->
// Handle error
}
)
}
}
Decrypting Text¶
fun decryptText() {
viewModelScope.launch {
decryptCommand.handle(
AesDecryptTextCommand(
encryptedText = uiState.encryptedTextInput,
iv = uiState.ivInput,
keyId = uiState.selectedKeyId,
algorithm = uiState.selectedAlgorithm
)
).fold(
onSuccess = { decryptedView ->
// Update UI with decrypted text
},
onFailure = { error ->
// Handle error
}
)
}
}
Encryption Process¶
- Key Selection: User selects a saved encryption key
- Text Input: User enters plain text
- IV Generation: Secure random IV is generated
- Encryption: Text is encrypted using selected algorithm
- Output: Encrypted text and IV are displayed
Decryption Process¶
- Key Selection: User selects the same key used for encryption
- Encrypted Text Input: User enters encrypted text (Base64)
- IV Input: User enters IV (if custom IV was used)
- Decryption: Text is decrypted using selected algorithm
- Output: Decrypted plain text is displayed
Security Features¶
- Secure Random IV: Each encryption uses a unique IV
- Key Validation: Keys are validated before use
- Error Handling: Clear error messages for invalid inputs
UI Components¶
- KeySelectionSection: Select encryption key
- EncryptionSection: Encrypt text input and results
- DecryptionSection: Decrypt text input and results
- EncryptedResultCard: Display encrypted text and IV
- DecryptedResultCard: Display decrypted text
Error Handling¶
Common errors:
- Key Not Found: Selected key doesn’t exist
- Invalid Key: Key format is incorrect
- Decryption Failed: Wrong key or corrupted data
- Invalid Base64: Encrypted text or IV format is invalid
Learn More¶
- Key Generation - Generate encryption keys
- Architecture - Domain Layer - Encryption services
- Architecture - Application Layer - Command handlers