def importKey(): try: file = sys.argv[2] except (IndexError): print 'You need to supply a source key file!' print 'Ex: '+sys.argv[0]+' --import <key file>' sys.exit(1) try: with open(KEYS_DB): pass except IOError: print 'No keys database (keys.db)' print 'initialize the database with '+sys.argv[0]+' --init' sys.exit(1) print 'Importing new DH public key to database' with open (file, "r") as f: signed_data=f.read() verified = gpg.verify(str(signed_data)) if verified.username is not None: print('Verified signed by: %s' % verified.username) print('at trust level: %s' % verified.trust_text) else: print 'Signature not valid' sys.exit(0) data = signed_data.split('\n') pubkey = '' for line in data: if len(line) == 50: pubkey += line while pubkey[:1] == '0': pubkey = pubkey[1:] try: toEmail = verified.username.split('<')[1].split('>')[0] # regular email except IndexError: toEmail = verified.username # only a name - probably anonymous print 'To Email is: %s' % toEmail.lower() fromEmail = raw_input('Enter From Email: ') keys = dhutils.getKeys(fromEmail.lower(),toEmail.lower(),gpg,dbpassphrase) if not keys: print 'key doesn\'t exist for the '+fromEmail+' -> '+toEmail+' route' print 'create new key?' ans = raw_input('y/N: ') if ans == 'y': dhutils.insertKeys(fromEmail.lower(),toEmail.lower(),pubkey.lower(),gpg,dbpassphrase) else: print 'key exists for the '+fromEmail.lower()+' -> '+toEmail.lower()+' route' print 'change key?' ans = raw_input('y/N: ') if ans == 'y': dhutils.changePubKey(fromEmail.lower(),toEmail.lower(),pubkey.lower(),gpg,dbpassphrase)
def mutate(): try: fromEmail = sys.argv[2] toEmail = sys.argv[3] except (IndexError): print 'You need to supply a fromEmail, and toEmail!' print 'Ex: '+sys.argv[0]+' --mutate-key <fromEmail> <toEmail>' sys.exit(1) try: with open(KEYS_DB): pass except IOError: print 'No keys database (keys.db)' print 'initialize the database with '+sys.argv[0]+' --init' sys.exit(1) oldpassphrase = dhutils.genSharedSecret(fromEmail,toEmail,gpg,dbpassphrase) dhutils.mutateKey(fromEmail,toEmail,gpg,dbpassphrase) privkey, mypubkey, otherpubkey = dhutils.getKeys(fromEmail,toEmail,gpg,dbpassphrase) while len(mypubkey) < 50*50: mypubkey = '0'+mypubkey brokenkey = [mypubkey[i:i+50] for i in range(0, len(mypubkey), 50)] new_mypubkey = '' for line in brokenkey: new_mypubkey += line+'\n' passphrase = getpass('Signing key ('+fromEmail+') password: '******'DH Public Key:\n'+new_mypubkey+'\n', passphrase=passphrase, keyid=fromEmail) print '' print str(signed_data) ans = raw_input('Do you want to send this key anonymously? (y/N)') if ans == 'y': sendAnon = True else: sendAnon = False msg = gpg.encrypt(str(signed_data), recipients=None, symmetric=CIPHER, always_trust=True, passphrase=oldpassphrase) if sendAnon: iv = hsub.cryptorandom() hsubject = hsub.hash(oldpassphrase) with open('mutatedkey.asc', "w") as f: if sendAnon: f.write('To: [email protected],[email protected]\n') f.write('Subject: %s\n' % hsubject) f.write('Newsgroups: alt.anonymous.messages\n') f.write('X-No-Archive: Yes\n') f.write('\n') f.write(re.sub('\nV.*$', '', str(msg), count=1, flags=re.MULTILINE)) print 'New key encrypted with old DH shared secret is in "mutatedkey.asc"' print 'Get unencrypted, signed copy of new key with '+sys.argv[0]+' --sign-pub '+fromEmail+' '+toEmail
def get(): try: fromEmail = sys.argv[2] toEmail = sys.argv[3] except (IndexError): print 'You need to supply a fromEmail, and toEmail!' print 'Ex: '+sys.argv[0]+' --get-key <fromEmail> <toEmail>' sys.exit(1) try: with open(KEYS_DB): pass except IOError: print 'No keys database (keys.db)' print 'initialize the database with '+sys.argv[0]+' --init' sys.exit(1) privkey, mypubkey, otherpubkey = dhutils.getKeys(fromEmail,toEmail,gpg,dbpassphrase) print fromEmail+' Public Key: ', mypubkey print toEmail+' Public Key: ', otherpubkey
def sign_pub(): try: fromEmail = sys.argv[2] toEmail = sys.argv[3] except (IndexError): print 'You need to supply source and target email addresses!' print 'Ex: ' + sys.argv[0] + ' --sign-pub <fromEmail> <toEmail>' sys.exit(1) try: with open(KEYS_DB): pass except IOError: print 'No keys database (keys.db)' print 'initialize the database with ' + sys.argv[0] + ' --init' sys.exit(1) privkey, mypubkey, otherpubkey = dhutils.getKeys(fromEmail, toEmail, gpg, dbpassphrase) while len(mypubkey) < 50 * 50: mypubkey = '0' + mypubkey brokenkey = [mypubkey[i:i + 50] for i in range(0, len(mypubkey), 50)] new_mypubkey = '' for line in brokenkey: new_mypubkey += line + '\n' passphrase = getpass('Signing key (' + fromEmail + ') password: '******'DH Public Key:\n' + new_mypubkey + '\n', passphrase=passphrase, keyid=fromEmail) print '' print str(signed_data) verified = gpg.verify(str(signed_data)) if verified.username is not None: print('Verified signed by: %s' % verified.username) print('at trust level: %s' % verified.trust_text) else: print 'Sigature not verified'
def get(): try: fromEmail = sys.argv[2] toEmail = sys.argv[3] except (IndexError): print 'You need to supply a fromEmail, and toEmail!' print 'Ex: ' + sys.argv[0] + ' --get-key <fromEmail> <toEmail>' sys.exit(1) try: with open(KEYS_DB): pass except IOError: print 'No keys database (keys.db)' print 'initialize the database with ' + sys.argv[0] + ' --init' sys.exit(1) privkey, mypubkey, otherpubkey = dhutils.getKeys(fromEmail, toEmail, gpg, dbpassphrase) print fromEmail + ' Public Key: ', mypubkey print toEmail + ' Public Key: ', otherpubkey
def sign_pub(): try: fromEmail = sys.argv[2] toEmail = sys.argv[3] except (IndexError): print 'You need to supply source and target email addresses!' print 'Ex: '+sys.argv[0]+' --sign-pub <fromEmail> <toEmail>' sys.exit(1) try: with open(KEYS_DB): pass except IOError: print 'No keys database (keys.db)' print 'initialize the database with '+sys.argv[0]+' --init' sys.exit(1) privkey, mypubkey, otherpubkey = dhutils.getKeys(fromEmail,toEmail,gpg,dbpassphrase) while len(mypubkey) < 50*50: mypubkey = '0'+mypubkey brokenkey = [mypubkey[i:i+50] for i in range(0, len(mypubkey), 50)] new_mypubkey = '' for line in brokenkey: new_mypubkey += line+'\n' passphrase = getpass('Signing key ('+fromEmail+') password: '******'DH Public Key:\n'+new_mypubkey+'\n', passphrase=passphrase, keyid=fromEmail) print '' print str(signed_data) verified = gpg.verify(str(signed_data)) if verified.username is not None: print('Verified signed by: %s' % verified.username) print('at trust level: %s' % verified.trust_text) else: print 'Sigature not verified'
def mutate(): try: fromEmail = sys.argv[2] toEmail = sys.argv[3] except (IndexError): print 'You need to supply a fromEmail, and toEmail!' print 'Ex: ' + sys.argv[0] + ' --mutate-key <fromEmail> <toEmail>' sys.exit(1) try: with open(KEYS_DB): pass except IOError: print 'No keys database (keys.db)' print 'initialize the database with ' + sys.argv[0] + ' --init' sys.exit(1) oldpassphrase = dhutils.genSharedSecret(fromEmail, toEmail, gpg, dbpassphrase) dhutils.mutateKey(fromEmail, toEmail, gpg, dbpassphrase) privkey, mypubkey, otherpubkey = dhutils.getKeys(fromEmail, toEmail, gpg, dbpassphrase) while len(mypubkey) < 50 * 50: mypubkey = '0' + mypubkey brokenkey = [mypubkey[i:i + 50] for i in range(0, len(mypubkey), 50)] new_mypubkey = '' for line in brokenkey: new_mypubkey += line + '\n' passphrase = getpass('Signing key (' + fromEmail + ') password: '******'DH Public Key:\n' + new_mypubkey + '\n', passphrase=passphrase, keyid=fromEmail) print '' print str(signed_data) ans = raw_input('Do you want to send this key anonymously? (y/N)') if ans == 'y': sendAnon = True else: sendAnon = False msg = gpg.encrypt(str(signed_data), recipients=None, symmetric=CIPHER, always_trust=True, passphrase=oldpassphrase) if sendAnon: iv = hsub.cryptorandom() hsubject = hsub.hash(oldpassphrase) with open('mutatedkey.asc', "w") as f: if sendAnon: f.write('To: [email protected],[email protected]\n') f.write('Subject: %s\n' % hsubject) f.write('Newsgroups: alt.anonymous.messages\n') f.write('X-No-Archive: Yes\n') f.write('\n') f.write(re.sub('\nV.*$', '', str(msg), count=1, flags=re.MULTILINE)) print 'New key encrypted with old DH shared secret is in "mutatedkey.asc"' print 'Get unencrypted, signed copy of new key with ' + sys.argv[ 0] + ' --sign-pub ' + fromEmail + ' ' + toEmail
def importKey(): try: file = sys.argv[2] except (IndexError): print 'You need to supply a source key file!' print 'Ex: ' + sys.argv[0] + ' --import <key file>' sys.exit(1) try: with open(KEYS_DB): pass except IOError: print 'No keys database (keys.db)' print 'initialize the database with ' + sys.argv[0] + ' --init' sys.exit(1) print 'Importing new DH public key to database' with open(file, "r") as f: signed_data = f.read() verified = gpg.verify(str(signed_data)) if verified.username is not None: print('Verified signed by: %s' % verified.username) print('at trust level: %s' % verified.trust_text) else: print 'Signature not valid' sys.exit(0) data = signed_data.split('\n') pubkey = '' for line in data: if len(line) == 50: pubkey += line while pubkey[:1] == '0': pubkey = pubkey[1:] try: toEmail = verified.username.split('<')[1].split('>')[ 0] # regular email except IndexError: toEmail = verified.username # only a name - probably anonymous print 'To Email is: %s' % toEmail.lower() fromEmail = raw_input('Enter From Email: ') keys = dhutils.getKeys(fromEmail.lower(), toEmail.lower(), gpg, dbpassphrase) if not keys: print 'key doesn\'t exist for the ' + fromEmail + ' -> ' + toEmail + ' route' print 'create new key?' ans = raw_input('y/N: ') if ans == 'y': dhutils.insertKeys(fromEmail.lower(), toEmail.lower(), pubkey.lower(), gpg, dbpassphrase) else: print 'key exists for the ' + fromEmail.lower( ) + ' -> ' + toEmail.lower() + ' route' print 'change key?' ans = raw_input('y/N: ') if ans == 'y': dhutils.changePubKey(fromEmail.lower(), toEmail.lower(), pubkey.lower(), gpg, dbpassphrase)