doushou8730 2019-01-16 00:44
浏览 107

在PHP或NodeJS中实现Python加密代码

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

  • 写回答

1条回答 默认 最新

  • douzai3399 2019-01-16 01:04
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B