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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大