Search for embedded and encrypted objects in Metadata signatures
Search for embedded and encrypted objects in Metadata signatures
Leave feedback
GroupDocs.Signature provides additional features when searching for Metadata Signatures (MetadataSignature) that were previously encrypted or contains custom 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 with GroupDocs.Signature API:
Implement if needed custom data serialization class that implement IDataSerializer 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 if needed custom data encryption class that implementsIDataEncryption 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.
Process each Metadata signature and set property DataEncryption 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.
/// <summary>/// Creates class that implements IDataSerializer interface/// It cam support common serialization like JSon or custom data format/// </summary>classCustomSerializationAttribute:Attribute,IDataSerializer{publicTDeserialize<T>(stringsource)whereT:class{DocumentSignatureDataresult=newDocumentSignatureData();byte[]bytes=Encoding.UTF8.GetBytes(source);using(MemoryStreamstream=newMemoryStream(bytes)){stream.Seek(0,SeekOrigin.Begin);using(BinaryReaderreader=newBinaryReader(stream)){result.ID=reader.ReadString();result.Author=reader.ReadString();result.Signed=newDateTime(reader.ReadInt64());result.DataFactor=reader.ReadDecimal();}}varconverter=TypeDescriptor.GetConverter(typeof(T));return(T)converter.ConvertFrom(result);}publicstringSerialize(objectdata){stringresult=string.Empty;DocumentSignatureDatasignatureData=dataasDocumentSignatureData;if(signatureData!=null){using(MemoryStreamstream=newMemoryStream()){using(BinaryWriterwriter=newBinaryWriter(stream)){writer.Write(signatureData.ID);writer.Write(signatureData.Author);writer.Write(signatureData.Signed.Ticks);writer.Write(signatureData.DataFactor);}result=Encoding.UTF8.GetString(stream.ToArray());}}returnresult;}}
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.
// Define class that implements IDataEncryption interfaceprivateclassCustomXOREncryptionAttribute:Attribute,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.
// setup CustomSerialization Attribute to setup customer serialization(see example above)[CustomSerialization]// setup CustomXOREncryption Attribute for custom encryption (see example above)[CustomXOREncryption]privateclassDocumentSignatureData{ [Format("SignID")]publicstringID{get;set;} [Format("SAuth")]publicstringAuthor{get;set;} [Format("SDate", "yyyy-MM-dd")]publicDateTimeSigned{get;set;} [Format("SDFact", "N2")]publicdecimalDataFactor{get;set;} [SkipSerialization]publicstringComments{get;set;}}
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
// instantiating the signature objectusing(Signaturesignature=newSignature("signed.pdf")){// setup search optionsMetadataSearchOptionssearchOptions=newMetadataSearchOptions();// search documentList<MetadataSignature>signatures=signature.Search<MetadataSignature>(searchOptions);// output signaturesforeach(MetadataSignaturemetadataSignatureinsignatures){if(qrCodeSignature!=null){DocumentSignatureDatadocSignature=metadataSignature.GetData<DocumentSignatureData>();if(docSignature!=null){Console.WriteLine("Found DocumentSignature: #{0} by {1} from {2} DataFactor = {3}",docSignature.ID,docSignature.Author,docSignature.Signed,docSignature.DataFactor.ToString("N2"));}}}}
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: