1

I'm planning to use these XMP Metadata property handlers to store my encryption keys in order for my programs to read it (for security purposes). I've done some on PDF Files and now I'm trying to add an encryption key support for Images and MS Word Files.

I'm using Aspose Imaging to convert any image to TIFF and add custom metadata to it but it seems that Metadata Extractor from github https://github.com/drewnoakes/metadata-extractor cannot read what I've imported.

Importing XMP via Aspose Imaging:

            using (TiffImage image = (TiffImage)Aspose.Imaging.Image.Load(imagepath))
            {
                
                XmpHeaderPi xmpHeader = new XmpHeaderPi("Company Metadata");

                XmpTrailerPi xmpTrailer = new XmpTrailerPi(true);

                XmpMeta xmpMeta = new XmpMeta();

                xmpMeta.AddAttribute("Company", "Some Company Inc.");
                xmpMeta.AddAttribute("EncryptionKey", cryptography.Encrypt(Guid.NewGuid().ToString(),"somekey"));

                XmpPacketWrapper xmpData = new XmpPacketWrapper(xmpHeader, xmpTrailer, xmpMeta);

                image.XmpData = xmpData;
                image.Save();

              
            }

Result from MetadataExtractor

enter image description here

Am I doing the wrong way to import metadata? Or is there any libraries that can read this instead of using Aspose Imaging to read it?

Kagetsuchi
  • 35
  • 6
  • Code looks correct to me. If you're sure the file contains xmp (open it in text or hex editor to check) then please open an issue on the GitHub project and attach an image. – Drew Noakes Feb 25 '21 at 11:08
  • @DrewNoakes Sorry for the late reply, it seems that there are no issues if you create an existing schema supported by the library instead of custom. Please check my answer in this thread. Thanks. – Kagetsuchi Mar 18 '21 at 07:19

1 Answers1

0

Finally solved my problem by creating a Dublin Core Schema instead of Custom XMP. Maybe the idea is to create an existing schema then add any custom values from it like handlers for encryption keys. (ex. dc:encryptionkey)

DublinCorePackage dublinCorePackage = new DublinCorePackage();
dublinCorePackage.SetAuthor("AUTHOR_HERE");
dublinCorePackage.SetTitle("Encrypted Image File");

//property handler for the encrypted key
dublinCorePackage.AddValue("dc:encryptionkey", "ENCRYPTION_KEY");

XmpPacketWrapper xmpData = new XmpPacketWrapper(xmpHeader, xmpTrailer, xmpMeta);

// Add dublinCore Package into XMP metadata
xmpData.AddPackage(dublinCorePackage);

Then use Metadata-Extractor to search the property created

private void ReadMetadata(string path)
    {
        var xmpDirectory = ImageMetadataReader.ReadMetadata(path).OfType<XmpDirectory>()?.FirstOrDefault();

        if (xmpDirectory == null) return;

        var query = xmpDirectory.XmpMeta.Properties.Where(e => e.Path == "dc:encryptionkey").FirstOrDefault();

        if (query != null)
        {
            var key = query.Value;
            var decrypt = cryptography.Decrypt(key, "SOME_KEY");
            Console.WriteLine(decrypt);
        }

    }

Debug Result:

enter image description here

Kagetsuchi
  • 35
  • 6