Implement custom encryption with Metadata signatures
Implement custom encryption with Metadata signatures
Leave feedback
On this page
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 DataEncryption 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 interfaceprivateclassCustomXOREncryption:IDataEncryption{/// <summary>/// Gets or sets non empty key for encryption (at least one character)/// </summary>publicintKey{get;set;}/// <summary>/// Encode method to encrypt string./// </summary>/// <param name="source">Source string to encode.</param>/// <returns>Returns enccrypted string</returns>publicstringEncode(stringsource){returnProcess(source);}/// <summary>/// Decode method to obtain decrypted string./// </summary>/// <param name="source">Source string to decode.</param>/// <returns>Returns decrypted string</returns>publicstringDecode(stringsource){returnProcess(source);}/// <summary>/// Using XOR operation get encoded / decoded string/// </summary>/// <param name="source"></param>/// <returns></returns>privatestringProcess(stringsource){StringBuildersrc=newStringBuilder(source);StringBuilderdst=newStringBuilder(src.Length);charchTmp;for(intindex=0;index<src.Length;++index){chTmp=src[index];chTmp=(char)(chTmp^this.Key);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.
Implementation of embedding custom object into Metadata signature
This example shows how to embed custom object into Metadata signature.
using(Signaturesignature=newSignature("sample.docx")){// create data encryptionIDataEncryptionencryption=newCustomXOREncryption();// setup options with text of signatureMetadataSignOptionsoptions=newMetadataSignOptions(){// set encryption for all metadata signatures for this options// if you need separate encryption use own MetadataSignature.DataEncryption propertyDataEncryption=encryption};// create custom objectDocumentSignatureDatadocumentSignatureData=newDocumentSignatureData(){ID=Guid.NewGuid().ToString(),Author=Environment.UserName,Signed=DateTime.Now,DataFactor=11.22M};// setup signature metadataWordProcessingMetadataSignaturemdSignature=newWordProcessingMetadataSignature("Signature",documentSignatureData);// setup signature metadataWordProcessingMetadataSignaturemdAuthor=newWordProcessingMetadataSignature("Author","Mr.Scherlock Holmes");// setup data of document idWordProcessingMetadataSignaturemdDocId=newWordProcessingMetadataSignature("DocumentId",Guid.NewGuid().ToString());// add signatures to optionsoptions.Signatures.Add(mdSignature);options.Signatures.Add(mdAuthor);options.Signatures.Add(mdDocId);// sign document to filesignature.Sign("MetadataCustomEncryptionObject.docx",options);}
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: