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
目录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实现解析markdown文档中的图片,并且保存到本地~
背景 前阵子简书好像说是凉了,搞得我有点小慌,毕竟我的大部分博客都是放在简书上面的,虽然简书提供了打包导出功能,但是只能导出文字,图片的话还是存在简书服务器上面,再加上我一直想要重新做一个个人博客,于是就有了这篇文章。 思路 首先是要解析markdown文档,然后获取到其中的所有图片,再把图片按md文件分好目录保存。 解析markdown文档 这里我用了misaka模块,据说是python的mar
2021-12-18 · Python · 约 1 分钟
Jupyter-Notebook-简单配置
安装主题 默认主题太丑了,必须换个好看的主题,pip安装: pip install jupyterthemes 装完之后: # 列出所有主题 jt -l # 设置主题 # -T 启用工具栏 # -N 启用名称栏 jt -t 主题名称 -T -N 自动补全 安装nbextensions pip install jupyter_contrib_nbextensions jupyter contrib
2021-12-18 · Python · 约 1 分钟
配置pip国内镜像加快python第三方库安装速度~
现在这网络对于开发者来说有点不太友好,下载什么东西都慢慢,pip install慢,git clone也慢,难受…… 不过还好有各种代替的国内镜像源可以加速 临时配置 在使用pip的时候加参数-i https://pypi.tuna.tsinghua.edu.cn/simple 例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple d
2022-11-12 · Python · 约 1 分钟
Python实现给图片加水印功能
前言 最近忙得连轴转,很久没更新博客了,代码倒是没啥写,积累了好些东西,接下来一有时间就来更新吧~ 本文记录使用Python实现给图片添加水印的功能实现过程 先看效果 把公众号的封面作为素材 原图是这样的 codelab 加个水印的效果 temp 实现代码 主要实现是参考GitHub上一个项目的:https://github.com/2Dou/watermarker 用到了Pillow库,使用前请
2022-09-29 · Python · 约 1 分钟
讨论
共 0 条加载评论中…