【Python】加密/解密

"""
-*- coding:utf-8 -*-
@Time   :2020/11/4 19:02
@Author :
@File   :jar_encryption_util.py
@Version:1.0
"""


class JarEncryptionUtil:
    @staticmethod
    def str_encryption_ascii(str_1: str, salt=0):
        """
        字符串转ASCII码
        :param str_1: 字符串
        :param salt: 盐
        :return: ASCII码列表
        """
        ascii_list = []
        for i in range(len(str_1)):
            # 字符串 —> ASCII
            ascii_list.append(ord(str_1[i]) + salt)
        return ascii_list

    @staticmethod
    def ascii_encryption_str(ascii_1: list, salt=0):
        """
        ASCII码转字符串
        :param ascii_1: ASCII列表
        :param salt: 盐
        :return: 字符串
        """
        result = ''
        for i in range(len(ascii_1)):
            # ASCII —> 字符串
            result += chr(ascii_1[i] - salt)
        return result


if __name__ == '__main__':
    # 加密字符串
    str1 = '测,,@aA试测试测试'
    #
    salt = 12345

    # 加密
    print('加密后:{}'.format(JarEncryptionUtil.str_encryption_ascii(str1, salt)))

    # 解密
    ascii_1 = [40324, 12389, 77637, 12409, 12442, 12410, 48142, 40324, 48142, 40324, 48142]
    print('解密后:{}'.format(JarEncryptionUtil.ascii_encryption_str(ascii_1, salt)))

原文地址:https://www.cnblogs.com/danhuai/p/13940645.html