Decrypt XML Elements with Asymmetric Keys

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.

  • 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.
    
  • 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.

  • 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?

  • 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.

  • 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.

  • 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.

  • 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.