From 5a47adb56bfe80c34ed5b04a4824e517a3cde16d Mon Sep 17 00:00:00 2001 From: Devendra Date: Tue, 26 Feb 2013 01:02:44 +0530 Subject: fixiing encryption to interact with php app --- PubnubBase.py | 4 +++- PubnubCrypto.py | 13 +++++++------ python-tornado/Pubnub.py | 1 + 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/PubnubBase.py b/PubnubBase.py index 074b891..07dd27f 100644 --- a/PubnubBase.py +++ b/PubnubBase.py @@ -16,6 +16,7 @@ import time import hashlib import urllib2 import uuid +from PubnubCrypto import PubnubCrypto class PubnubBase(object): def __init__( @@ -95,7 +96,7 @@ class PubnubBase(object): out.append(outdict) message = json.dumps(out[0]) else: - message = json.dumps(pc.encrypt(self.cipher_key, message).replace('\n','')) + message = json.dumps(pc.encrypt(self.cipher_key, json.dumps(message)).replace('\n','')) else : message = json.dumps(message) @@ -103,6 +104,7 @@ class PubnubBase(object): def decrypt(self, message): if self.cipher_key: + pc = PubnubCrypto() if type( message ) == type(list()): for item in message: encryptItem = pc.decrypt(self.cipher_key, item ) diff --git a/PubnubCrypto.py b/PubnubCrypto.py index 744f2d3..b7a57a1 100644 --- a/PubnubCrypto.py +++ b/PubnubCrypto.py @@ -38,7 +38,8 @@ class PubnubCrypto() : #* @return msg with padding. #** """ - return msg + ((block_size - len(msg) % block_size) * chr(block_size - len(msg) % block_size)) + padding = block_size - (len(msg) % block_size) + return msg + chr(padding)*padding def depad( self, msg ): """ @@ -60,7 +61,7 @@ class PubnubCrypto() : #* @return key in MD5 format #** """ - return MD5.new(key).digest() + return hashlib.sha256(key).hexdigest() def encrypt( self, key, msg ): """ @@ -73,9 +74,9 @@ class PubnubCrypto() : """ secret = self.getSecret(key) Initial16bytes='0123456789012345' - cipher = AES.new(secret,AES.MODE_CBC,Initial16bytes) - return encodestring(cipher.encrypt(self.pad(msg))) - + cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) + enc = encodestring(cipher.encrypt(self.pad(msg))) + return enc def decrypt( self, key, msg ): """ #** @@ -87,6 +88,6 @@ class PubnubCrypto() : """ secret = self.getSecret(key) Initial16bytes='0123456789012345' - cipher = AES.new(secret,AES.MODE_CBC,Initial16bytes) + cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) return self.depad((cipher.decrypt(decodestring(msg)))) diff --git a/python-tornado/Pubnub.py b/python-tornado/Pubnub.py index 880e396..eba2a0f 100644 --- a/python-tornado/Pubnub.py +++ b/python-tornado/Pubnub.py @@ -51,6 +51,7 @@ class Pubnub(PubnubCoreAsync): publish_key=publish_key, subscribe_key=subscribe_key, secret_key=secret_key, + cipher_key=cipher_key, ssl_on=ssl_on, origin=origin, ) -- cgit v1.2.3