#!/usr/bin/python sig=""" -----BEGIN PGP SIGNED MESSAGE----- """ import pure # # 2003/8/20 # # Read key from PGP-keyfile and store in a file. # Keys must be in Version 3 format or legacy format. # Secret keys must have an empty passphrase in order to # extract the secret decryption value. # # PUBKEY-Structure assumed: (Version 3 and version 2) # # octetts value meaning # 1 153 indicates public key # 2/3 length of whole key # 4 3/2 version # 5/6/7/8 creation time # 9/10 expiration time in days # 11 1 RSA-Algorithm # 12+x MPI: modulus n # 12+x+y MPI: encryption e # Rest User-ID packet begining with 180 # signature packets begining with 137 # followed by 19 octetts and one MPI # # SECRETKEY-Structure assumed: # # octets value meaning # 1 149 indicates secret key # 2/3 length of whole key # 4 3/2 version # 5/6/7/8 creation time # 9/10 expiration time in days # 11 1 RSA-Algorithm # 12+x MPI: modulus n # 12+x+y MPI: encryption e # 12+x+y+1 0 indicates secret values are unencrypted # 12+x+y+1 3 IDEA encryption of secret values followed by 8 octets of data # 14+x+y (+8) MPI: Decryption (secret key) # MPI: p # MPI: q (q 0 : length = length * 256 + ord(key[i]) i = i + 1 N = N - 1 return length ######################################################### def readMPI(): global i Length = 0 Length = readNbytes(2) # in bits Bytes = Length / 8 if Length % 8 != 0 : Bytes = Bytes + 1 # read L octets print "reading " , pure.toString(Length), " bit or ", print pure.toString(Bytes), " bytes" X = 1 MPI = 0L while X <= Bytes : MPI = MPI * 256 + ord(key[i]) i = i + 1 X = X + 1 return MPI ######################################################### def dearmor(armoredString) : List = [] for C in armoredString : Y = 0 if ord(C) >= 97 : # lowercase Y = ord(C) - 71 if ord(C) >= 65 and ord(C) <= 90 : # lowercase Y = ord(C) - 65 if ord(C) >= 48 and ord(C) <= 57 : # decimal Y = ord(C) + 4 if ord(C) == 43 : # + 62 Y = ord(C) + 19 if ord(C) == 47 : # / 63 Y = ord(C) +16 List.append(Y) TEXT = "" i = 0 while i+1 <= len(armoredString) : A = B = C = D = 0 A = List[i] if i+1 < len(armoredString) : i = i + 1 B = List[i] if i+1 < len(armoredString) : i = i + 1 C = List[i] if i+1 < len(armoredString) : i = i + 1 D = List[i] i = i + 1 E = (((A * 64) + B) * 64 + C) * 64 + D LINE = "" K = 3 while K > 0 : REM = E % 256 E = E / 256 LINE = chr(REM) + LINE K = K - 1 TEXT = TEXT + LINE return TEXT ######################################################### if len(sys.argv) != 3 : print "usage: read-pgpkey infile outfile" else: try: FILE = open (sys.argv[1], "r") keyfile = FILE.readlines() FILE.close() except: print "Cannot read input file" sys.exit(3) i = 0 data = "" FirstByte = ord(keyfile[0][0]) if FirstByte != 149 and FirstByte != 153 : # keyfile is armored while keyfile[i][:25] != "-----BEGIN PGP SECRET KEY" and \ keyfile[i][:26] != "-----BEGIN PGP PRIVATE KEY" and \ keyfile[i][:25] != "-----BEGIN PGP PUBLIC KEY" : i = i + 1 if i < len(keyfile) : i = i + 2 while keyfile[i][:23] != "-----END PGP SECRET KEY" and \ keyfile[i][:24] != "-----END PGP PRIVATE KEY" and \ keyfile[i][:23] != "-----END PGP PUBLIC KEY" : data = data + pure.Line(keyfile[i]) i = i + 1 key = dearmor(data) else: # keyfile is binary key = "" for Line in keyfile: key = key + Line # print first 11 octetts print "First 11 octetts:" for X in range(11) : print ord(key[X]), print print i = 0 if ord(key[i]) == 153 : print "PUBLIC KEY\n" i = i + 3 if ord(key[i]) == 3 : print "Version 3 key" if ord(key[i]) == 2 : print "Version 2 key" i = i + 1 time = readNbytes(4) print "Time:" + pure.toString(time) Expiration = readNbytes(2) print "Key expires:" + pure.toString(Expiration) if ord(key[i]) == 1 : print "Algo 1" i = i + 1 Modulus = 0L Encryption = 0L Modulus = readMPI() print "Modulus = ", pure.toString(Modulus) Encryption = readMPI() print "Encryption = ", pure.toString(Encryption) if ord(key[i]) == 180 : i = i + 1 Length = readNbytes(1) UserID = key[i:int(i+Length)] print UserID KEY = "" KEY = KEY + "Modulus = " + pure.toString(Modulus) + "\n" KEY = KEY + "Encryption = " + pure.toString(Encryption) + "\n" KEY = KEY + "Hashmodulus = 1" + "\n" KEY = KEY + "Generator = 1" + "\n" KEY = KEY + UserID + "\n" KEY = KEY + "Securityhash = None" + "\n" #------------------------------------------------------------------# if ord(key[i]) == 149 : print "SECRET KEY" i = i + 3 if ord(key[i]) == 3 : print "Version 3 key" if ord(key[i]) == 2 : print "Version 2 key" i = i + 1 time = readNbytes(4) print "Time:" + pure.toString(time) Expiration = readNbytes(2) print "Key expires:" + pure.toString(Expiration) if ord(key[i]) == 1 : print "Algo 1" i = i + 1 Modulus = 0L Encryption = 0L Decryption = 0L Modulus = readMPI() print "Modulus = ", pure.toString(Modulus) Encryption = readMPI() print "Encryption = ", pure.toString(Encryption) if ord(key[i]) == 0 : print print "Secret key is stored unprotected.\n" i = i + 1 Decryption = readMPI() print "Decryption = ", pure.toString(Decryption) p = readMPI() print "p = ", pure.toString(p) q = readMPI() print "q = ", pure.toString(q) r = readMPI() checksum = readNbytes(2) if ord(key[i]) == 180 : print "User-ID packet:", i = i + 1 Length = readNbytes(1) UserID = key[i:int(i+Length)] print UserID else: print "Secret key is stored protected with ALGO ", i = i + 1 Algo = readNbytes(1) print Algo print "Skipping the rest of the key!" sys.exit(4) KEY = "" KEY = KEY + "Modulus = " + pure.toString(Modulus) + "\n" KEY = KEY + "Encryption = " + pure.toString(Encryption) + "\n" KEY = KEY + "Decryption = " + pure.toString(Decryption) + "\n" KEY = KEY + "Hashmodulus = 1" + "\n" KEY = KEY + "Generator = 1" + "\n" KEY = KEY + UserID + "\n" KEY = KEY + "Protection = None\n" KEY = KEY + "Securityhash = None" + "\n" #------------------------------------------------------------------# outfile = sys.argv[2] try: FILE = open (outfile, "w") FILE.write(KEY) FILE.close() except: print "Cannot write file to filesystem" sys.exit(3) print "EXIT" sys.exit(0) ################################################################## sig=""" -----BEGIN PGP SIGNATURE----- Version: 2.6.3in Charset: noconv iQEVAwUBP0ccbL6wVDeIE49tAQFrQAgAlJJl1EG1VXYLc1MSrAWSH6aC0XupN74A CLbqyS/M9zAP37V4cGXrQss7ZAzFwxfrf+S3E4BU8NDGBEN5JyavYiOt7PiOnnAJ fPwOzDnuPdmZku31sVFyRaNZVxi05U9jLMerVESn8W24cjU0NxZOep/2OojPybhX 5kIe3P//DYe/Hw7CXVqmHsogWrnjYT814E3GtItf3CWOj7wIYwu1Veg4oov+p+vw 7A4tp6UmY/s91tVpYwQF9FBLwAK+BUm5njXF3dSq1syiGO/g+8VU7CoSr8Z+v3y/ o+hhIO25LVD2HGc/qFkMzVvFelrjOFSZ/b0HU4wKV40omu4DGRmlig== =QHxj -----END PGP SIGNATURE----- """