示例#1
0
文件: cttx.py 项目: mpesce/CrypTweet
def get_key_by_name(uname):
	pubkey = keyring.get_key_by_name(uname)
	if pubkey == None:
		print "No matching public key, checking the keyserver..."
		pubkey = key_from_server(uname)
		if pubkey != None:
			print 'Found the public key in the keyserver, adding to local keyring...'
			# Add the key to our local list of keys
			keyring.add_key(uname, 8118, pubkey)
		else:
			print 'Could not find a public key for %s, aborting...' % (uname,)
			return None
	return pubkey
示例#2
0
	def do_GET(s):
		s.send_response(200)
		s.send_header("Content-type", "text/html")
		s.end_headers()
		parts = string.split(s.path,'/')  # Strip aw
		uname = parts[1]
		if uname == 'favicon.ico':
			return
		logging.info("Requesting public key for %s" % (uname,))
		key = keyring.get_key_by_name(uname)
		if key == None:
			s.wfile.write(json.dumps("{}"))
			logging.info("Public key not found.")
		else:
			s.wfile.write(json.dumps(key.save_pkcs1(format='PEM')))	
			logging.info("Public key found, returning JSON string.")	
示例#3
0
文件: txrx.py 项目: mpesce/CrypTweet
def send_cryptweet(uname, msg):
	"""Basically all of cttx.py in a single function.
	Ain't programming grand?
	Will return a tuple (success, error data), which will be None if no error thrown"""

	msg = msg.encode('ascii', 'ignore')	# Convert to ASCII
	if len(msg) > 140:
		logging.warning('DMs are limited to 140 characters -- message truncated!')
		msg = msg[0:139]	# Truncate the message

	# Encrypt message, and transform to base64 representation for transmission
	pubkey = keyring.get_key_by_name(uname)
	if pubkey is None:
		return (False, "No public key!")
	#logging.debug("Public key %s" % (pubkey, ))
	#logging.debug("Message is %s" % (msg, ))

	crypt_msg = rsa.encrypt(msg, pubkey)
	crypt_msg_64 = base64.b64encode(crypt_msg)
	sequence = packetize_msg(crypt_msg_64)
	tw = open_twitter()
	success = False
	if tw != None:
		logging.debug("Sending encrypted DM...")
		try:
			result = tw.direct_messages.new(user=uname, text=sequence[0])
			result = tw.direct_messages.new(user=uname, text=sequence[1])
			result = tw.direct_messages.new(user=uname, text=sequence[2])
			success = True

		except twitter.TwitterError as e:
			logging.error('Could not send the DM.  Is it possible %s is not following you?' % (uname,))
			logging.error(e)

		except twitter.TwitterHTTPError as e:
			logging.error('Could not communicate with Twitter.  Please try again later.')
			logging.error(e)
		
		except:			# Not sure what's gone wrong here.
			logging.error('You got some other kind of error: %s' % (sys.exc_info(),))
			e = sys.exc_info()

	if success:
		return (True, None)
	else:
		return (False, e)
示例#4
0
文件: ctwww.py 项目: mpesce/CrypTweet
	def pickuser():
		global pubkey
		uname = bottle.request.forms.uname
		force_lookup = bottle.request.forms.force_lookup
		logging.debug(force_lookup)
		if force_lookup == 'on':
			pubkey = None
			logging.debug("Forcing public key retreival from server")
		else:
			pubkey = keyring.get_key_by_name(uname)
		if pubkey:
			return bottle.template('sendmsg', user=uname)
		else:
			pubkey = txrx.key_from_server(uname)
			if pubkey:
				#return 'Found the public key for %s in the keyserver, adding to local keyring...' % (uname, )
				# Add the key to our local list of keys
				keyring.add_key(uname, 8118, pubkey)
				return bottle.template('sendmsg', user=uname)
			else:
				return bottle.template('nouser', user=uname)