编程语言
首页 > 编程语言> > Python 实现RC4流密码

Python 实现RC4流密码

作者:互联网

RC4流密码
Python3.7


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   rc4.py    
@Contact :   nickdlk@outlook.com

@Modify Time            @Author    @Version    @Desciption
------------            -------    --------    -----------
2021/3/28 17:14         Nick      1.0         RC4流密码

'''

import  hashlib,base64


class rc4:
    def __init__(self, public_key=None):
        self.public_key = public_key or 'none_public_key'

    def encode(self, string):
        """
        加密
        :param string: 输入utf-8
        :return:
        """
        bData = [ord(i) for i in string]
        result = self.__docrypt(bData)
        result = base64.b64encode(result.encode())#编码成可见的字符
        return str(result,encoding='utf-8')

    def decode(self, string):
        """
        解密
        :param string:输入utf-8
        :return:
        """
        message = base64.b64decode(string).decode()
        bData = [ord(i) for i in message]
        deresult = self.__docrypt(bData)
        return deresult

    def __rc4_init(self, K):
        """
        :param K: 密钥
        :return:
        """
        j = 0
        K = hashlib.md5(K.encode("utf8")).hexdigest()
        k = []  # 临时数组
        SBox = []  # S盒
        for i in range(256):
            SBox.append(i)
            k.append(K[i % len(K)])  # T[i] = K[ i mod keylen ];
        for i in range(256):
            j = (j + SBox[i] + ord(k[i])) % 256
            SBox[i], SBox[j] = SBox[j], SBox[i]  # 交换S[i],S[j]
        return SBox

    def __docrypt(self,string):
        i = j = 0
        result = ''
        SBox = self.__rc4_init(self.public_key)
        for _str in string:
            i = (i + 1) % 256
            j = (j + SBox[i]) % 256
            SBox[i], SBox[j] = SBox[j], SBox[i]
            k = chr(_str ^ SBox[(SBox[i] + SBox[j]) % 256])
            result += k
        return result


_rc4 = rc4('testkey')
tempcode = _rc4.encode('HelloWorldNick')
print("加密结果",tempcode)
tempdecode = _rc4.decode(tempcode)
print("解密结果",tempdecode)
#加密结果 wqsgMcKIw4DDt8OIQMO2ahTCgMOocQ==
#解密结果 HelloWorldNick

标签:return,string,SBox,密码,Python,self,rc4,RC4,result
来源: https://blog.csdn.net/weixin_43924621/article/details/115285578