def write_xml(self): # Created Salmon decrypted header decrypted_header = "<decrypted_header>\n\ <iv>%s</iv>\n\ <aes_key>%s</aes_key>\n\ <author>\n\ <name>%s</name>\n\ <uri>acct:%s</uri>\n\ </author>\n\ </decrypted_header>\n" % (base64.urlsafe_b64encode(self.aes_key[1]),base64.urlsafe_b64encode(self.aes_key[0]),self.author,self.author_uri) # Encrypt decrypted_header key = aes_helper.get_random_key() ciphertext = base64.b64encode(aes_helper.encrypt(decrypted_header,key)) # Encrypt AES session-key with the receivers public key key_hash = simplejson.dumps({'key':base64.b64encode(key[0]),'iv':base64.b64encode(key[1])}) encrypted_key = base64.b64encode(rsa_helper.encrypt(key_hash,self.public_key)) # Pack encrypted header encrypted_header = base64.b64encode(simplejson.dumps({'aes_key':encrypted_key,'ciphertext':ciphertext})) # Put it all together to a nice Salmon-friendly atom XML xml = "<?xml version='1.0' encoding='UTF-8'?>\n\ <entry xmlns='http://www.w3.org/2005/Atom'>\n\ <encrypted_header>%s</encrypted_header>\n\ %s\n\ </entry>" % (encrypted_header,self.envelope) return xml
def create(self,author_name,author_uri,author_private_key,receiver_public_key,activity): self.author = author_name self.author_uri = author_uri self.author_private_key = rsa_helper.pem_to_private_tuple(author_private_key) self.public_key = receiver_public_key # Generate AES Key self.aes_key = aes_helper.get_random_key() # Encrypt activity self.env_message = base64.urlsafe_b64encode(base64.urlsafe_b64encode(aes_helper.encrypt(activity,self.aes_key))) # Sign the activity data self.env_signature = self.sign_salmon(self.env_message,self.author_private_key) # Create the magic signature envelope env_protocol = MagicEnvelopeProtocol() self.envelope = MagicEnvelopeProtocol().ToXmlString(Envelope(self.env_message,'application/atom+xml',self.env_signature),fulldoc=False)
res2 = aes_helper.get_random_key() if res and res != res2: print " - Success" success += 1 else: print " - Fail" fail +=1 except: print " - Epic fail" fail +=1 if test_aes: print "Encrypting message using aes_helper..." try: plain = "Testing encryption" cipher = aes_helper.encrypt(plain,res) if cipher and cipher != plain: print " - Success" success += 1 else: print " - Fail" fail +=1 except: print " - Epic fail" fail +=1 if test_aes: print "Decrypting message using aes_helper..." try: new_plain = aes_helper.decrypt(cipher,res) if new_plain.strip() == plain.strip():