我要做的是使用Python Gae阅读一些PEM公共钥匙。

Rsakey模块不会解析PEM格式化的公共钥匙,而只是私人。

如果我可以从PEM获得模量和指数,我可以从那里去。

与OpenSSL ASN1PARSE一起探索典型的PEM(我将使用的类型)我可以找到 BIT STRING 他们居住的地方。

但是我不知道如何使用gdata asn1parser找到它们。

例如OpenSSL输出:

openssl asn1parse -i -in test.pem
 0:d=0  hl=3 l= 159 cons: SEQUENCE          
 3:d=1  hl=2 l=  13 cons:  SEQUENCE          
 5:d=2  hl=2 l=   9 prim:   OBJECT            :rsaEncryption
16:d=2  hl=2 l=   0 prim:   NULL              
18:d=1  hl=3 l= 141 prim:  BIT STRING

然后钻探我可以看到RSA模量和指数:

openssl asn1parse -strparse 18 -i -in test.pem 
  0:d=0  hl=3 l= 137 cons: SEQUENCE          
  3:d=1  hl=3 l= 129 prim:  INTEGER           :09C7A8007111B2B...
135:d=1  hl=2 l=   3 prim:  INTEGER           :010001

如果我然后将同样的pem拿来,然后将其粘在 bytes, ,如何获得正确的孩子来获得这些价值观?

asn1 = ASN1Parser(bytes)
modulus = asn1.getChild(1).getChild(0).value
exponent = asn1.getChild(1).getChild(1).value
binascii.hexlify(modulus)

或者是什么?我无法弄清楚我需要看什么级别等。我也不真的知道我在做什么...使用hexlify我可以看到那里的值,但是总是(与孩子一起玩,深度),前面有额外的东西,//或不如图所示。在openssl中。

有帮助吗?

解决方案

我修改了tlslite来做您在说的事情...这是一个应该为您提供帮助的片段。 “字节”是DER编码的公钥。

我认为您遇到的问题是有些钥匙可能具有“填充”。填充长度是有效载荷的第一个字节。然后,您需要跳过填充字节和许多字节。

@staticmethod
def parseDERPublicKey(bytes):
    a = ASN1Parser(bytes)
    b = a.getChild(1)
    padding = b.value[1]
    # TODO: I am assuming padding is 0, this is wrong.
    #       Skip the padding as well.
    c = b.value[1:] # get the mod/exp portion after the padding
    d = ASN1Parser(c)

    modulus = bytesToNumber(d.getChild(0).value)
    exponent = bytesToNumber(d.getChild(1).value)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top