Implementation of a Python encription code in PHP or NodeJS

-3

I am trying to implement a python encription code based on gzip and crypto library but i dont know the equivalent modules or functions in either php or node js

from Crypto.Cipher import DES, AES
def enclen(p):
   return ((16 - p % 16) & 0xF) + p + 4

def rsb(a, b):
return (0 - a + b) & 0xffffffff

def ands(a, b):
   return (a & b) & 0xffffffff


def _ror(val, bits, bit_size):
    return ((val & (2 ** bit_size - 1)) >> bits % bit_size) | \
           (val << (bit_size - (bits % bit_size)) & (2 ** bit_size - 1))


def __ROR4__(a, b):
    return _ror(a, b, 32)


def eor(a, b):
   return (a ^ b) & 0xffffffff


class Crypto(object):
    def __init__(self):
        self.key = b'!*ss!_defaul%t54'
       self.kl = 0x10
        self.sbox0 = bytes.fromhex(
        '637C777BF26B6FC53001672BFED7AB76CA82C97DFA5947F0ADD4A2AF9CA472'
        'C0B7FD9326363FF7CC34A5E5F171D8311504C723C31896059A071280E2EB27'
        'B27509832C1A1B6E5AA0523BD6B329E32F8453D100ED20FCB15B6ACBBE394A'
        '4C58CFD0EFAAFB434D338545F9027F503C9FA851A3408F929D38F5BCB6DA21'
        '10FFF3D2CD0C13EC5F974417C4A77E3D645D197360814FDC222A908846EEB8'
        '14DE5E0BDBE0323A0A4906245CC2D3AC629195E479E7C8376D8DD54EA96C56'
        'F4EA657AAE08BA78252E1CA6B4C6E8DD741F4BBD8B8A703EB5664803F60E61'
        '3557B986C11D9EE1F8981169D98E949B1E87E9CE5528DF8CA1890DBFE64268'
        '41992D0FB054BB16')
    self.plen = 0

def crypt(self, payload):
    if not isinstance(payload, bytes):
        payload = payload.encode('utf8')
    self.plen = len(payload)
    i = 0
    r = [00] * 16
    while i < self.kl:
        a = self.key[i]
        r[i] = self.sbox0[a]
        i += 1

    t = int.from_bytes(r[:4], 'big')
    r[:4] = int.to_bytes(t, 4, 'little')
    t = int.from_bytes(r[4:8], 'big')
    r[4:8] = int.to_bytes(t, 4, 'little')
    t = int.from_bytes(r[8:12], 'big')
    r[8:12] = int.to_bytes(t, 4, 'little')
    t = int.from_bytes(r[12:16], 'big')
    r[12:16] = int.to_bytes(t, 4, 'little')
    b = rsb(self.plen, 0)
    b = ands(b, 0xf)
    c = b + self.plen + 4

    result = [00] * enclen(self.plen)
    result[0] = 0x74
    result[1] = 0x63
    result[2] = 0x02
    result[3] = b
    result[4:len(payload) + 4] = payload

    i = 4
    while i != c:
        a = result[i]
        result[i] = self.sbox0[a]
        i += 1

    a = c - 4
    b = 0
    a = a >> 4
    d = 4
    while b < a:
        c = int.from_bytes(result[d:d + 4], 'big')
        e = int.from_bytes(r[:4], 'little')
        c ^= e
        result[d:d + 4] = int.to_bytes(c, 4, 'big')
        c = int.from_bytes(result[d + 4:d + 8], 'big')
        e = int.from_bytes(r[4:8], 'little')
        c = eor(e, __ROR4__(c, 24))
        result[d + 4:d + 8] = int.to_bytes(c, 4, 'big')
        c = int.from_bytes(result[d + 8:d + 12], 'big')
        e = int.from_bytes(r[8:12], 'little')
        c = eor(e, __ROR4__(c, 16))
        result[d + 8:d + 12] = int.to_bytes(c, 4, 'big')
        c = int.from_bytes(result[d + 12:d + 16], 'big')
        e = int.from_bytes(r[12:16], 'little')
        c = eor(e, __ROR4__(c, 8))
        result[d + 12:d + 16] = int.to_bytes(c, 4, 'big')
        b += 1
        d += 0x10
    return bytes(result)

I dont have much idea what really goes behind this code some byte size operations but i dont know what exactly happens

Either nodejs or php equivalent of this code or any idea to how to implement it in those lang or if those languages are supported this kind of operations I dont know

php
python
node.js

1 Answer

0

Node.js has really good modules that can handle compression and encryption.

  • Crypto for cryptography related operations. It supports many algorithms to encrypt/hash stuff
  • Zlib for compression. Supports algorithms like gzip, deflate, zip, etc...

The docs also include examples on how to use these modules, so make sure to check them

answered on Stack Overflow Jan 16, 2019 by molamk

User contributions licensed under CC BY-SA 3.0