返回文章列表文章索引阅读全文
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中,和部分高级语言一样,使用了try/except/finally语句块来处理异常,如果你有其他编程语言的经验,实践起来并不难。 Python 中(至少)有两种错误:语法错误和异常( syntax errors 和 exceptions )。下面稍微介

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

Python-缓存机制与-functools-lru_cache

缓存是一种将定量数据加以保存以备迎合后续获取需求的处理方式,旨在加快数据获取的速度。数据的生成过程可能需要经过计算,规整,远程获取等操作,如果是同一份数据需要多次使用,每次都重新生成会大大浪费时间。所以,如果将计算或者远程请求等操作获得的数据缓存下来,会加快后续的数据获取需求。 先来一个简单的例子以了解缓存机制的概念: # -*- coding: utf-8 -*- import random

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

Python实现给图片加水印功能

前言 最近忙得连轴转,很久没更新博客了,代码倒是没啥写,积累了好些东西,接下来一有时间就来更新吧~ 本文记录使用Python实现给图片添加水印的功能实现过程 先看效果 把公众号的封面作为素材 原图是这样的 codelab 加个水印的效果 temp 实现代码 主要实现是参考GitHub上一个项目的:https://github.com/2Dou/watermarker 用到了Pillow库,使用前请

2022-09-29 · Python · 约 1 分钟

讨论

共 0 条

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

加载评论中…