Highlighted
Absent Member.
Absent Member.
2829 views

Decrypt XML Elements with Asymmetric Keys

Jump to solution

I have this code working in VB.NET and I am migrating to VC2010. NET. Unfortunately I'm having trouble with this function. How would this code?

    Sub Decrypt(ByVal Doc As XmlDocument, ByVal Alg As RSA, ByVal KeyName As String)
        ' Check the arguments.  

        If KeyName Is Nothing Then
            Throw New ArgumentNullException("KeyName")
        End If
        ' Create a new EncryptedXml object.
        Dim exml As New EncryptedXml(Doc)
        ' Add a key-name mapping.
        ' This method can only decrypt documents
        ' that present the specified key name.
        exml.AddKeyNameMapping(KeyName, Alg)
        ' Decrypt the element.
        exml.DecryptDocument()
    End Sub

VC2010 .NET

       method-id Decrypt public.      
       local-storage section.
       01 exml type System.Xml.XmlDocument.       
       procedure division using by value cDoc as type XmlDocument by value cAlg As type RSA by value cKeyName As type String.       
      *    Dim exml As New EncryptedXml(Doc)       - vb.net
          
           set exml to new XmlDocument()
           set exml to cDoc
        
       
      *     exml.AddKeyNameMapping(KeyName, Alg)   - vb.net
              
      *     exml.DecryptDocument()                 - vb.net

           goback.
       end method.

0 Likes
1 Solution

Accepted Solutions
Highlighted
Absent Member.
Absent Member.

RE: Decrypt XML Elements with Asymmetric Keys

Jump to solution

It worked. Thank you very much.

View solution in original post

0 Likes
6 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Decrypt XML Elements with Asymmetric Keys

Jump to solution

Something like the following should work:

      $set ilusing"System.Xml" 
      $set ilusing"System.Security.Cryptography"
      $set ilusing"System.Security.Cryptography.Xml"
       class-id vbdecrypt.Class1.
       working-storage section.

       method-id Decrypt.
       local-storage section.
       procedure division using by value Doc as type XmlDocument
                                by value Alg as type RSA
                                by value KeyName as string.
        *> Check the arguments.  

           If KeyName = ""
            Raise New ArgumentNullException("KeyName")
           End-If
        *> Create a new EncryptedXml object.
           declare exml as type EncryptedXml = new EncryptedXml(Doc)
        *> Add a key-name mapping.
        *> This method can only decrypt documents
        *> that present the specified key name.
           invoke exml::AddKeyNameMapping(KeyName, Alg)
        *> Decrypt the element.
           invoke exml::DecryptDocument
    
           goback.
       end method.
    
       end class.
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Decrypt XML Elements with Asymmetric Keys

Jump to solution

Hi Altair, should be something like:

      class-id a.

      method-id Decrypt public.      

      local-storage section.

      01 exml type EncryptedXml.

      procedure division using by value Doc as type XmlDocument by value Alg As type RSA by value KeyName As string.

           if KeyName = null

               raise new ArgumentNullException("KeyName")

           end-if

           set exml to New EncryptedXml(Doc)

           invoke exml::AddKeyNameMapping(KeyName, Alg)

           invoke exml::DecryptDocument()

          goback.

      end method.

      end class.

I'm not sure where the EncryptedXml class is so cannot confirm 100% that this is correct.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Decrypt XML Elements with Asymmetric Keys

Jump to solution

Hi, Robert and  Chris

The VC isn´t EncryptedXml instantiating the class.

Being that it is part of namespaces: System.Security.Cryptography.Xml

01 exml type EncryptedXml. (Err)

In vb.net (on the same machine) this class is normally instantiated.

How to solve this error?

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Decrypt XML Elements with Asymmetric Keys

Jump to solution

You will have to add the assembly System.Security to the project-->References.

My example should work as is because I have added the ilusing directives to the top which bring in the required namespaces.

     $set ilusing"System.Xml"

     $set ilusing"System.Security.Cryptography"

     $set ilusing"System.Security.Cryptography.Xml"

In Robert's example you would have to add these.

My example uses a declare statement to define the exml data-item and Robert's defines this in local-storage.

Either method should work for you.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Decrypt XML Elements with Asymmetric Keys

Jump to solution

Hi Chris,

Thanks for the feedback.

I tested the two examples and both do not instantiate the class.

I had already added the ilusing directives to the top and referenced the nomespaces . Even so, do not instantiate the class. Attached  the source code.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Decrypt XML Elements with Asymmetric Keys

Jump to solution

It worked. Thank you very much.

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.