Как зашифровать метаданные в Java с помощью GroupDocs.Signature

Электронные подписи отличны, но скрытые свойства документа — имена авторов, метки времени, внутренние идентификаторы — могут всё ещё утекать в открытом виде. Если вам нужно узнать, как зашифровать метаданные, это руководство покажет именно это, используя гибкий API GroupDocs.Signature. К концу урока вы сможете:

  • Сериализовать пользовательские структуры метаданных в Java‑документах.
  • Применить шифрование (в примере используется XOR для наглядности, но вы увидите, как заменить его на AES).
  • Подписать документ, внедрив зашифрованные метаданные.
  • Масштабировать решение для обеспечения уровня безопасности и производительности, соответствующего производственным требованиям.

Начнём.

Быстрые ответы

  • What does “encrypt metadata” mean? It protects hidden document properties with cryptographic transformation before signing.
  • Which library do I need? GroupDocs.Signature for Java 23.12 or newer.
  • Is a license required? A free trial works for development; a full license is mandatory for production.
  • Can I replace XOR with a stronger algorithm? Yes—implement AES‑GCM or another vetted scheme.
  • Is the approach format‑agnostic? GroupDocs.Signature supports 30+ file formats, including DOCX, PDF, XLSX, PPTX, and more.

Что такое шифрование метаданных документа в Java?

Шифрование метаданных документа в Java означает взятие скрытых свойств, которые сопровождают файл, и применение к ним криптографического преобразования, чтобы только уполномоченные стороны могли их прочитать. Это защищает внутренние идентификаторы, заметки рецензентов и другие конфиденциальные данные от случайного просмотра.

Зачем шифровать метаданные документа?

Шифрование метаданных защищает чувствительную информацию, которую можно использовать для идентификации людей или раскрытия внутренних процессов. Преобразуя эти скрытые свойства в шифртекст, вы соблюдаете такие нормативы, как GDPR и HIPAA, поддерживаете целостность аудиторских следов и предотвращаете извлечение конкурентами бизнес‑критичных данных. Этот уровень безопасности дополняет видимую цифровую подпись, обеспечивая конфиденциальность всего документа.

Предварительные требования

Необходимые библиотеки и зависимости

  • GroupDocs.Signature for Java (version 23.12 or later) – core signing library.
  • Java Development Kit (JDK) – JDK 8 or higher.
  • Maven or Gradle for dependency management.

Настройка окружения

Рекомендуется использовать Java IDE (IntelliJ IDEA, Eclipse или VS Code) с Maven/Gradle проектом.

Требования к знаниям

  • Базовый Java (классы, методы, объекты).
  • Понимание концепций метаданных документа.
  • Знакомство с основами симметричного шифрования.

Настройка GroupDocs.Signature для Java

Choose your build tool and add the dependency.

Maven:

<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-signature</artifactId>
    <version>23.12</version>
</dependency>

Gradle:

implementation 'com.groupdocs:groupdocs-signature:23.12'

Alternatively, you can grab the JAR file directly from Выпуски GroupDocs.Signature для Java and add it to your project manually (though Maven/Gradle is preferred).

Шаги получения лицензии

  • Free Trial – full features for a limited period.
  • Temporary License – extended evaluation.
  • Full Purchase – production use.

Базовая инициализация и настройка

The Signature class is GroupDocs.Signature’s core object that loads a document, applies signatures, and writes the result back to disk.

Signature signature = new Signature("YOUR_DOCUMENT_PATH");

Replace "YOUR_DOCUMENT_PATH" with the actual path to your DOCX, PDF, or other supported file.

Pro tip: Wrap the Signature object in a try‑with‑resources block or call close() explicitly to avoid memory leaks.

Руководство по реализации

Как создать пользовательские структуры метаданных в Java

A custom metadata class defines the structure of the information you wish to protect and how it will be serialized by GroupDocs.Signature. By annotating fields with @FormatAttribute, you instruct the library on the order and format of each element, enabling consistent encryption and later de‑serialization. This class becomes the blueprint for the encrypted payload embedded in the signed document.

class DocumentSignatureData {
    @FormatAttribute(propertyName = "SignID")
    private String ID;
    
    public String getID() { return ID; }
    public void setID(String value) { ID = value; }

    @FormatAttribute(propertyName = "SAuth")
    private final String Author;

    public final String getAuthor() { return Author; }
    public DocumentSignatureData(String author) { this.Author = author; }

    @FormatAttribute(propertyName = "SDate", propertyFormat = "yyyy-MM-dd")
    private Date Signed = new Date();

    public final Date getSigned() { return Signed; }
    public void setSigned(Date value) { Signed = value; }

    @FormatAttribute(propertyName = "SDFact", propertyFormat = "N2")
    private BigDecimal DataFactor = new BigDecimal(0.01);

    public final BigDecimal getDataFactor() { return DataFactor; }
    public void setDataFactor(BigDecimal value) { DataFactor = value; }
}
  • @FormatAttribute tells GroupDocs.Signature how to serialize each field.
  • Extend this class with any additional properties your business requires.

Реализация пользовательского шифрования для метаданных документа

Implementing a custom encryption routine allows you to control how metadata bytes are transformed before being stored. By creating a class that implements the IDataEncryption interface, you can plug in any algorithm—XOR for demonstration, AES‑GCM for production, or even a proprietary scheme. The signing process will automatically invoke your encryptor during metadata serialization.

class CustomXOREncryption implements IDataEncryption {
    @Override
    public byte[] encrypt(byte[] data) {
        byte key = 0x5A; 
        byte[] encryptedData = new byte[data.length];
        for (int i = 0; i < data.length; i++) {
            encryptedData[i] = (byte) (data[i] ^ key);
        }
        return encryptedData;
    }

    @Override
    public byte[] decrypt(byte[] data) {
        // XOR decryption uses the same logic as encryption
        return encrypt(data);  
    }
}

Important: XOR is not suitable for production security. Replace it with AES‑GCM or another vetted algorithm before deploying.

Как подписать документы с зашифрованными метаданными

Signing a document while embedding encrypted metadata ties the hidden information to the digital signature, ensuring both authenticity and confidentiality. Using MetadataSignOptions, you specify which metadata fields to include and provide the encryption implementation. The Signature object then processes the document, applies the signature, and writes the encrypted payload alongside the visible signature elements.

MetadataSignOptions is the configuration object that tells GroupDocs.Signature which metadata to embed and how to encrypt it.

DocumentSignatureData holds the actual values that will be serialized and encrypted.

WordProcessingMetadataSignature represents a single piece of metadata (e.g., author, custom ID) that will be attached to a Word‑processing document.

class SignWithMetadataCustomSerialization {
    public static void run() throws Exception {
        String filePath = "YOUR_DOCUMENT_DIRECTORY/SampleDocument.docx";
        String outputFilePath = new File("YOUR_OUTPUT_DIRECTORY", "SignedDocument.docx").getPath();

        try {
            Signature signature = new Signature(filePath);
            
            // Custom encryption instance
            IDataEncryption encryption = new CustomXOREncryption();
            
            MetadataSignOptions options = new MetadataSignOptions();
            options.setDataEncryption(encryption);

            DocumentSignatureData documentSignature = new DocumentSignatureData(System.getenv("USERNAME"));
            documentSignature.setID(java.util.UUID.randomUUID().toString());
            documentSignature.setSigned(new Date());
            documentSignature.setDataFactor(new BigDecimal("11.22"));

            WordProcessingMetadataSignature mdSignature = new WordProcessingMetadataSignature(
                "Signature", documentSignature);
            WordProcessingMetadataSignature mdAuthor = new WordProcessingMetadataSignature(
                "Author", "Mr.Scherlock Holmes");
            WordProcessingMetadataSignature mdDocId = new WordProcessingMetadataSignature(
                "DocumentId", java.util.UUID.randomUUID().toString());

            options.getSignatures().add(mdSignature);
            options.getSignatures().add(mdAuthor);
            options.getSignatures().add(mdDocId);

            signature.sign(outputFilePath, options);
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }
}

Пошаговый разбор

  1. Initialize Signature with the source file.
  2. Create an IDataEncryption implementation (CustomXOREncryption).
  3. Configure MetadataSignOptions and attach the encryption instance.
  4. Populate DocumentSignatureData with your custom fields.
  5. Create individual WordProcessingMetadataSignature objects for each piece of metadata.
  6. Add them to the options collection and call sign().

Pro tip: Using System.getenv("USERNAME") automatically captures the current OS user, which is handy for audit trails.

Когда использовать этот подход

Choosing to encrypt metadata is ideal when documents contain confidential identifiers, internal comments, or regulatory data that must not be exposed to unauthorized readers. Scenarios include legal contracts with hidden clause numbers, financial statements with proprietary calculations, healthcare records with patient IDs, and multi‑party agreements where each participant should only see their own metadata. In fully public documents, this step may be unnecessary.

СценарийЗачем шифровать метаданные?
Юридические контрактыСкрыть внутренние идентификаторы рабочего процесса и заметки рецензентов.
Финансовые отчётыЗащитить источники расчётов и конфиденциальные цифры.
Медицинские записиОбеспечить безопасность идентификаторов пациентов и заметок обработки (HIPAA).
Многосторонние соглашенияГарантировать, что только уполномоченные стороны могут просматривать встроенные метаданные.

Avoid this technique for fully public documents where transparency is required.

Соображения безопасности: за пределами XOR‑шифрования

Почему XOR недостаточен

XOR encryption merely obscures data and lacks the cryptographic strength required for protecting sensitive metadata. The static key can be discovered through frequency analysis, and there is no built‑in integrity verification, leaving the payload vulnerable to tampering. For compliance and security, replace XOR with an authenticated encryption mode such as AES‑GCM, which provides both confidentiality and tamper detection.

Альтернативы для производственной среды

AES‑GCM Example (conceptual):

// Example pattern (not complete implementation)
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encrypted = cipher.doFinal(data);
  • Provides confidentiality and authentication.
  • Recognized by NIST and widely adopted in enterprise security.

Key Management: Store keys in a secure vault (AWS KMS, Azure Key Vault) and never hard‑code them.

Action item: Replace CustomXOREncryption with an AES‑based class that implements IDataEncryption. The rest of your signing code stays unchanged.

Распространённые проблемы и решения

Метаданные не шифруются

  • Verify options.setDataEncryption(encryption) is invoked.
  • Confirm your encryption class correctly implements IDataEncryption.

Документ не удаётся подписать

  • Check file existence and write permissions.
  • Ensure the license is active (trial may expire).

Дешифрование не удаётся после подписи

  • Use the identical encryption key for both encrypt and decrypt operations.
  • Confirm you’re reading the correct metadata fields.

Узкие места производительности при работе с большими файлами

  • Process documents in batches (10–20 at a time).
  • Dispose of Signature objects promptly.
  • Profile your encryption algorithm; AES adds modest overhead compared to XOR.

Руководство по устранению неполадок

Signature initialization fails:

try {
    Signature signature = new Signature(filePath);
} catch (Exception e) {
    System.err.println("Failed to load document: " + e.getMessage());
    // Verify: file exists, correct format, sufficient permissions
}

Encryption exceptions:

if (data == null || data.length == 0) {
    throw new IllegalArgumentException("Cannot encrypt empty data");
}

Missing metadata after signing:

System.out.println("Signatures added: " + options.getSignatures().size());
// Should be > 0

Соображения по производительности

  • Memory: Dispose of Signature objects; for bulk jobs, use a fixed‑size thread pool.
  • Speed: Cache the encryption instance to reduce object‑creation overhead.
  • Benchmarks (approx.):
    • 5 MB DOCX with XOR: 200‑500 ms
    • Same file with AES‑GCM: ~250‑600 ms

Лучшие практики для продакшна

  1. Swap XOR for AES (or another vetted algorithm).
  2. Use a secure key store – never embed keys in source code.
  3. Log signing operations (who, when, which file).
  4. Validate inputs (file type, size, metadata format).
  5. Implement comprehensive error handling with clear messages.
  6. Test decryption in a staging environment before release.
  7. Maintain an audit trail for compliance purposes.

Заключение

You now have a complete, step‑by‑step recipe to how to encrypt metadata using GroupDocs.Signature:

  • Define a typed metadata class with @FormatAttribute.
  • Implement IDataEncryption (XOR shown for illustration).
  • Sign the document while attaching encrypted metadata.
  • Upgrade to AES for production‑grade security.

Next steps: experiment with different encryption algorithms, integrate a secure key‑management service, and extend the metadata model to cover your specific business needs.

Часто задаваемые вопросы

Q: Can I use a different encryption algorithm than XOR?
A: Absolutely. Implement any class that fulfills the IDataEncryption interface—AES‑GCM is a recommended choice for strong confidentiality and integrity.

Q: Do I need to modify the signing code when I switch to AES?
A: No. Once your custom AES implementation conforms to IDataEncryption, simply replace the CustomXOREncryption instance with your new class.

Q: Is encrypted metadata visible in the signed file if I open it with a regular viewer?
A: The metadata remains part of the file but appears as unintelligible binary data. Only your decryption routine can interpret it.

Q: How does this affect file size?
A: Encryption adds minimal overhead (typically a few bytes per metadata field). The impact on overall document size is negligible.

Q: What licensing do I need for production use?
A: A full GroupDocs.Signature license is required for commercial deployment. A trial license suffices for development and testing.


Last Updated: 2026-07-06
Tested With: GroupDocs.Signature 23.12 (Java)
Author: GroupDocs

Связанные руководства