python3版Tea加解密

QQ的tea算法
找了挺久终于在github上找到了
原项目地址:https://github.com/ColasDAD/Py3QQTEA

import struct

def Hex2Bytes(hexstr:str):
    strBytes = hexstr.strip()
    pkt = bytes.fromhex(strBytes)
    return pkt

def Bytes2Hex(bin_: bytes):
    return ''.join(['%02X ' % b for b in bin_])

class QQ_TEA():
    """QQ TEA 加解密, 64比特明码, 128比特密钥
这是一个确认线程安全的独立加密模块,使用时必须要有一个全局变量secret_key,要求大于等于16位
    """
    def xor(self,a, b):
        op = 0xffffffff
        a1,a2 = struct.unpack(b'>LL', a[0:8])
        b1,b2 = struct.unpack(b'>LL', b[0:8])
        return struct.pack(b'>LL', ( a1 ^ b1) & op, ( a2 ^ b2) & op)
    
    def code(self,v, k):
        n=16
        op = 0xffffffff
        delta = 0x9e3779b9
        k = struct.unpack(b'>LLLL', k[0:16])
        y, z = struct.unpack(b'>LL', v[0:8])
        s = 0
        for i in range(n):
            s += delta
            y += (op &(z<<4))+ k[0] ^ z+ s ^ (op&(z>>5)) + k[1]
            y &= op
            z += (op &(y<<4))+ k[2] ^ y+ s ^ (op&(y>>5)) + k[3]
            z &= op
        r = struct.pack(b'>LL',y,z)
        return r

    def decipher(self,v, k):
        n = 16
        op = 0xffffffff
        y, z = struct.unpack(b'>LL', v[0:8])
        a, b, c, d = struct.unpack(b'>LLLL', k[0:16])
        delta = 0x9E3779B9
        s = (delta << 4)&op
        for i in range(n):
            z -= ((y<<4)+c) ^ (y+s) ^ ((y>>5) + d)
            z &= op
            y -= ((z<<4)+a) ^ (z+s) ^ ((z>>5) + b)
            y &= op
            s -= delta
            s &= op
        return struct.pack(b'>LL', y, z)

    def encrypt(self,v):
        END_CHAR = b'\0'
        FILL_N_OR = 0xF8
        vl = len(v)
        filln = (8-(vl+2))%8 + 2
        fills = b''
        for i in range(filln):
            fills = fills + bytes([220])
        v = ( bytes([(filln -2)|FILL_N_OR])
              + fills
              + v
              + END_CHAR * 7)
        tr = b'\0'*8
        to = b'\0'*8
        r = b''
        o = b'\0' * 8
        for i in range(0, len(v), 8):
            o = self.xor(v[i:i+8], tr)
            tr = self.xor( self.code(o, secret_key), to)
            to = o
            r += tr
        return r
    
    def decrypt(self,v):
        l = len(v)
        prePlain = self.decipher(v, secret_key)
        pos = (prePlain[0] & 0x07) +2
        r = prePlain
        preCrypt = v[0:8]
        for i in range(8, l, 8):
            x = self.xor(self.decipher(self.xor(v[i:i+8], prePlain),secret_key ), preCrypt)
            prePlain = self.xor(x, preCrypt)
            preCrypt = v[i:i+8]
            r += x
        if r[-7:] != b'\0'*7:
            return None
        return r[pos+1:-7]

if __name__ == '__main__':
    global secret_key
    secret_key = Hex2Bytes('11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11')
    print(Bytes2Hex(secret_key))
    
    QQ = QQ_TEA()
    
    plaintext = '哈哈哈hhh,'
    plaintext = bytes(plaintext,encoding = "utf-8")
    enc = QQ.encrypt(plaintext)
    print(Bytes2Hex(enc))
    dec = QQ.decrypt(enc)
    print(Bytes2Hex(dec))
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python实现3DES加密解密的代码示例如下: ```python import pyDes # 定义密钥,长度必须为8个字符(64位) key = b'12345678' # 定义初始化向量,长度为8个字符(64位) iv = b'abcdefgh' # 创建加密器 k = pyDes.triple_des(key, pyDes.CBC, iv, pad=None, padmode=pyDes.PAD_PKCS5) # 要加密的明文 data = b'Hello World' # 加密 cipher_text = k.encrypt(data) # 解密 plain_text = k.decrypt(cipher_text) print('加密前的明文:', data) print('加密后的密文:', cipher_text) print('解密后的明文:', plain_text) ``` 3DES的原理是:对明文进行三次加密,每次加密的密钥不同。加密时,首先使用第一个密钥对明文进行加密,然后使用第二个密钥对加密后的数据进行解密,最后再使用第三个密钥对解密后的数据进行加密。解密时,则按照相反的方式进行操作。 另外,cryptography库也提供了3DES的实现方式,代码示例如下: ```python from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import base64 key = b'8C7BD6A28C7BD6A28C7BD6A28C7BD6A28C7BD6A28C7BD6A2' # 48位 key = base64.b16decode(key) iv = b'3BF23BF23BF23BF2' # 16位 iv = base64.b16decode(iv) # 需要加密的内容 message = "a secret message" cipher = Cipher(algorithms.TripleDES(key), modes.CBC(iv)) # 加密 encryptor = cipher.encryptor() cipher_bytes = encryptor.update(message.encode()) encryptor.finalize() cipher_hex = cipher_bytes.hex().upper() # 解密 cipher_bytes = bytes.fromhex(cipher_hex) decryptor = cipher.decryptor() plain_text_bytes = decryptor.update(cipher_bytes) decryptor.finalize() plain_text_str = plain_text_bytes.decode() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值