[英]Using python to send unsigned char
I am doing a project related to cryptography. 我正在做一个与密码学相关的项目。 I am building the code in
python
. 我在
python
构建代码。 But the crypto functions use a C library PBC
for computations. 但加密函数使用C库
PBC
进行计算。 Hence I have made a C program that contains just the right functions and I have compiled it as a shared library. 因此,我制作了一个包含恰当函数的C程序,并将其编译为共享库。 I import the library using
ctypes
. 我使用
ctypes
导入库。 All has been well so far. 到目前为止,一切都很好。
The problem that I face now is that I need to take an unsigned char
declared as a global variable in the library and I need to send it through the python socket to another receiving python socket, where it will be passed back as an unsigned char
to the library. 我现在面临的问题是我需要在库中将一个
unsigned char
声明为全局变量,我需要通过python套接字将它发送到另一个接收python套接字,在那里它将作为unsigned char
传递给图书馆。
Right now, when I try to retrieve the unsigned char
from the library, 现在,当我尝试从库中检索
unsigned char
,
ibc = cdll.LoadLibrary('./libibc.so.1.0.1')
strin = 'some string'
ibc.init_pairing(1, 1, 1)# Initializing the lib
ibc.read_share()
id_str = (c_char * 40)()
id_str.value = strin
ibc.hash_id_s(id_str) # Passing a char to a function
hsid = (c_ubyte * 1000)()
hsid = c_ubyte.in_dll(ibc, "hid")
ibc.gen_privatekey(hsid, 1, 3) #pass unsigned char to function
The last line, ends in a segmentation fault. 最后一行以分段错误结束。 So I called the
gen_privatekey
function from c itself, using the following code : 所以我使用以下代码从c本身调用了
gen_privatekey
函数:
int genprkey(){
unsigned char key[100];
element_to_bytes(key, pks);
gen_privatekey(key, 1, 2);
}
I then call this function from python using : 然后我使用python从python调用此函数:
ibc.genprkey()
and the program succeeds. 并且程序成功。
PROBLEM 问题
The above function generates an unsigned char
which will also be stored in another global variable. 上面的函数生成一个
unsigned char
,它也将存储在另一个全局变量中。 I tried to retrieve the variable thus : 我试图检索变量:
hsidp = (c_char_p)()
hsidp = c_char_p.in_dll(ibc, "hid")
I tried the above code on the python interpreter. 我在python解释器上尝试了上面的代码。 When I type :
当我输入:
hsidp
output is : 输出是:
c_char_p(10598250801977757708)
But when I type : 但是当我键入:
hsidp.value
I get a segmentation fault (core dumped)
. 我遇到了
segmentation fault (core dumped)
。 I suspect that my way of handling the variable is not correct. 我怀疑我处理变量的方式不正确。 Once I get the
unsigned char
as a variable in python, I need to send it as a message to another socket that will read it and pass it as an unsigned char
to the same library. 一旦我将
unsigned char
作为python中的变量,我需要将它作为消息发送到另一个读取它的套接字并将其作为unsigned char
传递给同一个库。
I don't have an idea how this will be possible. 我不知道如何做到这一点。 Please help
请帮忙
Here's how to access an array in a shared library: 以下是如何访问共享库中的数组:
Dummy library: 虚拟图书馆:
>>> from ctypes import *
>>> import os
>>> open('lib.c', 'w').write(
... 'unsigned char data[] = {0, 1, 2, 3};\n')
>>> ret = os.system('gcc -shared -o lib.so lib.c')
Load the library and access a length 4 array of unsigned char
加载库并访问长度为4的
unsigned char
数组
>>> lib = CDLL('./lib.so')
>>> data = (c_ubyte * 4).in_dll(lib, 'data')
>>> list(data)
[0, 1, 2, 3]
Also, c_char_p
is a pointer, and getting that from the first 4 or 8 bytes of the array will point who knows where and thus likely segfault when you try to dereference it. 此外,
c_char_p
是一个指针,从数组的前4或8个字节获取该指针将指出谁知道在哪里,因此当您尝试取消引用它时可能会出现段错误。 Plus the first statement below instantiates a pointer that gets deallocated immediately when you reassign hsidp
in the 2nd statement: 另外,下面的第一个语句实例化一个指针,当您在第二个语句中重新分配
hsidp
,该指针会立即释放:
hsidp = (c_char_p)()
hsidp = c_char_p.in_dll(ibc, "hid")
Likewise the first line below creates a length 1000 array and throws it away. 同样,下面的第一行创建一个长度为1000的数组并将其抛出。 I don't know why.
我不知道为什么。 Then you get a single byte from the library.
然后你从库中得到一个字节。
hsid = (c_ubyte * 1000)()
hsid = c_ubyte.in_dll(ibc, "hid")
Do you need a byte or an array of bytes? 你需要一个字节或一个字节数组? What length?
什么长度? Or does
gen_privatekey
want a pointer to a null-terminated string? 或者
gen_privatekey
是否想要一个指向以null结尾的字符串的指针?
You can use string_at
to get a Python str
object: 您可以使用
string_at
来获取Python str
对象:
>>> hid = (c_ubyte * 24)(*map(ord, "This is a test."))
>>> string_at(hid, 24)
'This is a test.\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Or you can create a c_char
array from the buffer (or just use that from the outset instead of a c_ubyte
array): 或者你可以从缓冲区创建一个
c_char
数组(或者只是从一开始就使用它而不是c_ubyte
数组):
>>> (c_char * 24).from_buffer(hid).value
'This is a test.'
A c_char
array is more convenient for working with strings because of its value
and raw
descriptors that return Python str
objects. c_char
数组更方便使用字符串,因为它的value
和返回Python str
对象的raw
描述符。 These descriptors also allow you to write a string to the buffer: 这些描述符还允许您将字符串写入缓冲区:
>>> hid = (c_char * 24)()
>>> hid.value = 'This is a test.'
>>> hid.value
'This is a test.'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.