python 中使用 M2Crypto 想读写文件时总是报:
OPENSSL_Uplink(58D46000,08): no OPENSSL_Applink
windows 平台下使用 openssl 时不能直接操作与文件 IO 有关的函数。 
因为 ms/uplink.c 会动态从当前 exe 中获取 ms/applink.c 的函数表,逻辑是
void OPENSSL_Uplink (volatile void **table, int index)
{
HANDLE h = GetModuleHandle(NULL)
GetProcAddress(h,"OPENSSL_Applink")
}
。如果想使用,则必须在链接 exe 时候也链接 ms/uplink.c。
 
当 python 使用 M2Crypto 时,如果操作文件,可先创建一个 MemoryBuffer 的 BIO 对象, 然后将 MemoryBuffer 读取为字符串,手动将字符串写入文件。
print "Generating a 1024 bit private/public key pair for Bob..."
Bob = M2Crypto.RSA.gen_key (1024, 65537)

if os.name == 'nt':
    mb = M2Crypto.BIO.MemoryBuffer()
    Bob.save_key_bio(mb, None)
    with open('Bob-private.pem', 'wb') as f:
        f.write(mb.read_all())
else:
    Bob.save_key ('Bob-private.pem', None)

 读时顺序相反,先读为字符串,然后创建 MemoryBuffer 的 BIO 对象,接着用 BIO 创建所需对象

if os.name == 'nt':
    mb = None
    with open('Bob-private.pem', 'rb') as f:
        mb = M2Crypto.BIO.MemoryBuffer(f.read())

    if mb:
        Bob = M2Crypto.RSA.load_key_bio(mb)
    else:
        print 'error load key'
else:
    Bob = M2Crypto.RSA.load_key('Bob-private.pem')

 

相关文章:

  • 2021-06-03
  • 2022-12-23
  • 2021-09-18
  • 2021-11-25
  • 2022-12-23
  • 2021-06-27
  • 2021-11-04
  • 2021-06-15
猜你喜欢
  • 2022-12-23
  • 2021-05-18
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
  • 2022-01-06
  • 2022-12-23
相关资源
相似解决方案