Search for embedded object with custom serialization of QR-Code signatures
Search for embedded object with custom serialization of QR-Code signatures
Leave feedback
GroupDocs.Signature provides following abilities to search for embedded data objects in QR-code signatures (QrCodeSignature) that were embedded with custom encryption and serialization
ability to search for embedded custom objects into metadata and decrypt them to original source values
ability to search for custom encrypted QR-code signature and decrypt it
ability to search for custom serialized QR-code signatures
Here are the steps to search and decrypt previously encrypted text of QR-Code and decrypt custom object from QR-Code signature with GroupDocs.Signature API:
Implement (use) custom data serialization class that implements IDataEncryption interface.
Ensure that data class if defined with custom serialization attribute
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/// This implementation is using json library to provide serialization and deserialization/// </summary>publicclassCustomSerializationAttribute:Attribute,IDataSerializer{publicTDeserialize<T>(stringsource)whereT:class{returnJsonConvert.DeserializeObject<T>(source);}publicstringSerialize(objectdata){varserializerSettings=newJsonSerializerSettings{NullValueHandling=NullValueHandling.Ignore};returnJsonConvert.SerializeObject(data,serializerSettings);}}
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 interface// Class could inherits Attribute to use it with class definitionpublicclassCustomXOREncryption: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 for serialization.
// define class with custom serialization attribute[CustomSerialization]publicclassDocumentSignatureData{ [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 QR-code signatures
This example shows how to decrypt previously embedded encrypted custom objects into QrCodeSignature contains method GetData to retrieve object
using(Signaturesignature=newSignature("QRCodeCustomSerializationObject.pdf")){// create data encryptionIDataEncryptionencryption=newCustomXOREncryption();QrCodeSearchOptionsoptions=newQrCodeSearchOptions(){// specify special pages to search onAllPages=true,DataEncryption=encryption};// search for signatures in documentList<QrCodeSignature>signatures=signature.Search<QrCodeSignature>(options);Console.WriteLine("\nSource document contains following signatures:");foreach(varqrCodeSignatureinsignatures){Console.WriteLine("QRCode signature found at page {0} with type {1}.",qrCodeSignature.PageNumber,qrCodeSignature.EncodeType);DocumentSignatureDatadocumentSignatureData=qrCodeSignature.GetData<DocumentSignatureData>();if(documentSignatureData!=null){Console.WriteLine("QRCode signature has DocumentSignatureData object:\n ID = {0}, Author = {1}, Signed = {2}, DataFactor {3}",documentSignatureData.ID,documentSignatureData.Author,documentSignatureData.Signed.ToShortDateString(),documentSignatureData.DataFactor);}}}
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: