This Java application enables secure processing of PDF documents by providing functionalities for signing, encrypting, and decrypting PDFs. It incorporates AES-256 encryption for confidentiality and uses a self-signed certificate for document authenticity. The application also stores document metadata in a MySQL database, logging actions and errors in a structured manner.
This project is organized into the following layers:
- Controller: Manages input/output and handles user interactions.
- Service: Contains core logic for encryption, decryption, and digital signing.
- Database: Manages database connections and handles data storage.
Package: com.example.controller
This class handles user interactions for processing and decrypting PDF files. It prompts the user for inputs and passes file paths and keys to the respective services.
- handlePdfProcessing(): Prompts the user for a PDF file path, processes the PDF by signing and encrypting it, and logs success or error messages.
- handleDecrypting(): Prompts for an AES key, encrypted file path, and output path, then decrypts the PDF using the provided key.
Package: com.example.database
This class manages the MySQL database connection, ensuring the database and necessary tables are created if they don’t exist. It supports metadata storage for processed PDF documents.
- initializeDatabase(): Sets up the
pdf_storedatabase andpdf_metadataanduserstables, if not already created. - connectToDatabase(): Establishes a connection to the
pdf_storedatabase. - storeMetadata(PdfMetadata metadata): Inserts metadata about a processed PDF file into the
pdf_metadatatable. - close(): Closes the database connection, ensuring resources are properly released.
Package: com.example.model
Holds metadata information about processed PDF documents, such as document name and file path.
name: The document name.path: The file path where the document is stored.
- getName(), getPath(): Accessors for retrieving document name and path.
- setName(String name), setPath(String path): Setters for updating document name and path.
- toString(): Returns a string representation of the metadata for logging and debugging.
Package: com.example.service
Handles decryption of encrypted PDF files using AES keys.
- DecryptionService(String aesKey): Initializes the service with a Base64-encoded AES key.
- decrypt(byte[] encryptedContent): Decrypts byte data using AES.
- decryptPdfFile(File encryptedFile, File outputFile): Reads an encrypted PDF file, decrypts it, and writes the result to the output file.
Package: com.example.service
Handles AES-256 encryption for PDF documents. The AES key is generated at runtime and securely stored in memory.
- EncryptionService(): Generates a new AES-256 key.
- encrypt(byte[] content): Encrypts byte content using the AES key.
- saveEncryptedPdf(byte[] encryptedContent): Saves encrypted PDF content to a temporary file.
Package: com.example.service
This class coordinates the signing, encryption, and metadata storage processes for PDF documents.
- processPdf(String pdfPath): Signs, encrypts, and saves metadata for a given PDF file. Logs successful processing and stores file metadata in the database.
- decrypt(String encryptedFilePath, String outputFilePath): Decrypts a previously encrypted PDF file and writes it to the specified output location.
Package: com.example.service
Generates a self-signed certificate and attaches a digital signature to a PDF document.
- generateKeyPair(): Generates an RSA key pair for signing.
- generateSelfSignedCertificate(KeyPair keyPair): Creates a self-signed X.509 certificate using the generated key pair.
- signPdf(String inputPdfPath): Signs a PDF document and returns the signed document as a byte array.
Package: com.example.utils
A utility class for logging information, warnings, and errors to a log file (application.log). This is essential for tracking application activities and debugging errors.
- info(String message): Logs informational messages.
- warn(String message): Logs warning messages.
- error(String message, Exception e): Logs error messages with exception details.
- close(): Closes the log file handler when logging is no longer needed.
Package: com.example
The Main class initializes the application and provides a user interface in the console for processing and decrypting PDFs.
- main(String[] args): The main entry point for the application. Displays options for PDF processing or decryption, then delegates tasks to the
PdfControllerclass based on user selection.
- PDFBox: Library for PDF manipulation.
- BouncyCastle: Provides cryptographic functions for generating digital certificates.
- The database configuration uses a local MySQL database. Ensure that
DB_USERandDB_PASSWORDare correctly set in theDatabaseConnectionclass.
- All logs are saved to
application.log, capturing key actions, errors, and database connection statuses.
This documentation provides a structured overview of the project, covering all core functionalities, classes, and methods. Use this document as a reference while developing, testing, and debugging the application.