473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Why pack_into doesn't write correctly some bytes?

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

Expand|Select|Wrap|Line Numbers
  1. >>> from ctypes import create_string_buffer
  2. >>> from struct import pack_into
  3. >>> test = create_string_buffer(1)
  4. >>> pack_into("B", test, 0, 48)
  5. >>> print repr(test.raw)
  6. '0'
  7.  
The answer should be '\x30'. What I'm doing wrong?
Mar 12 '10 #1
3 2791
Glenton
391 Expert 256MB
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?
Mar 14 '10 #2
Glenton
391 Expert 256MB
Oh, by the way:

chr(48) = "0"
chr(112) = "p"
Mar 14 '10 #3
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:

Expand|Select|Wrap|Line Numbers
  1.     # Exponent and modulus must be in a string in OpenSSL's MPINT
  2.     # format - 4-byte big-endian bit-count followed by the appropriate
  3.     # number of bits
  4.  
  5.     # Exponent
  6.  
  7.     exponentFinal = create_string_buffer(4 + exponentSize)
  8.     exponentOffset = 0
  9.  
  10.     pack_into(">L", exponentFinal, exponentOffset, exponentSize)
  11.     exponentOffset += 4
  12.  
  13.     for i in range(0, exponentSize):
  14.       pack_into("B", exponentFinal, exponentOffset, ord(exponent[i]))
  15.       exponentOffset += 1
  16.  
  17.     # Modulus
  18.  
  19.     modulusSize = len(modulus)
  20.     modulusFinal = create_string_buffer(4 + modulusSize)
  21.     modulusOffset = 0
  22.  
  23.     pack_into(">L", modulusFinal, modulusOffset, modulusSize)
  24.     modulusOffset += 4
  25.  
  26.     for i in range(0, modulusSize):
  27.       pack_into("B", modulusFinal, modulusOffset, ord(modulus[i]))
  28.       modulusOffset += 1
  29.  
  30.     try:
  31.       rsa = RSA.new_pub_key((exponentFinal.raw, modulusFinal.raw))
  32.     except RSA.RSAError, e:
  33.       return None
  34.  
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
Mar 15 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Betty Hickman | last post by:
I'm having trouble writing a 2D array to a binary file. Here's what I have: FILE *outfile; short **clump_class; clump_class = malloc(nrows * sizeof(short *)); for (i=0; i<nrows; ++i)...
19
by: James Harris | last post by:
My K&R 2nd ed has in the Reference Manual appendix, A7.4.8 sizeof yields the number of BYTES required to store an object of the type of its operand. What happens if C is running on a machine that...
3
by: collinm | last post by:
hi i send a command to a led display, the led display is suppose to return me some character i write a string on a serial port void ledDisplayExist() { char msg={'\0', '\0', '\0', '\0',...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
3
by: Robert A. van Ginkel | last post by:
In news:OZ0W9RsdDHA.2432@TK2MSFTNGP10.phx.gbl... I ask the question how I can see if all the data is on the other side of the connection. I got as answer that I should use the blocking property. I...
7
by: PCH | last post by:
I'm trying to pull a data file that has an image (jpeg) embedded in it. I can locate the bytes for the image, but when I try to write it to a file, the image ends up being corrupt! the kb/b...
13
by: Jason | last post by:
Could someone here show me how I would write a vb program to convert decimal ip address to binary? For example a small form with a convert button and a label for the result and a textbox for the...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
9
by: andy.z | last post by:
If 2 people try to access the same text file at the same time to write to it - what happens in PHP ? What I mean is - presumably the first will be ok - But what will the second person actually...
4
by: Tzury Bar Yochay | last post by:
Hi, I can't find in the documentation the way to use these two functions. can someone share a simple code that utilize these two functions?
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.