开发者社区> 问答> 正文

在PHP或NodeJS中实现Python编码代码

我试图实现基于gzip和crypto库的python入门代码,但我不知道php或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)

展开
收起
一码平川MACHEL 2019-01-16 17:22:00 4318 0
1 条回答
写回答
取消 提交回答
  • Node.js有很好的模块可以处理压缩和加密。

    加密的密码相关的操作。它支持许多算法来加密/散列东西
    Zlib用于压缩。支持gzip,deflate,zip等算法......
    文档还包括有关如何使用这些模块的示例,因此请务必检查它们

    2019-07-17 23:25:41
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载