Skip to content

Commit 2972d33

Browse files
committed
feat: ✨ 更新社会主义核心价值观编码实现方式
1 parent 7c243fc commit 2972d33

File tree

3 files changed

+132
-11
lines changed

3 files changed

+132
-11
lines changed

qsnctf/cvecodepy.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import re
2+
import random
3+
import argparse
4+
from urllib.parse import quote, unquote
5+
6+
VALUES = '富强民主文明和谐自由平等公正法治爱国敬业诚信友善'
7+
8+
9+
def str2utf8(s: str) -> str:
10+
pattern = re.compile(r'[A-Za-z0-9\-_.!~*\'()]')
11+
12+
def replace(match):
13+
return f'{ord(match.group()):02X}'
14+
15+
encoded = pattern.sub(replace, s)
16+
return quote(encoded, safe='').upper().replace('%', '')
17+
18+
19+
def utf82str(hex_str: str) -> str:
20+
if len(hex_str) % 2 != 0:
21+
raise ValueError('Invalid hex length')
22+
23+
percent_encoded = '%'.join([hex_str[i:i + 2] for i in range(0, len(hex_str), 2)])
24+
return unquote(f'%{percent_encoded}')
25+
26+
27+
def hex2duo(hex_str: str) -> list:
28+
duo = []
29+
for c in hex_str:
30+
n = int(c, 16)
31+
if n < 10:
32+
duo.append(n)
33+
else:
34+
if random.choice([True, False]):
35+
duo.extend([10, n - 10])
36+
else:
37+
duo.extend([11, n - 6])
38+
return duo
39+
40+
41+
def duo2hex(duo: list) -> str:
42+
hex_digits = []
43+
i = 0
44+
while i < len(duo):
45+
if duo[i] < 10:
46+
hex_digits.append(str(duo[i]))
47+
i += 1
48+
else:
49+
if duo[i] == 10:
50+
hex_digits.append(f'{duo[i + 1] + 10:X}')
51+
else:
52+
hex_digits.append(f'{duo[i + 1] + 6:X}')
53+
i += 2
54+
return ''.join(hex_digits)
55+
56+
57+
def encode(text: str) -> str:
58+
hex_str = str2utf8(text)
59+
duo = hex2duo(hex_str)
60+
return ''.join([VALUES[2 * d] + VALUES[2 * d + 1] for d in duo])
61+
62+
63+
def decode(encoded: str) -> str:
64+
duo = []
65+
for c in encoded:
66+
idx = VALUES.find(c)
67+
if idx != -1 and idx % 2 == 0:
68+
duo.append(idx // 2)
69+
hex_str = duo2hex(duo)
70+
return utf82str(hex_str)
71+
72+
73+
def interactive_mode():
74+
modes = {
75+
'1': ('加密', encode),
76+
'2': ('解密', decode),
77+
}
78+
79+
while True:
80+
print('\n=== 社会主义核心价值观编解码 ===')
81+
print('1. 加密\n2. 解密\n3. 退出')
82+
choice = input('请选择操作: ').strip()
83+
84+
if choice == '3':
85+
break
86+
87+
if choice not in modes:
88+
print('无效选择')
89+
continue
90+
91+
mode_name, func = modes[choice]
92+
93+
text = input(f'输入待{mode_name}文本: ')
94+
try:
95+
result = func(text)
96+
print(f'\n{mode_name}结果:\n{result}')
97+
except Exception as e:
98+
print(f'错误: {e}')
99+
100+
101+
if __name__ == '__main__':
102+
parser = argparse.ArgumentParser(description='社会主义核心价值观编解码工具')
103+
parser.add_argument('-m', '--mode', choices=['encode', 'decode'], help='编码或解码模式')
104+
parser.add_argument('-t', '--text', help='直接处理的文本')
105+
parser.add_argument('-o', '--output', help='输出文件路径')
106+
107+
args = parser.parse_args()
108+
109+
if args.text:
110+
try:
111+
if args.mode == 'encode':
112+
result = encode(args.text)
113+
elif args.mode == 'decode':
114+
result = decode(args.text)
115+
else:
116+
raise ValueError('请指定模式参数 --mode')
117+
118+
if args.output:
119+
with open(args.output, 'w', encoding='utf-8') as f:
120+
f.write(result)
121+
print(f'结果已保存至 {args.output}')
122+
else:
123+
print(result)
124+
except Exception as e:
125+
print(f'处理错误: {e}')
126+
else:
127+
interactive_mode()

qsnctf/misc.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import uuid
1717
from qsnctf.auxiliary import read_file_to_list, is_http_or_https_url, normalize_url
1818
import rarfile
19+
from qsnctf.cvecodepy import encode,decode
1920

2021

2122
def get_uuid():
@@ -24,18 +25,12 @@ def get_uuid():
2425

2526
def Chinese_socialism_encode(string):
2627
# 社会主义核心价值观编码
27-
package_path = os.path.abspath(os.path.dirname(__file__))
28-
file_path = os.path.join(package_path, 'plugin', 'js', 'cvencode.js')
29-
content = execjs.compile(js_from_file(file_path))
30-
return str(content.call('encode', string))
28+
return encode(string)
3129

3230

3331
def Chinese_socialism_decode(string):
3432
# 社会主义核心价值观解码
35-
package_path = os.path.abspath(os.path.dirname(__file__))
36-
file_path = os.path.join(package_path, 'plugin', 'js', 'cvencode.js')
37-
content = execjs.compile(js_from_file(file_path))
38-
return str(content.call('decode', string))
33+
return decode(string)
3934

4035

4136
def string_reverse(string):

test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from qsnctf import *
22

3-
a = DomainScan("qsnctf.com", threadline=100, echo=True)
4-
print(a.results_title)
5-
print(a.results)
3+
a = Chinese_socialism_encode("1234")
4+
print(a)

0 commit comments

Comments
 (0)