From b8d1dd86a0d8c4261d4f3765f3ca227d7b555c84 Mon Sep 17 00:00:00 2001 From: Devendra Date: Tue, 25 Mar 2014 10:50:39 +0530 Subject: fixing encryption, and changing from urllib3 to urllib --- common/PubnubCrypto.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'common/PubnubCrypto.py') diff --git a/common/PubnubCrypto.py b/common/PubnubCrypto.py index df7cb8d..1f5fc8d 100644 --- a/common/PubnubCrypto.py +++ b/common/PubnubCrypto.py @@ -28,7 +28,7 @@ class PubnubCrypto() : #** """ padding = block_size - (len(msg) % block_size) - return msg + chr(padding)*padding + return msg + (chr(padding)*padding).encode('utf-8') def depad( self, msg ): """ @@ -50,7 +50,7 @@ class PubnubCrypto() : #* @return key in MD5 format #** """ - return hashlib.sha256(key).hexdigest() + return hashlib.sha256(key.encode("utf-8")).hexdigest() def encrypt( self, key, msg ): """ @@ -64,8 +64,7 @@ class PubnubCrypto() : secret = self.getSecret(key) Initial16bytes='0123456789012345' cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) - enc = encodestring(cipher.encrypt(self.pad(msg))) - return enc + return encodestring(cipher.encrypt(self.pad(msg.encode('utf-8')))).decode('utf-8') def decrypt( self, key, msg ): """ #** @@ -78,4 +77,4 @@ class PubnubCrypto() : secret = self.getSecret(key) Initial16bytes='0123456789012345' cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) - return self.depad((cipher.decrypt(decodestring(msg)))) + return (cipher.decrypt(decodestring(msg.encode('utf-8')))).decode('utf-8') -- cgit v1.2.3 From 9ac3ccf6283772b404a0c80945e3cdf3406ac5bf Mon Sep 17 00:00:00 2001 From: Devendra Date: Tue, 25 Mar 2014 11:47:03 +0530 Subject: making version 2 and version 3 work same time --- common/PubnubCrypto.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) (limited to 'common/PubnubCrypto.py') diff --git a/common/PubnubCrypto.py b/common/PubnubCrypto.py index 1f5fc8d..3489216 100644 --- a/common/PubnubCrypto.py +++ b/common/PubnubCrypto.py @@ -4,7 +4,84 @@ from base64 import encodestring, decodestring import hashlib import hmac -class PubnubCrypto() : +class PubnubCrypto2() : + """ + #** + #* PubnubCrypto + #* + #** + + ## Initiate Class + pc = PubnubCrypto + + """ + + def pad( self, msg, block_size=16 ): + """ + #** + #* pad + #* + #* pad the text to be encrypted + #* appends a padding character to the end of the String + #* until the string has block_size length + #* @return msg with padding. + #** + """ + padding = block_size - (len(msg) % block_size) + return msg + chr(padding)*padding + + def depad( self, msg ): + """ + #** + #* depad + #* + #* depad the decryptet message" + #* @return msg without padding. + #** + """ + return msg[0:-ord(msg[-1])] + + def getSecret( self, key ): + """ + #** + #* getSecret + #* + #* hases the key to MD5 + #* @return key in MD5 format + #** + """ + return hashlib.sha256(key).hexdigest() + + def encrypt( self, key, msg ): + """ + #** + #* encrypt + #* + #* encrypts the message + #* @return message in encrypted format + #** + """ + secret = self.getSecret(key) + Initial16bytes='0123456789012345' + cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) + enc = encodestring(cipher.encrypt(self.pad(msg))) + return enc + def decrypt( self, key, msg ): + """ + #** + #* decrypt + #* + #* decrypts the message + #* @return message in decryped format + #** + """ + secret = self.getSecret(key) + Initial16bytes='0123456789012345' + cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) + return self.depad((cipher.decrypt(decodestring(msg)))) + + +class PubnubCrypto3() : """ #** #* PubnubCrypto -- cgit v1.2.3 From 09cd0c015ae276aa849297a6a976065b2b3f247b Mon Sep 17 00:00:00 2001 From: Devendra Date: Wed, 23 Apr 2014 14:03:13 +0530 Subject: modifying code for pep 8 compliance --- common/PubnubCrypto.py | 63 +++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 29 deletions(-) (limited to 'common/PubnubCrypto.py') diff --git a/common/PubnubCrypto.py b/common/PubnubCrypto.py index 3489216..295a76e 100644 --- a/common/PubnubCrypto.py +++ b/common/PubnubCrypto.py @@ -1,10 +1,11 @@ from Crypto.Cipher import AES from Crypto.Hash import MD5 -from base64 import encodestring, decodestring +from base64 import encodestring, decodestring import hashlib import hmac -class PubnubCrypto2() : + +class PubnubCrypto2(): """ #** #* PubnubCrypto @@ -15,8 +16,8 @@ class PubnubCrypto2() : pc = PubnubCrypto """ - - def pad( self, msg, block_size=16 ): + + def pad(self, msg, block_size=16): """ #** #* pad @@ -28,9 +29,9 @@ class PubnubCrypto2() : #** """ padding = block_size - (len(msg) % block_size) - return msg + chr(padding)*padding - - def depad( self, msg ): + return msg + chr(padding) * padding + + def depad(self, msg): """ #** #* depad @@ -41,7 +42,7 @@ class PubnubCrypto2() : """ return msg[0:-ord(msg[-1])] - def getSecret( self, key ): + def getSecret(self, key): """ #** #* getSecret @@ -52,7 +53,7 @@ class PubnubCrypto2() : """ return hashlib.sha256(key).hexdigest() - def encrypt( self, key, msg ): + def encrypt(self, key, msg): """ #** #* encrypt @@ -62,11 +63,12 @@ class PubnubCrypto2() : #** """ secret = self.getSecret(key) - Initial16bytes='0123456789012345' - cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) + Initial16bytes = '0123456789012345' + cipher = AES.new(secret[0:32], AES.MODE_CBC, Initial16bytes) enc = encodestring(cipher.encrypt(self.pad(msg))) return enc - def decrypt( self, key, msg ): + + def decrypt(self, key, msg): """ #** #* decrypt @@ -76,12 +78,12 @@ class PubnubCrypto2() : #** """ secret = self.getSecret(key) - Initial16bytes='0123456789012345' - cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) + Initial16bytes = '0123456789012345' + cipher = AES.new(secret[0:32], AES.MODE_CBC, Initial16bytes) return self.depad((cipher.decrypt(decodestring(msg)))) -class PubnubCrypto3() : +class PubnubCrypto3(): """ #** #* PubnubCrypto @@ -92,8 +94,8 @@ class PubnubCrypto3() : pc = PubnubCrypto """ - - def pad( self, msg, block_size=16 ): + + def pad(self, msg, block_size=16): """ #** #* pad @@ -105,9 +107,9 @@ class PubnubCrypto3() : #** """ padding = block_size - (len(msg) % block_size) - return msg + (chr(padding)*padding).encode('utf-8') - - def depad( self, msg ): + return msg + (chr(padding) * padding).encode('utf-8') + + def depad(self, msg): """ #** #* depad @@ -118,7 +120,7 @@ class PubnubCrypto3() : """ return msg[0:-ord(msg[-1])] - def getSecret( self, key ): + def getSecret(self, key): """ #** #* getSecret @@ -129,7 +131,7 @@ class PubnubCrypto3() : """ return hashlib.sha256(key.encode("utf-8")).hexdigest() - def encrypt( self, key, msg ): + def encrypt(self, key, msg): """ #** #* encrypt @@ -139,10 +141,12 @@ class PubnubCrypto3() : #** """ secret = self.getSecret(key) - Initial16bytes='0123456789012345' - cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) - return encodestring(cipher.encrypt(self.pad(msg.encode('utf-8')))).decode('utf-8') - def decrypt( self, key, msg ): + Initial16bytes = '0123456789012345' + cipher = AES.new(secret[0:32], AES.MODE_CBC, Initial16bytes) + return encodestring( + cipher.encrypt(self.pad(msg.encode('utf-8')))).decode('utf-8') + + def decrypt(self, key, msg): """ #** #* decrypt @@ -152,6 +156,7 @@ class PubnubCrypto3() : #** """ secret = self.getSecret(key) - Initial16bytes='0123456789012345' - cipher = AES.new(secret[0:32],AES.MODE_CBC,Initial16bytes) - return (cipher.decrypt(decodestring(msg.encode('utf-8')))).decode('utf-8') + Initial16bytes = '0123456789012345' + cipher = AES.new(secret[0:32], AES.MODE_CBC, Initial16bytes) + return (cipher.decrypt( + decodestring(msg.encode('utf-8')))).decode('utf-8') -- cgit v1.2.3 From 98242257846e9276dd83adbea950f83e25a4f2b6 Mon Sep 17 00:00:00 2001 From: Devendra Date: Thu, 24 Apr 2014 02:07:56 +0530 Subject: pep8 compliance, removing redundant files --- common/PubnubCrypto.py | 162 ------------------------------------------------- 1 file changed, 162 deletions(-) delete mode 100644 common/PubnubCrypto.py (limited to 'common/PubnubCrypto.py') diff --git a/common/PubnubCrypto.py b/common/PubnubCrypto.py deleted file mode 100644 index 295a76e..0000000 --- a/common/PubnubCrypto.py +++ /dev/null @@ -1,162 +0,0 @@ -from Crypto.Cipher import AES -from Crypto.Hash import MD5 -from base64 import encodestring, decodestring -import hashlib -import hmac - - -class PubnubCrypto2(): - """ - #** - #* PubnubCrypto - #* - #** - - ## Initiate Class - pc = PubnubCrypto - - """ - - def pad(self, msg, block_size=16): - """ - #** - #* pad - #* - #* pad the text to be encrypted - #* appends a padding character to the end of the String - #* until the string has block_size length - #* @return msg with padding. - #** - """ - padding = block_size - (len(msg) % block_size) - return msg + chr(padding) * padding - - def depad(self, msg): - """ - #** - #* depad - #* - #* depad the decryptet message" - #* @return msg without padding. - #** - """ - return msg[0:-ord(msg[-1])] - - def getSecret(self, key): - """ - #** - #* getSecret - #* - #* hases the key to MD5 - #* @return key in MD5 format - #** - """ - return hashlib.sha256(key).hexdigest() - - def encrypt(self, key, msg): - """ - #** - #* encrypt - #* - #* encrypts the message - #* @return message in encrypted format - #** - """ - secret = self.getSecret(key) - Initial16bytes = '0123456789012345' - cipher = AES.new(secret[0:32], AES.MODE_CBC, Initial16bytes) - enc = encodestring(cipher.encrypt(self.pad(msg))) - return enc - - def decrypt(self, key, msg): - """ - #** - #* decrypt - #* - #* decrypts the message - #* @return message in decryped format - #** - """ - secret = self.getSecret(key) - Initial16bytes = '0123456789012345' - cipher = AES.new(secret[0:32], AES.MODE_CBC, Initial16bytes) - return self.depad((cipher.decrypt(decodestring(msg)))) - - -class PubnubCrypto3(): - """ - #** - #* PubnubCrypto - #* - #** - - ## Initiate Class - pc = PubnubCrypto - - """ - - def pad(self, msg, block_size=16): - """ - #** - #* pad - #* - #* pad the text to be encrypted - #* appends a padding character to the end of the String - #* until the string has block_size length - #* @return msg with padding. - #** - """ - padding = block_size - (len(msg) % block_size) - return msg + (chr(padding) * padding).encode('utf-8') - - def depad(self, msg): - """ - #** - #* depad - #* - #* depad the decryptet message" - #* @return msg without padding. - #** - """ - return msg[0:-ord(msg[-1])] - - def getSecret(self, key): - """ - #** - #* getSecret - #* - #* hases the key to MD5 - #* @return key in MD5 format - #** - """ - return hashlib.sha256(key.encode("utf-8")).hexdigest() - - def encrypt(self, key, msg): - """ - #** - #* encrypt - #* - #* encrypts the message - #* @return message in encrypted format - #** - """ - secret = self.getSecret(key) - Initial16bytes = '0123456789012345' - cipher = AES.new(secret[0:32], AES.MODE_CBC, Initial16bytes) - return encodestring( - cipher.encrypt(self.pad(msg.encode('utf-8')))).decode('utf-8') - - def decrypt(self, key, msg): - """ - #** - #* decrypt - #* - #* decrypts the message - #* @return message in decryped format - #** - """ - secret = self.getSecret(key) - Initial16bytes = '0123456789012345' - cipher = AES.new(secret[0:32], AES.MODE_CBC, Initial16bytes) - return (cipher.decrypt( - decodestring(msg.encode('utf-8')))).decode('utf-8') -- cgit v1.2.3