1

这是从 pgp 密钥中提取算法特定值的正确方法吗?我有一个奇怪的问题,除了 DSA 和 ElGamal 的最后一个值之外的所有值都可以正确提取,这会弄乱我程序的其余部分

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

例如:我有一个从网上某处获得的密钥,其中的数据(从 pgpdump.net 转储)为:

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 bits) - ... 
  ElGamal g(2 bits) - ... 
  ElGamal y(2044 bits) - ...

我会得到除了 y 之外的所有东西,这会弄乱我的程序的其余部分。我得到的 y 长度是 2079,这是没有意义的,因为其他一切都是正确的,但是当我查看二进制数据时,程序并没有读错,所以我不知道我的编程哪里出错了。

有任何想法吗?

4

1 回答 1

1

我对 Python 不熟悉,但似乎 data = data[16 + length:] 跳过了 16 + length: bits in data,但在 OpenPGP 中,多精度整数的主体被填充到字节边界,即你应该写类似 data =数据[16 + (((长度 + 7)/8)*8):]

于 2010-12-29T13:02:36.030 回答