Huawei Modem/Router sending encrypted requests
This relates to my Huawei python API here.
I'm adding a dedicated post on this as I expect it may be useful for others.
This took me quite a while to get working and I couldn't find any working solutions online.
This implements the javascript RSA encryption used for encrypting requests to be sent to the modem's underlying API in python.
The javascript code is:
The python code I got working is:
RSAE and RSAN tokens are returned by the router on a successful login.
The content type on the encrypted call also needs to be:
I'm adding a dedicated post on this as I expect it may be useful for others.
This took me quite a while to get working and I couldn't find any working solutions online.
This implements the javascript RSA encryption used for encrypting requests to be sent to the modem's underlying API in python.
The javascript code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 | function RSAEncrypt(text) { var m = pkcs1pad2(text,( this .n.bitLength()+7)>>3); if (m == null ) return null ; var c = this .doPublic(m); if (c == null ) return null ; var h = c.toString(16); if ((h.length & 1) == 0) return h; else return "0" + h; } |
The python code I got working is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import uuid import hashlib import hmac from binascii import hexlify import math import base64 from Crypto.Cipher import PKCS1_v1_5 from Crypto.PublicKey.RSA import construct def rsa_encrypt(rsae, rsan, data): if (data is None or data = = ' '): return ' ' N = long (rsan, 16 ) E = long (rsae, 16 ) b64data = base64.b64encode(data) pubkey = construct((N, E)) cipher = PKCS1_v1_5.new(pubkey) blocks = int (math.ceil( len (b64data) / 245.0 )) result = [] for i in range (blocks): block = b64data[i * 245 :(i + 1 ) * 245 ] d = cipher.encrypt(block) result.append(d) result = hexlify(''.join(result)) if (( len (result) & 1 ) = = 0 ): return result else : return '0' + result |
RSAE and RSAN tokens are returned by the router on a successful login.
The content type on the encrypted call also needs to be:
headers['Content-type'] = 'application/x-www-form-urlencoded; charset=UTF-8;enc'
Comments
Post a Comment