曦远 (DealiAxy)一份私人的文字刊物

StarBlog
返回文章列表折页手记阅读全文
Python

Hand-Written Core Socialist Values Encoding Tool - Python Implementation

Preface Today is May Day, Labor Day. Happy holiday to all proletarian workers! Then let's have some fun and share something interesting~ This little tool is something I made for fun in college. For all you successors, the 12-word Core Socialist Values are something everyone is very familiar with. This tool can encode a piece of text into Core Socialist Values for encryption, and can also decrypt the ciphertext. Why did I make this? It's been too long, I don't quite remember, ~~seems like it was because trash-talking teammates in games kept getting censored, so I had a flash of inspiration: if I replaced it with harmonious text, it wouldn't get censored, right (jk~~ Implementation Principle Simple

曦远 (DealiAxy)约 11 分钟
目录5 个章节

Preface

Today is May Day, Labor Day. Happy holiday to all proletarian workers!

Then let's have some fun and share something interesting~

This little tool is something I made for fun in college. For all you successors, the 12-word Core Socialist Values are something everyone is very familiar with. This tool can encode a piece of text into Core Socialist Values for encryption, and can also decrypt the ciphertext.

Why did I make this? It's been too long, I don't quite remember, seems like it was because trash-talking teammates in games kept getting censored, so I had a flash of inspiration: if I replaced it with harmonious text, it wouldn't get censored, right (jk

Implementation Principle

Briefly explaining the principle—it's quite simple. Our text is represented as ASCII in computers. To a computer, all data is 0s and 1s, i.e. binary. The "Core Socialist Values" we're familiar with have 12 words, and each word can represent one digit, so we can define a new encoding: a base-12 encoding made of "values"~

We just need to convert binary ASCII characters into base-12 "values" encoding.

Let's Start Coding

Once the principle is clear, we can start writing code. This article uses Python to implement it (I may try implementations in other languages later)

Here we use Python's standard library binascii to handle ASCII encoding. First, import it.

Converting ASCII to binary can be done with the binascii standard library; converting to base-12 we'll have to implement ourselves.

(At the time) considering that converting binary directly to base-12 is rather troublesome, I compromised: first convert binary to decimal, then convert to base-12.

The code for converting decimal to base-12 is as follows

def from_num(input_num: int, b: int) -> int:
    """
    将数字转换为指定进制
    :param input_num: 输入的数字
    :param b: 进制
    :return: 输出结果
    """
    return ((input_num == 0) and "0") or \
           (from_num(input_num // b, b).lstrip("0") + "0123456789abcdefghijklmnopqrstuvwxyz"[input_num % b])

With the prerequisites in place, let's start writing the encryption code

def encode(raw: str) -> str:
    """
    编码
    :param raw: 输入的原始字符串
    :return: 返回编码结果
    """
    result = list()
    for char in raw:
        # 把字符转换为16进制字符串
        str_16 = binascii.b2a_hex(char.encode('utf-8'))
        # 把16进制字符串转换为10进制数字
        num_10 = int(str_16, 16)
        str_12 = str(from_num(num_10, 12))
        result.append(char_replace(str_12))

    return ','.join(result)

The code is very simple; everything is clearly written in the comments.

Then comes decryption—just reverse the encryption process.

def decode(encoded: str) -> str:
    """
    解码
    :param encoded: 已编码字符串
    :return: 解码的原始字符串
    """
    str_list = encoded.split(',')
    result = list()
    for char_list in str_list:
        # 整个12进制字符串
        char_12 = ''
        for char in char_list.split(' '):
            # 还原出12进制字符串
            str_12 = str(total_list.index(char))
            if str_12 == '10':
                str_12 = 'a'
            if str_12 == '11':
                str_12 = 'b'
            char_12 += str_12
        # 12进制串转换为10进制数
        num_10 = int(char_12, 12)
        str_16 = hex(num_10)

        result.append(binascii.a2b_hex(str_16[2:]).decode('utf-8'))
    return ''.join(result)

Testing the Results

The code is done, let's test how well it works

print(encode('劳动节快乐'))

Result

平等 富强 平等 平等 法治 富强 和谐,平等 富强 平等 平等 公正 友善 自由,平等 民主 文明 友善 自由 富强 文明,平等 富强 公正 民主 平等 文明 和谐,平等 富强 文明 诚信 法治 文明 爱国

A bit long, hahaha, but it's fine for fun

Let's try decrypting

print(decode('平等 富强 平等 平等 法治 富强 和谐,平等 富强 平等 平等 公正 友善 自由,平等 民主 文明 友善 自由 富强 文明,平等 富强 公正 民主 平等 文明 和谐,平等 富强 文明 诚信 法治 文明 爱国'))

We can correctly get the original text

劳动节快乐

Online Demo

I quickly put together a page. If you're interested, feel free to play around with it: http://dc.sblt.deali.cn:9800/core-socialist-values/

PS: Even better if you post the encrypted result in the comments

_EOF_

Hand-Written Core Socialist Values Encoding Tool - Python Implementation

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

自己动手开发简单消息队列(异步任务队列):Python实现

又是造轮子系列咯,Python上有很多成熟完善的异步任务队列框架可以用,比如Celery,或者RQ,不过这些都不自带消息队列服务,都需要使用Redis、RabbitMQ之类的消息队列才行,我用到小项目中又不需要附带这么多东西,于是自己动手来实现咯。 思路 将需要异步执行的任务添加到队列 自动从队列中取出任务,创建新线程执行 保存任务的执行结果和输出 任务完成,调用回调函数,处理返回的数据 使用输出

2021-12-18 · Python · 约 1 分钟

关于Python的异常处理

前言 异常处理在任何一门编程语言里都是值得关注的一个话题,良好的异常处理可以让你的程序更加健壮,清晰的错误信息更能帮助你快速修复问题。在Python中,和部分高级语言一样,使用了try/except/finally语句块来处理异常,如果你有其他编程语言的经验,实践起来并不难。 Python 中(至少)有两种错误:语法错误和异常( syntax errors 和 exceptions )。下面稍微介

2022-05-27 · Python · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…