Search for embedded object with custom serialization of Metadata signatures

GroupDocs.Signature provides additional features when searching for Metadata Signatures that were previously encrypted and have embedded data objects. 

  • ability to search for embedded custom objects into metadata and decrypt them to original source values
  • ability to search for encrypted text of metadata signature and decrypt it   

Here are the steps to search and decrypt previously encrypted text of metadata and decrypt custom object from metadata signature (MetadataSignature) with GroupDocs.Signature API:

  • Implement custom data serialization class that implement IDataEncryption interface. By default Signature uses embedded json format serialization but allows user to customize it. if object of class was serialized by custom serialization when searching for it, this class should also has same serialization attribute.
  • Implement custom data encryption class that implements IDataEncryption interface. By default Signature has several encryption implementation you can use but allows user to customize it. There’s ability to specify inline encryption to use
  • Define class with proper DataSerialization attribute
  • Instantiate the MetadataSearchOptions object value 
  • Call search method of Signature class instance and pass MetadataSearchOptions to it.
  • Process each Metadata signature and set property setDataEncryption to specify data encryption and call getData method to retrieve object 

Implementation of custom data serialization

 This example shows how to specify custom serialization class. This class should be implemented as Attribute and IDataSerializer interface.

public class CustomSerializationAttribute implements IDataSerializer{
 
 
    public <T> T deserialize(String source, Class<T> type)
    {
        return new Gson().fromJson(source, type);
    }
    public String serialize(Object data)
    {
        return new Gson().toJson(data);
    }
 
}

Implementation of custom data encryption

This example shows how to specify custom serialization class. This class could be implemented also as Attribute (optional) to specify as class attribute.

public class CustomXOREncryption implements IDataEncryption
{
    /**
     * <p>
     * Gets or sets non empty key for encryption (at least one character)
     * </p>
     */
    public final int getKey(){ return auto_Key; }
    /**
     * <p>
     * Gets or sets non empty key for encryption (at least one character)
     * </p>
     */
    public final void setKey(int value){ auto_Key = value; }
    private int auto_Key;
 
    /**
     * <p>
     * Encode method to encrypt string.
     * </p>
     * @return Returns enccrypted string
     * @param source Source string to encode.
     */
    public final String encode(String source)
    {
        return process(source);
    }
 
    /**
     * <p>
     * Decode method to obtain decrypted string.
     * </p>
     * @return Returns decrypted string
     * @param source Source string to decode.
     */
    public final String decode(String source)
    {
        return process(source);
    }
 
    /**
     * <p>
     * Using XOR operation get encoded / decoded string
     * </p>
     * @return
     * @param source
     */
    private String process(String source)
    {
        StringBuilder src = new StringBuilder(source);
        StringBuilder dst = new StringBuilder(src.length());
        char chTmp;
        for (int index = 0; index < src.length(); ++index)
        {
            chTmp = src.charAt(index);
            chTmp = (char)(chTmp ^ this.getKey());
            dst.append(chTmp);
        }
        return dst.toString();
    }
}

Definition of class

This example shows how to define custom class with serialization and encryption properties and setup Format attributes for properties.

public static class DocumentSignatureData
{
    public String getID(){ return ID; }
    public void setID(String value){ ID = value; }
    @FormatAttribute(propertyName = "SignID")
    public String ID;
 
 
    public final String getAuthor(){ return Author; }
    public final void setAuthor(String value){ Author = value; }
    @FormatAttribute(propertyName = "SAuth")
    public String Author;
    //JAVA-added public wrapper for internalized property accessor
 
    public final java.util.Date getSigned() {  return Signed; }
    public final void setSigned(java.util.Date value) { Signed = value; }
    @FormatAttribute (propertyName = "SDate",propertyFormat = "yyyy-MM-dd")
    public java.util.Date Signed = new java.util.Date();
 
 
    public final java.math.BigDecimal getDataFactor() { return DataFactor; }
    public final void setDataFactor(java.math.BigDecimal value) { DataFactor = value; }
    @FormatAttribute (propertyName = "SDFact",propertyFormat = "N2")
    public java.math.BigDecimal DataFactor = new java.math.BigDecimal(0.01);
}

Search for embedded custom objects in metadata signatures

 This example shows how to decrypt previously embedded encrypted custom objects into metadata signature. MetadataSignature contains method getData to retrieve object

Signature signature = new Signature("MetadataCustomSerializationObject.docx");
 
// create data encryption
IDataEncryption encryption = new CustomXOREncryption();
 
MetadataSearchOptions options = new MetadataSearchOptions();
options.setDataEncryption(encryption);
 
// search for signatures in document
List<WordProcessingMetadataSignature> signatures = signature.search(WordProcessingMetadataSignature.class,options);
System.out.print("\nSource document contains following signatures.");
// get required metadata signatures
WordProcessingMetadataSignature wordSignature = null;
for (WordProcessingMetadataSignature mdSign : signatures) {
    if (mdSign.getName().equals("Signature")) {
        wordSignature = mdSign;
        break;
    }
}
if (wordSignature != null)
{
    DocumentSignatureData documentSignatureData = wordSignature.getData(DocumentSignatureData.class);
    if (documentSignatureData != null)
    {
        System.out.print("Signature has DocumentSignatureData object:\n ID = " + documentSignatureData.getID() + ", Author = " + documentSignatureData.getAuthor() + ", Signed = " + documentSignatureData.getSigned() + ", DataFactor " + documentSignatureData.getDataFactor());
    }
}
// get required metadata signatures
WordProcessingMetadataSignature mdAuthor = null;
for (WordProcessingMetadataSignature mdSign : signatures) {
    if (mdSign.getName().equals("Author")) {
        mdAuthor = mdSign;
        break;
    }
}
if (mdAuthor != null)
{
    System.out.print("Metadata signature found. Name : " + mdAuthor.getName() + ". Value: " + mdAuthor.getData(String.class));
}
// get required metadata signatures
WordProcessingMetadataSignature mdDocId = null;
for (WordProcessingMetadataSignature mdSign : signatures) {
    if (mdSign.getName().equals("DocumentId")) {
        mdDocId = mdSign;
        break;
    }
}
if (mdDocId != null)
{
    System.out.print("Metadata signature found. Name : " + mdDocId.getName() + ". Value: " + mdDocId.getData(String.class));
}

More resources

GitHub Examples 

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App 

Along with full-featured .NET library we provide simple, but powerful free Apps.
You are welcome to eSign PDF, Word, Excel, PowerPoint documents with free to use online GroupDocs Signature App.

© Aspose Pty Ltd 2001-2022. All Rights Reserved.