开发者

pgp algorithm specific data extraction

would this be a correct way extract the algorithm specific values from a pgp key? i have a weird problem where all but the last values for DSA and ElGamal can be extracted properly, which then messes up the rest of my program

def algorithm(data, pka):
    #data is a large binary string with the algorithm specific values, 
    #along with the rest of the data

    # MPI is defined as: 2 byte showing how many bits the value is + the value
    if pka in [pub[1], pub[2], pub[3]]:  # the 3 different RSA values/types
        '''MPI of RSA public modulus n'''
        # I havent actually used this part of the code yet
        length = int(data[:16], 2)       # get the length of the value in bits 
        n = int(data[16:16 + length], 2) # get the value
        data = data[16 + length:]        # remove the MPI from the data

            '''MPI of RSA public modulus e'''
        length = int(data[:16], 2)
        e = int(data[16:16 + length], 2)
        data = data[16 + length:]

            temp = [n, e]
    elif pka == 'DSA':
        '''MPI of DSA prime p'''
        length = int(data[:16], 2)
        p = int(data[16:16 + length], 2)
        data = data[16 + length:]

        '''MPI of DSA group order q (q is a prime divisor of p-1)'''
        length = int(data[:16], 2)
        q = int(data[16:16 + length], 2)
        data = data[16 + length:]

        '''MPI of DSA group generator g'''
        length = int(data[:16], 2)
        g = int(data[16:16 + length], 2)
        data = data[16 + length:]

        '''MPI of DSA public-key value y (= g**x mod p where x is secret)'''
        length = int(data[:16], 2)
        y = int(data[16:16 + length], 2)
        data = data[16 + length:]

            temp = [p, q, g, y]
    elif pka == 'Elgamal (Encrypt-Only)':
        '''MPI of Elgamal prime p'''
        length = int(data[:16], 2)
        p = int(data[16:16 + length], 2)
        data = data[16 + length:]

        '''MPI of Elgamal group generator g'''
        length = int(data[:16], 2)
        g = int(data[16:16 + length], 2)
        data = data[16 + length:]

        '''MPI of Elgamal public key value y (= g**x mod p where x
            is secret)'''
        length = int(data[:16], 2)
        y = int(data[16:16 + length], 2)
        data = data[16 + length:]

            temp = [p, g, y]

    return data, temp

for example: i have a key i got from somewhere online with data (dumped from pgpdump.net) being:

Old: Public Subkey Packet(tag 14)(525 bytes) 
  Ver 4 - new Public key creation time - Sat Oct 24 20:28:43 UTC 1998 
  Pub alg - ElGamal Encrypt-Only(pub 16) 
  ElGamal p(2048 b开发者_如何学Cits) - ... 
  ElGamal g(2 bits) - ... 
  ElGamal y(2044 bits) - ...

i would get everything except y, which messes up the rest of my program. the length i get for y is 2079, which makes no sense, since everything else is correct, but when i look at the binary data, the program is not reading it wrong, so i dont get where my programming is going wrong.

any ideas?


I'm not familiar with Python, but it seems that data = data[16 + length:] skips 16 + length: bits in data, but in OpenPGP multiprecision integers body is padded to byte boundary, i.e. you should write something like data = data[16 + (((length + 7)/8)*8):]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜