Why pack_into doesn't write correctly some bytes?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rafael Justo
    New Member
    • Mar 2007
    • 9

    Why pack_into doesn't write correctly some bytes?

    When I try to write a specific byte (letter 'p') in a writable buffer using pack_into the result is a corrupted data:

    Code:
    >>> from ctypes import create_string_buffer
    >>> from struct import pack_into
    >>> test = create_string_buffer(1)
    >>> pack_into("B", test, 0, 48)
    >>> print repr(test.raw)
    '0'
    The answer should be '\x30'. What I'm doing wrong?
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    What are you trying to do? I've not really come across those commands before, and wonder if they are still relevant to modern versions of python?

    Comment

    • Glenton
      Recognized Expert Contributor
      • Nov 2008
      • 391

      #3
      Oh, by the way:

      chr(48) = "0"
      chr(112) = "p"

      Comment

      • Rafael Justo
        New Member
        • Mar 2007
        • 9

        #4
        I use pack_into to build a buffer of bytes that are going to be the modulus of a public key. I think that the problem isn't what I suspected, the \x30 when wrote in a terminal is replaced by 'zero' as you said.

        The main problem is to convert a DNSKEY (dns record) into a public key structure (RSA) of M2crypto. Today I'm doing the following:

        Code:
            # Exponent and modulus must be in a string in OpenSSL's MPINT
            # format - 4-byte big-endian bit-count followed by the appropriate
            # number of bits
        
            # Exponent
        
            exponentFinal = create_string_buffer(4 + exponentSize)
            exponentOffset = 0
        
            pack_into(">L", exponentFinal, exponentOffset, exponentSize)
            exponentOffset += 4
        
            for i in range(0, exponentSize):
              pack_into("B", exponentFinal, exponentOffset, ord(exponent[i]))
              exponentOffset += 1
        
            # Modulus
        
            modulusSize = len(modulus)
            modulusFinal = create_string_buffer(4 + modulusSize)
            modulusOffset = 0
        
            pack_into(">L", modulusFinal, modulusOffset, modulusSize)
            modulusOffset += 4
        
            for i in range(0, modulusSize):
              pack_into("B", modulusFinal, modulusOffset, ord(modulus[i]))
              modulusOffset += 1
        
            try:
              rsa = RSA.new_pub_key((exponentFinal.raw, modulusFinal.raw))
            except RSA.RSAError, e:
              return None
        But something is wrong, because when I try to verify a signature with the public key a got an error telling that the modulus isn't compatible with data size.

        Best regards,
        Rafael

        Comment

        Working...