[鹤城杯 2021]easy_crypto | 社会主义核心价值观编码 |

image

发现题目是社会主义核心价值观加密

社会主义核心价值观加解密python算法:

```python

import random
print("加密输入1\n解密输入2")
choice = int(input("Enter your choice: "))
if choice == 1:
    while 1:
        str1 = str(bytes(input('输入要编码的文本:'), encoding='u8'))[2:-1].replace('\\x', '')
        list1 = []
        for f1 in str1:
            str2 = int(f1, 16)
            if str2 < 10:
                list1.append(str2)
            elif random.random() < 0.5:
                list1.append(11)
                list1.append(str2 - 6)
            else:
                list1.append(10)
                list1.append(str2 - 10)
        str1 = '富强民主文明和谐自由平等公正法治爱国敬业诚信友善'
        str1 = ''.join([str1[f1 << 1] + str1[f1 << 1 | 1] for f1 in list1])
        print('编码结果:',str1,'\n'*3)




if choice == 2:
    while 1:
        str1 = input('输入要解码的文本:')
        list1 = ['富强', '民主', '文明', '和谐', '自由', '平等', '公正', '法治', '爱国', '敬业', '诚信', '友善']
        list1 = [list1.index(str1[f1 << 1] + str1[f1 << 1 | 1]) for f1 in range(int(len(str1)/2))]
        a = 0
        list2 = []
        while a < len(list1):
            if list1[a] < 10:
                list2.append(str(list1[a]))
            elif list1[a] == 10:
                a += 1
                list2.append(hex(list1[a]+10)[2:])
            else:
                a += 1
                list2.append(hex(list1[a]+6)[2:])
            a += 1
        str1 = bytes.fromhex(''.join(list2)).decode('utf-8')
        print('解码结果:', str1, '\n'*3)

image

posted @ 2025-11-10 21:12  Dragon_Roar  阅读(28)  评论(0)    收藏  举报