简体   繁体   中英

C#: Metadata-Extractor cannot read the XMP created from Aspose Imaging

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

在此处输入图像描述

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?

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM