Uncategorized

AES Between Python, Javascript(NodeJS) and PHP

AES

Wikipedia: https://en.wikipedia.org/wiki/Advanced_Encryption_Standard. AES needs Key, IV and PKCS.

  • Use 256 bits key length, Python can auto choice by key length your passed. Named MCRYPT_RIJNDAEL_128 in PHP5, Nodejs/PHP7.1 is aes-256-cbc.
  • Use CBC mode due to ECB no IV.
  • Key is AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA (256 bits),IV is AAAAAAAAAAAAAAAA(128bits).
  • Python use pycrypto(pip install pycrypto),NodeJS require crypto(npm install crypto),PHP5 need mcrypt.

Python

Javascript(NodeJS)

PHP

Testing

Environment:
Ubuntu 14.04.2, Python 2.7.6, NodeJS v0.10.25, PHP 5.5.9

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
Saran Kumar P
Guest
Saran Kumar P

Excellent post, I was searched 4 days for this solution. I have found one drawback though. unpad is missing in the python function.

def unpad(s): return s[:-ord(s[len(s) – 1:])]

Small modification in the main function.

if __name__ == ‘__main__’:
encrypted = base64.b64encode(encrypt_token(‘TestingPython’))
encrypted = encrypted.decode(‘utf-8’)
print(‘Python encrypt: ‘ , encrypted )
print(‘Python decrypt: ‘ , unpad(decrypt_token(base64.b64decode(encrypted)).decode(‘utf-8’)))