Implement custom encryption with Metadata signatures
Implement custom encryption with Metadata signatures
Leave feedback
GroupDocs.Signature provides ability to embed into Metadata signature custom objects. This feature is implemented over object serialization to string and further encryption. By default library uses json format serialization and symmetric encryption but allows to provide custom encryption. This procedure requires implementation of interface IDataEncryption with two methods to encrypt and decrypt data.
Here are the steps to embed into Metadata values with custom encryption with GroupDocs.Signature:
Define custom data encryption class that implements IDataEncryption interface. By default GroupDocs.Signature has several encryption implementation you can use but allows user to customize it.
Implement if needed custom data serialization class that implement IDataSerializer interface. By default GroupDocs.Signature uses embedded json format serialization but allows user to customize it.
Implement class with properties and specify if needed class attributes (like custom serialization attribute, custom encryption attribute), specify attributes for properties like FormatAttribute to specify serialization name and display format, same as SkipSerializationAttribute to mark property of class as not serialize
Create new instance of Signature class and pass source document path as a constructor parameter
Instantiate the MetadataSignOptions object according to your requirements, add all metadata signatures to its collection and setup if needed setDataEncryption property
This example shows how to specify custom serialization class. This class could be implemented also as Attribute (optional) to specify as class attribute.
// Define class that implements IDataEncryption interface
publicclassCustomXOREncryptionimplementsIDataEncryption{/**
* <p>
* Gets or sets non empty key for encryption (at least one character)
* </p>
*/publicfinalintgetKey(){returnauto_Key;}/**
* <p>
* Gets or sets non empty key for encryption (at least one character)
* </p>
*/publicfinalvoidsetKey(intvalue){auto_Key=value;}privateintauto_Key;/**
* <p>
* Encode method to encrypt string.
* </p>
* @return Returns enccrypted string
* @param source Source string to encode.
*/publicfinalStringencode(Stringsource){returnprocess(source);}/**
* <p>
* Decode method to obtain decrypted string.
* </p>
* @return Returns decrypted string
* @param source Source string to decode.
*/publicfinalStringdecode(Stringsource){returnprocess(source);}/**
* <p>
* Using XOR operation get encoded / decoded string
* </p>
* @return
* @param source
*/privateStringprocess(Stringsource){StringBuildersrc=newStringBuilder(source);StringBuilderdst=newStringBuilder(src.length());charchTmp;for(intindex=0;index<src.length();++index){chTmp=src.charAt(index);chTmp=(char)(chTmp^this.getKey());dst.append(chTmp);}returndst.toString();}}
Definition of class
This example shows how to define custom class with serialization and encryption properties and setup Format attributes for properties.
publicclassDocumentSignatureData{publicStringgetID(){returnID;}publicvoidsetID(Stringvalue){ID=value;}@FormatAttribute(propertyName="SignID")publicStringID;publicfinalStringgetAuthor(){returnAuthor;}publicfinalvoidsetAuthor(Stringvalue){Author=value;}@FormatAttribute(propertyName="SAuth")publicStringAuthor;//JAVA-added public wrapper for internalized property accessor
publicfinaljava.util.DategetSigned(){returnSigned;}publicfinalvoidsetSigned(java.util.Datevalue){Signed=value;}@FormatAttribute(propertyName="SDate",propertyFormat="yyyy-MM-dd")publicjava.util.DateSigned=newjava.util.Date();publicfinaljava.math.BigDecimalgetDataFactor(){returnDataFactor;}publicfinalvoidsetDataFactor(java.math.BigDecimalvalue){DataFactor=value;}@FormatAttribute(propertyName="SDFact",propertyFormat="N2")publicjava.math.BigDecimalDataFactor=newjava.math.BigDecimal(0.01);}
Implementation of embedding custom object into Metadata signature
This example shows how to embed custom object into Metadata signature.
Signaturesignature=newSignature("sample.docx");// create data encryption
IDataEncryptionencryption=newCustomXOREncryption();// setup options with text of signature
MetadataSignOptionsoptions=newMetadataSignOptions();// set encryption for all metadata signatures for this options
// if you need separate encryption use own MetadataSignature.DataEncryption property
options.setDataEncryption(encryption);// create custom object
DocumentSignatureDatadocumentSignature=newDocumentSignatureData();documentSignature.setID(java.util.UUID.randomUUID().toString());documentSignature.setAuthor(System.getenv("USERNAME"));documentSignature.setSigned(newjava.util.Date());documentSignature.setDataFactor(newjava.math.BigDecimal("11.22"));// setup signature metadata
WordProcessingMetadataSignaturemdSignature=newWordProcessingMetadataSignature("Signature",documentSignature);// setup signature metadata
WordProcessingMetadataSignaturemdAuthor=newWordProcessingMetadataSignature("Author","Mr.Scherlock Holmes");// setup data of document id
WordProcessingMetadataSignaturemdDocId=newWordProcessingMetadataSignature("DocumentId",java.util.UUID.randomUUID().toString());// add signatures to options
options.getSignatures().add(mdSignature);options.getSignatures().add(mdAuthor);options.getSignatures().add(mdDocId);// sign document to file
SignResultsignResult=signature.sign("MetadataCustomEncryptionObject.docx",options);// analyzing result
System.out.print("List of newly created signatures:");intnumber=1;for(BaseSignaturetemp:signResult.getSucceeded()){System.out.print("Signature #"+number+++": Type: "+temp.getSignatureType()+" Id:"+temp.getSignatureId()+",Location: "+temp.getLeft()+"x"+temp.getTop()+". Size: "+temp.getWidth()+"x"+temp.getHeight());}
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples:
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.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.