loraMac.py 文件源码

python
阅读 37 收藏 0 点赞 0 评论 0

项目:LoRa-server 作者: gzwsc2007 项目源码 文件源码
def cipherPayload(self, aesWithKey, frmPayloadStr, updown, devAddr, seqCnt):
        '''
        aesWithKey: a cipher object from CryptoPlus
        frmPayloadStr: | FRMPayload |
        updown: 0 for UP_LINK and 1 for DOWN_LINK
        devAddr (uint32): 4-byte device address
        seqCnt (uint32): frame count

        LoRaWAN Specification v1.0 Ch4.3.3.1
        '''
        paddedPaylod = self.padToBlockSize(frmPayloadStr)
        k = int(math.ceil(len(frmPayloadStr) / 16.0))
        A = bytearray([1, 0, 0, 0, 0, updown, devAddr & 0xFF,
                       (devAddr >> 8) & 0xFF,
                       (devAddr >> 16) & 0xFF,
                       (devAddr >> 24) & 0xFF,
                       seqCnt & 0xFF,
                       (seqCnt >> 8) & 0xFF,
                       (seqCnt >> 16) & 0xFF,
                       (seqCnt >> 24) & 0xFF,
                       0, 0])

        S = ''
        aesWithKey.final() # clear the cipher's cache
        for i in xrange(1, k+1):
            A[15] = i
            S += aesWithKey.encrypt(str(A))
        aesWithKey.final() # clear the cipher's cache

        dtype = numpy.dtype('<Q8')
        ciphered = numpy.bitwise_xor(numpy.fromstring(paddedPaylod,dtype=dtype),
                                   numpy.fromstring(S,dtype=dtype)).tostring()
        return ciphered[:len(frmPayloadStr)]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号