一种基于奇怪字符的文本/文件加密方式

一种基于奇怪字符的文本/文件加密方式

PlayMcBKuwu

(鹿目円香 Pro Max)

2024 年10 月 2 日 11:03

1

基本原理

base64 + AES 后替换文本。

例(egyptian.py):

输入要加密的文本: Python

输入密码: pwd

加密文本: 𓀸𓀑𓀦𓀌𓀢𓀗𓀄𓀏𓀋𓀔𓀌𓀐𓀁𓀰𓀱𓀎𓀖𓀿𓀦𓀾𓀲𓀻𓀠𓀟𓀉𓀄𓀬𓀎𓀇𓀂𓀡𓀏𓀅𓀞𓀌𓀮𓀙𓀄𓀢𓀅𓀗𓀮𓀓𓀓𓀒𓀷𓀤𓀯𓀟𓀦𓀋𓀤𓀩𓀑𓀍𓀚𓀗𓀪𓀑𓀶𓀆𓀯𓀦𓀔

Python 版代码

文言文实词

代码

import base64

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

from cryptography.hazmat.backends import default_backend

from cryptography.hazmat.primitives import padding

from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

from cryptography.hazmat.primitives import hashes

import os

# 文言文实词集,替换标准的 Base64 字符集

wenyan_base64 = [

'天', '地', '人', '王', '君', '臣', '父', '母', '子', '女', '兄', '弟', '公', '侯', '仁', '义', '礼', '智', '信', '德', '宫', '商', '角', '徵', '羽',

'上', '下', '左', '右', '前', '后', '中', '外', '生', '死', '战', '和', '爱', '恨', '乐', '悲', '大', '小', '长', '短', '远', '近', '明', '暗', '进', '退',

'风', '雨', '雷', '电', '山', '水', '火', '土', '鱼', '鸟', '虎', '龙', '云', '月', '星', '日', '辰', '夫'

]

# 标准 Base64 字符集

charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="

# 将 Base64 编码转换为文言文实词的函数

def base64_to_wenyan(base64_str):

encrypted_text = ""

for char in base64_str:

index = charset.index(char)

encrypted_text += wenyan_base64[index]

return encrypted_text

# 将文言文实词转换回 Base64 的函数

def wenyan_to_base64(wenyan_str):

decrypted_text = ""

for char in wenyan_str:

index = wenyan_base64.index(char)

decrypted_text += charset[index]

return decrypted_text

# AES 加密函数

def encrypt_aes(data, password):

salt = os.urandom(16) # 生成随机盐

kdf = PBKDF2HMAC(

algorithm=hashes.SHA256(),

length=32,

salt=salt,

iterations=100000,

backend=default_backend()

)

key = kdf.derive(password.encode()) # 使用密码生成密钥

iv = os.urandom(16) # 生成随机的初始化向量

cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

encryptor = cipher.encryptor()

# 数据填充

padder = padding.PKCS7(algorithms.AES.block_size).padder()

padded_data = padder.update(data) + padder.finalize()

# 加密数据

encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

return base64.b64encode(salt + iv + encrypted_data).decode('utf-8')

# AES 解密函数

def decrypt_aes(encrypted_data, password):

encrypted_data_bytes = base64.b64decode(encrypted_data)

salt = encrypted_data_bytes[:16]

iv = encrypted_data_bytes[16:32]

encrypted_data = encrypted_data_bytes[32:]

kdf = PBKDF2HMAC(

algorithm=hashes.SHA256(),

length=32,

salt=salt,

iterations=100000,

backend=default_backend()

)

key = kdf.derive(password.encode()) # 使用密码生成密钥

cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

decryptor = cipher.decryptor()

# 解密数据

padded_data = decryptor.update(encrypted_data) + decryptor.finalize()

# 去除填充

unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()

data = unpadder.update(padded_data) + unpadder.finalize()

return data

def compress_and_encrypt(text, password):

text_bytes = text.encode('utf-8')

base64_encoded = base64.b64encode(text_bytes).decode('utf-8')

encrypted_base64 = encrypt_aes(base64_encoded.encode('utf-8'), password)

encrypted_text = base64_to_wenyan(encrypted_base64)

print(f"加密文本: {encrypted_text}")

def decrypt(encrypted_text, password):

base64_decoded_str = wenyan_to_base64(encrypted_text)

decrypted_base64 = decrypt_aes(base64_decoded_str, password)

text_bytes = base64.b64decode(decrypted_base64)

original_text = text_bytes.decode('utf-8')

print(f"解密文本: {original_text}")

# 加密文件

def encrypt_file(file_path, password):

with open(file_path, 'r', encoding='utf-8') as file:

content = file.read()

encrypted_content = compress_and_encrypt(content, password)

output_file = file_path + '.enc'

with open(output_file, 'w', encoding='utf-8') as enc_file:

enc_file.write(encrypted_content)

print(f"文件已加密并保存为: {output_file}")

# 解密文件

def decrypt_file(file_path, password):

with open(file_path, 'r', encoding='utf-8') as file:

encrypted_content = file.read()

try:

decrypted_content = decrypt(encrypted_content, password)

output_file = file_path.replace('.enc', '') + '.dec'

with open(output_file, 'w', encoding='utf-8') as dec_file:

dec_file.write(decrypted_content)

print(f"文件已解密并保存为: {output_file}")

except Exception as e:

print(f"解密失败: {str(e)}")

# 主菜单

def main_menu():

while True:

print("\n请选择一个选项:")

print("1. 加密文本")

print("2. 解密文本")

print("3. 加密文件")

print("4. 解密文件")

print("5. 退出")

choice = input("输入选项 (1-5): ")

if choice == '1':

text = input("输入要加密的文本: ")

password = input("输入密码: ")

encrypted_text = compress_and_encrypt(text, password)

print(f"加密后的文言文实词文本: {encrypted_text}")

elif choice == '2':

encrypted_text = input("输入要解密的文言文实词文本: ")

password = input("输入密码: ")

try:

decrypted_text = decrypt(encrypted_text, password)

print(f"解密后的文本: {decrypted_text}")

except Exception as e:

print(f"解密失败: {str(e)}")

elif choice == '3':

file_path = input("输入要加密的文件路径: ")

password = input("输入密码: ")

encrypt_file(file_path, password)

elif choice == '4':

file_path = input("输入要解密的文件路径: ")

password = input("输入密码: ")

decrypt_file(file_path, password)

elif choice == '5':

print("退出程序")

break

else:

print("无效选项,请重新输入。")

if __name__ == "__main__":

main_menu()

中文生僻字

代码

import base64

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

from cryptography.hazmat.backends import default_backend

from cryptography.hazmat.primitives import padding

from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

from cryptography.hazmat.primitives import hashes

import os

# 中文生僻字集,替换标准的 Base64 字符集

uncommon_cn = [

'齉', '龖', '貔', '貅', '饕', '餮', '龍', '籲', '驫', '麤', '讟', '鸜', '鱻', '黦', '灥',

'鸛', '耂', '皞', '顰', '黳', '齾', '鼂', '曨', '觿', '鼈', '纞', '瓛', '鷟', '爩', '虋',

'瓤', '鞻', '顟', '鸊', '靃', '贑', '龗', '囖', '欂', '騳', '醶', '魕', '繟', '讑', '靐',

'飝', '鬰', '魑', '蠿', '鼘', '鱺', '鷩', '齰', '蘡', '醷', '鰑', '鑴', '驎', '韘', '麷',

'龤', '灊', '觭', '鼯', '矗'

]

# 标准 Base64 字符集

charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="

# 将 Base64 编码转换为中文生僻字的函数

def base64_to_uncommon_cn(base64_str):

encrypted_text = ""

for char in base64_str:

index = charset.index(char)

encrypted_text += uncommon_cn[index]

return encrypted_text

# 将中文生僻字转换回 Base64 的函数

def uncommon_cn_to_base64(uncommon_cn_str):

decrypted_text = ""

for char in uncommon_cn_str:

index = uncommon_cn.index(char)

decrypted_text += charset[index]

return decrypted_text

# AES 加密函数

def encrypt_aes(data, password):

salt = os.urandom(16) # 生成随机盐

kdf = PBKDF2HMAC(

algorithm=hashes.SHA256(),

length=32,

salt=salt,

iterations=100000,

backend=default_backend()

)

key = kdf.derive(password.encode()) # 使用密码生成密钥

iv = os.urandom(16) # 生成随机的初始化向量

cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

encryptor = cipher.encryptor()

# 数据填充

padder = padding.PKCS7(algorithms.AES.block_size).padder()

padded_data = padder.update(data) + padder.finalize()

# 加密数据

encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

return base64.b64encode(salt + iv + encrypted_data).decode('utf-8')

# AES 解密函数

def decrypt_aes(encrypted_data, password):

encrypted_data_bytes = base64.b64decode(encrypted_data)

salt = encrypted_data_bytes[:16]

iv = encrypted_data_bytes[16:32]

encrypted_data = encrypted_data_bytes[32:]

kdf = PBKDF2HMAC(

algorithm=hashes.SHA256(),

length=32,

salt=salt,

iterations=100000,

backend=default_backend()

)

key = kdf.derive(password.encode()) # 使用密码生成密钥

cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

decryptor = cipher.decryptor()

# 解密数据

padded_data = decryptor.update(encrypted_data) + decryptor.finalize()

# 去除填充

unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()

data = unpadder.update(padded_data) + unpadder.finalize()

return data

def compress_and_encrypt(text, password):

text_bytes = text.encode('utf-8')

base64_encoded = base64.b64encode(text_bytes).decode('utf-8')

encrypted_base64 = encrypt_aes(base64_encoded.encode('utf-8'), password)

encrypted_text = base64_to_uncommon_cn(encrypted_base64)

print(f"加密文本: {encrypted_text}")

def decrypt(encrypted_text, password):

base64_decoded_str = uncommon_cn_to_base64(encrypted_text)

decrypted_base64 = decrypt_aes(base64_decoded_str, password)

text_bytes = base64.b64decode(decrypted_base64)

original_text = text_bytes.decode('utf-8')

print(f"解密文本: {original_text}")

# 加密文件

def encrypt_file(file_path, password):

with open(file_path, 'r', encoding='utf-8') as file:

content = file.read()

encrypted_content = compress_and_encrypt(content, password)

output_file = file_path + '.enc'

with open(output_file, 'w', encoding='utf-8') as enc_file:

enc_file.write(encrypted_content)

print(f"文件已加密并保存为: {output_file}")

# 解密文件

def decrypt_file(file_path, password):

with open(file_path, 'r', encoding='utf-8') as file:

encrypted_content = file.read()

try:

decrypted_content = decrypt(encrypted_content, password)

output_file = file_path.replace('.enc', '') + '.dec'

with open(output_file, 'w', encoding='utf-8') as dec_file:

dec_file.write(decrypted_content)

print(f"文件已解密并保存为: {output_file}")

except Exception as e:

print(f"解密失败: {str(e)}")

# 主菜单

def main_menu():

while True:

print("\n请选择一个选项:")

print("1. 加密文本")

print("2. 解密文本")

print("3. 加密文件")

print("4. 解密文件")

print("5. 退出")

choice = input("输入选项 (1-5): ")

if choice == '1':

text = input("输入要加密的文本: ")

password = input("输入密码: ")

encrypted_text = compress_and_encrypt(text, password)

elif choice == '2':

encrypted_text = input("输入要解密的文本: ")

password = input("输入密码: ")

try:

decrypted_text = decrypt(encrypted_text, password)

except Exception as e:

print(f"解密失败: {str(e)}")

elif choice == '3':

file_path = input("输入要加密的文件路径: ")

password = input("输入密码: ")

encrypt_file(file_path, password)

elif choice == '4':

file_path = input("输入要解密的文件路径: ")

password = input("输入密码: ")

decrypt_file(file_path, password)

elif choice == '5':

print("退出程序")

break

else:

print("无效选项,请重新输入。")

if __name__ == "__main__":

main_menu()

中文注音字符

代码

import base64

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

from cryptography.hazmat.backends import default_backend

from cryptography.hazmat.primitives import padding

from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

from cryptography.hazmat.primitives import hashes

import os

# 定义注音符号字符集

zhuyin = [

'ㄅ', 'ㄆ', 'ㄇ', 'ㄈ', 'ㄉ', 'ㄊ', 'ㄋ', 'ㄌ', 'ㄍ', 'ㄎ', 'ㄏ', 'ㄐ', 'ㄑ', 'ㄒ', 'ㄓ', 'ㄔ', 'ㄕ', 'ㄖ', 'ㄗ', 'ㄘ', 'ㄙ', 'ㄧ', 'ㄨ', 'ㄩ', 'ㄚ', 'ㄛ', 'ㄜ', 'ㄝ', 'ㄞ', 'ㄟ', 'ㄠ', 'ㄡ', 'ㄢ', 'ㄣ', 'ㄤ', 'ㄥ', 'ㄦ', '¯', 'ˊ', 'ˇ', 'ˋ', '˙', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '零', 'ㄪ', 'ㄫ', 'ㄬ', 'ㄭ', 'ㆬ', 'ㆭ', 'ㆺ', 'ㆸ', 'ㄏ', 'ㆦ', 'ㆤ', 'ㆱ', '–', '◜', '◞', '◝'

]

# 字符转译表,将字符映射为大小写字母、数字、符号

charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="

# 将 base64 编码的字符映射到注音符号字符集的函数

def base64_to_zhuyin(base64_str):

encrypted_text = ""

for char in base64_str:

index = charset.index(char)

encrypted_text += zhuyin[index]

return encrypted_text

# 将注音符号字符解密回 base64 字符串

def zhuyin_to_base64(zhuyin_str):

decrypted_text = ""

for char in zhuyin_str:

index = zhuyin.index(char)

decrypted_text += charset[index]

return decrypted_text

# AES 加密函数

def encrypt_aes(data, password):

salt = os.urandom(16) # 生成随机盐

kdf = PBKDF2HMAC(

algorithm=hashes.SHA256(),

length=32,

salt=salt,

iterations=100000,

backend=default_backend()

)

key = kdf.derive(password.encode()) # 使用密码生成密钥

iv = os.urandom(16) # 生成随机的初始化向量

cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

encryptor = cipher.encryptor()

# 数据填充

padder = padding.PKCS7(algorithms.AES.block_size).padder()

padded_data = padder.update(data) + padder.finalize()

# 加密数据

encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

return base64.b64encode(salt + iv + encrypted_data).decode('utf-8')

# AES 解密函数

def decrypt_aes(encrypted_data, password):

encrypted_data_bytes = base64.b64decode(encrypted_data)

salt = encrypted_data_bytes[:16]

iv = encrypted_data_bytes[16:32]

encrypted_data = encrypted_data_bytes[32:]

kdf = PBKDF2HMAC(

algorithm=hashes.SHA256(),

length=32,

salt=salt,

iterations=100000,

backend=default_backend()

)

key = kdf.derive(password.encode()) # 使用密码生成密钥

cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

decryptor = cipher.decryptor()

# 解密数据

padded_data = decryptor.update(encrypted_data) + decryptor.finalize()

# 去除填充

unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()

data = unpadder.update(padded_data) + unpadder.finalize()

return data

def compress_and_encrypt(text, password):

text_bytes = text.encode('utf-8')

base64_encoded = base64.b64encode(text_bytes).decode('utf-8')

encrypted_base64 = encrypt_aes(base64_encoded.encode('utf-8'), password)

encrypted_text = base64_to_zhuyin(encrypted_base64)

print(f"加密文本: {encrypted_text}")

def decrypt(encrypted_text, password):

base64_decoded_str = wenyan_to_base64(encrypted_text)

decrypted_base64 = decrypt_aes(base64_decoded_str, password)

text_bytes = base64.b64decode(decrypted_base64)

original_text = text_bytes.decode('utf-8')

print(f"解密文本: {original_text}")

# 加密文件

def encrypt_file(file_path, password):

with open(file_path, 'r', encoding='utf-8') as file:

content = file.read()

encrypted_content = compress_and_encrypt(content, password)

output_file = file_path + '.enc'

with open(output_file, 'w', encoding='utf-8') as enc_file:

enc_file.write(encrypted_content)

print(f"File encrypted and saved as: {output_file}")

# 解密文件

def decrypt_file(file_path, password):

with open(file_path, 'r', encoding='utf-8') as file:

encrypted_content = file.read()

try:

decrypted_content = decrypt(encrypted_content, password)

output_file = file_path.replace('.enc', '') + '.dec'

with open(output_file, 'w', encoding='utf-8') as dec_file:

dec_file.write(decrypted_content)

print(f"File decrypted and saved as: {output_file}")

except Exception as e:

print(f"Decryption failed: {str(e)}")

# 主菜单

def main_menu():

while True:

print("\n请选择一个选项:")

print("1. 加密文本")

print("2. 解密文本")

print("3. 加密文件")

print("4. 解密文件")

print("5. 退出")

choice = input("输入选项 (1-5): ")

if choice == '1':

text = input("输入要加密的文本: ")

password = input("输入密码: ")

encrypted_text = compress_and_encrypt(text, password)

elif choice == '2':

encrypted_text = input("输入要解密的文本: ")

password = input("输入密码: ")

try:

decrypted_text = decrypt(encrypted_text, password)

except Exception as e:

print(f"解密失败: {str(e)}")

elif choice == '3':

file_path = input("输入要加密的文件路径: ")

password = input("输入密码: ")

encrypt_file(file_path, password)

elif choice == '4':

file_path = input("输入要解密的文件路径: ")

password = input("输入密码: ")

decrypt_file(file_path, password)

elif choice == '5':

print("退出程序。")

break

else:

print("无效的选项,请重新输入。")

# 启动主程序

if __name__ == "__main__":

main_menu()

日语片假名

代码

import base64

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

from cryptography.hazmat.backends import default_backend

from cryptography.hazmat.primitives import padding

from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

from cryptography.hazmat.primitives import hashes

import os

# 定义片假名字符集 (片假名 + 片假浊音 + 长音)

katakana = [

'ア', 'イ', 'ウ', 'エ', 'オ', 'カ', 'キ', 'ク', 'ケ', 'コ', 'サ', 'シ', 'ス', 'セ', 'ソ',

'タ', 'チ', 'ツ', 'テ', 'ト', 'ナ', 'ニ', 'ヌ', 'ネ', 'ノ', 'ハ', 'ヒ', 'フ', 'ヘ', 'ホ',

'マ', 'ミ', 'ム', 'メ', 'モ', 'ヤ', 'ユ', 'ヨ', 'ラ', 'リ', 'ル', 'レ', 'ロ', 'ワ', 'ヲ', 'ン',

'ガ', 'ギ', 'グ', 'ゲ', 'ゴ', 'ザ', 'ジ', 'ズ', 'ゼ', 'ゾ', 'ダ', 'ヂ', 'ヅ', 'デ', 'ド',

'バ', 'ビ', 'ブ', 'ベ', 'ボ', 'パ', 'ピ', 'プ', 'ペ', 'ポ', 'ヰ', 'ヱ'

]

# 字符转译表,将字符映射为大小写字母、数字、符号

charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="

# 将 base64 编码的字符映射到片假名字符集的函数

def base64_to_katakana(base64_str):

encrypted_text = ""

for char in base64_str:

index = charset.index(char)

encrypted_text += katakana[index]

return encrypted_text

# 将片假名字符解密回 base64 字符串

def katakana_to_base64(katakana_str):

decrypted_text = ""

for char in katakana_str:

index = katakana.index(char)

decrypted_text += charset[index]

return decrypted_text

# AES 加密函数

def encrypt_aes(data, password):

salt = os.urandom(16) # 生成随机盐

kdf = PBKDF2HMAC(

algorithm=hashes.SHA256(),

length=32,

salt=salt,

iterations=100000,

backend=default_backend()

)

key = kdf.derive(password.encode()) # 使用密码生成密钥

iv = os.urandom(16) # 生成随机的初始化向量

cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

encryptor = cipher.encryptor()

# 数据填充

padder = padding.PKCS7(algorithms.AES.block_size).padder()

padded_data = padder.update(data) + padder.finalize()

# 加密数据

encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

return base64.b64encode(salt + iv + encrypted_data).decode('utf-8')

# AES 解密函数

def decrypt_aes(encrypted_data, password):

encrypted_data_bytes = base64.b64decode(encrypted_data)

salt = encrypted_data_bytes[:16]

iv = encrypted_data_bytes[16:32]

encrypted_data = encrypted_data_bytes[32:]

kdf = PBKDF2HMAC(

algorithm=hashes.SHA256(),

length=32,

salt=salt,

iterations=100000,

backend=default_backend()

)

key = kdf.derive(password.encode()) # 使用密码生成密钥

cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

decryptor = cipher.decryptor()

# 解密数据

padded_data = decryptor.update(encrypted_data) + decryptor.finalize()

# 去除填充

unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()

data = unpadder.update(padded_data) + unpadder.finalize()

return data

def compress_and_encrypt(text, password):

text_bytes = text.encode('utf-8')

base64_encoded = base64.b64encode(text_bytes).decode('utf-8')

encrypted_base64 = encrypt_aes(base64_encoded.encode('utf-8'), password)

encrypted_text = base64_to_katakana(encrypted_base64)

print(f"加密文本: {encrypted_text}")

def decrypt(encrypted_text, password):

base64_decoded_str = wenyan_to_base64(encrypted_text)

decrypted_base64 = decrypt_aes(base64_decoded_str, password)

text_bytes = base64.b64decode(decrypted_base64)

original_text = text_bytes.decode('utf-8')

print(f"解密文本: {original_text}")

# 加密文件

def encrypt_file(file_path, password):

with open(file_path, 'r', encoding='utf-8') as file:

content = file.read()

encrypted_content = compress_and_encrypt(content, password)

output_file = file_path + '.enc'

with open(output_file, 'w', encoding='utf-8') as enc_file:

enc_file.write(encrypted_content)

print(f"File encrypted and saved as: {output_file}")

# 解密文件

def decrypt_file(file_path, password):

with open(file_path, 'r', encoding='utf-8') as file:

encrypted_content = file.read()

try:

decrypted_content = decrypt(encrypted_content, password)

output_file = file_path.replace('.enc', '') + '.dec'

with open(output_file, 'w', encoding='utf-8') as dec_file:

dec_file.write(decrypted_content)

print(f"File decrypted and saved as: {output_file}")

except Exception as e:

print(f"Decryption failed: {str(e)}")

# 主菜单

def main_menu():

while True:

print("\n请选择一个选项:")

print("1. 加密文本")

print("2. 解密文本")

print("3. 加密文件")

print("4. 解密文件")

print("5. 退出")

choice = input("输入选项 (1-5): ")

if choice == '1':

text = input("输入要加密的文本: ")

password = input("输入密码: ")

encrypted_text = compress_and_encrypt(text, password)

elif choice == '2':

encrypted_text = input("输入要解密的文本: ")

password = input("输入密码: ")

try:

decrypted_text = decrypt(encrypted_text, password)

except Exception as e:

print(f"解密失败: {str(e)}")

elif choice == '3':

file_path = input("输入要加密的文件路径: ")

password = input("输入密码: ")

encrypt_file(file_path, password)

elif choice == '4':

file_path = input("输入要解密的文件路径: ")

password = input("输入密码: ")

decrypt_file(file_path, password)

elif choice == '5':

print("退出程序。")

break

else:

print("无效的选项,请重新输入。")

# 启动主程序

if __name__ == "__main__":

main_menu()

谚文字母(朝鲜国字/韩字)

代码

import base64

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

from cryptography.hazmat.backends import default_backend

from cryptography.hazmat.primitives import padding

from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

from cryptography.hazmat.primitives import hashes

import os

# 韩语集,替换标准的 Base64 字符集

hangul = [

'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ',

'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ', 'ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ',

'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ', 'ㄳ', 'ㄵ', 'ㄶ', 'ㄺ', 'ㄻ',

'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅄ', 'ᄆ', 'ᄀ', 'ᄋ', 'ᆞ', 'ᅎ', 'ᅀ',

'ᄵ', 'ᆷ', 'ᆼ', 'ᆠ', 'ᆸ', 'ᆮ', 'ᇹ', 'ᄌ', 'ᆺ'

]

# 标准 Base64 字符集

charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="

# 将 Base64 编码转换为韩语的函数

def base64_to_hangul(base64_str):

encrypted_text = ""

for char in base64_str:

index = charset.index(char)

encrypted_text += hangul[index]

return encrypted_text

# 将韩语转换回 Base64 的函数

def hangul_to_base64(hangul_str):

decrypted_text = ""

for char in hangul_str:

index = hangul.index(char)

decrypted_text += charset[index]

return decrypted_text

# AES 加密函数

def encrypt_aes(data, password):

salt = os.urandom(16) # 生成随机盐

kdf = PBKDF2HMAC(

algorithm=hashes.SHA256(),

length=32,

salt=salt,

iterations=100000,

backend=default_backend()

)

key = kdf.derive(password.encode()) # 使用密码生成密钥

iv = os.urandom(16) # 生成随机的初始化向量

cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

encryptor = cipher.encryptor()

# 数据填充

padder = padding.PKCS7(algorithms.AES.block_size).padder()

padded_data = padder.update(data) + padder.finalize()

# 加密数据

encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

return base64.b64encode(salt + iv + encrypted_data).decode('utf-8')

# AES 解密函数

def decrypt_aes(encrypted_data, password):

encrypted_data_bytes = base64.b64decode(encrypted_data)

salt = encrypted_data_bytes[:16]

iv = encrypted_data_bytes[16:32]

encrypted_data = encrypted_data_bytes[32:]

kdf = PBKDF2HMAC(

algorithm=hashes.SHA256(),

length=32,

salt=salt,

iterations=100000,

backend=default_backend()

)

key = kdf.derive(password.encode()) # 使用密码生成密钥

cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

decryptor = cipher.decryptor()

# 解密数据

padded_data = decryptor.update(encrypted_data) + decryptor.finalize()

# 去除填充

unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()

data = unpadder.update(padded_data) + unpadder.finalize()

return data

def compress_and_encrypt(text, password):

text_bytes = text.encode('utf-8')

base64_encoded = base64.b64encode(text_bytes).decode('utf-8')

encrypted_base64 = encrypt_aes(base64_encoded.encode('utf-8'), password)

encrypted_text = base64_to_hangul(encrypted_base64)

print(f"加密文本: {encrypted_text}")

def decrypt(encrypted_text, password):

base64_decoded_str = hangul_to_base64(encrypted_text)

decrypted_base64 = decrypt_aes(base64_decoded_str, password)

text_bytes = base64.b64decode(decrypted_base64)

original_text = text_bytes.decode('utf-8')

print(f"解密文本: {original_text}")

# 加密文件

def encrypt_file(file_path, password):

with open(file_path, 'r', encoding='utf-8') as file:

content = file.read()

encrypted_content = compress_and_encrypt(content, password)

output_file = file_path + '.enc'

with open(output_file, 'w', encoding='utf-8') as enc_file:

enc_file.write(encrypted_content)

print(f"文件已加密并保存为: {output_file}")

# 解密文件

def decrypt_file(file_path, password):

with open(file_path, 'r', encoding='utf-8') as file:

encrypted_content = file.read()

try:

decrypted_content = decrypt(encrypted_content, password)

output_file = file_path.replace('.enc', '') + '.dec'

with open(output_file, 'w', encoding='utf-8') as dec_file:

dec_file.write(decrypted_content)

print(f"文件已解密并保存为: {output_file}")

except Exception as e:

print(f"解密失败: {str(e)}")

# 主菜单

def main_menu():

while True:

print("\n请选择一个选项:")

print("1. 加密文本")

print("2. 解密文本")

print("3. 加密文件")

print("4. 解密文件")

print("5. 退出")

choice = input("输入选项 (1-5): ")

if choice == '1':

text = input("输入要加密的文本: ")

password = input("输入密码: ")

encrypted_text = compress_and_encrypt(text, password)

elif choice == '2':

encrypted_text = input("输入要解密的文本: ")

password = input("输入密码: ")

try:

decrypted_text = decrypt(encrypted_text, password)

except Exception as e:

print(f"解密失败: {str(e)}")

elif choice == '3':

file_path = input("输入要加密的文件路径: ")

password = input("输入密码: ")

encrypt_file(file_path, password)

elif choice == '4':

file_path = input("输入要解密的文件路径: ")

password = input("输入密码: ")

decrypt_file(file_path, password)

elif choice == '5':

print("退出程序")

break

else:

print("无效选项,请重新输入。")

if __name__ == "__main__":

main_menu()

古埃及圣书体

代码

import base64

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

from cryptography.hazmat.backends import default_backend

from cryptography.hazmat.primitives import padding

from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

from cryptography.hazmat.primitives import hashes

import os

egyptian = [

'𓀀', '𓀁', '𓀂', '𓀃', '𓀄', '𓀅', '𓀆', '𓀇', '𓀈', '𓀉', '𓀊', '𓀋', '𓀌', '𓀍', '𓀎', '𓀏', '𓀐', '𓀑', '𓀒', '𓀓',

'𓀔', '𓀕', '𓀖', '𓀗', '𓀘', '𓀙', '𓀚', '𓀛', '𓀜', '𓀝', '𓀞', '𓀟', '𓀠', '𓀡', '𓀢', '𓀣', '𓀤', '𓀥', '𓀦',

'𓀧', '𓀨', '𓀩', '𓀪', '𓀫', '𓀬', '𓀭', '𓀮', '𓀯', '𓀰', '𓀱', '𓀲', '𓀳', '𓀴', '𓀵', '𓀶', '𓀷', '𓀸', '𓀹',

'𓀺', '𓀻', '𓀼', '𓀽', '𓀾', '𓀿', '𓁀', '𓁁', '𓁂', '𓁃', '𓁄'

]

# 标准 Base64 字符集

charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="

# 将 Base64 编码转换为古埃及圣书体的函数

def base64_to_egyptian(base64_str):

encrypted_text = ""

for char in base64_str:

index = charset.index(char)

encrypted_text += egyptian[index]

return encrypted_text

# 将古埃及圣书体转换回 Base64 的函数

def egyptian_to_base64(egyptian_str):

decrypted_text = ""

for char in egyptian_str:

index = egyptian.index(char)

decrypted_text += charset[index]

return decrypted_text

# AES 加密函数

def encrypt_aes(data, password):

salt = os.urandom(16) # 生成随机盐

kdf = PBKDF2HMAC(

algorithm=hashes.SHA256(),

length=32,

salt=salt,

iterations=100000,

backend=default_backend()

)

key = kdf.derive(password.encode()) # 使用密码生成密钥

iv = os.urandom(16) # 生成随机的初始化向量

cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

encryptor = cipher.encryptor()

# 数据填充

padder = padding.PKCS7(algorithms.AES.block_size).padder()

padded_data = padder.update(data) + padder.finalize()

# 加密数据

encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

return base64.b64encode(salt + iv + encrypted_data).decode('utf-8')

# AES 解密函数

def decrypt_aes(encrypted_data, password):

encrypted_data_bytes = base64.b64decode(encrypted_data)

salt = encrypted_data_bytes[:16]

iv = encrypted_data_bytes[16:32]

encrypted_data = encrypted_data_bytes[32:]

kdf = PBKDF2HMAC(

algorithm=hashes.SHA256(),

length=32,

salt=salt,

iterations=100000,

backend=default_backend()

)

key = kdf.derive(password.encode()) # 使用密码生成密钥

cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())

decryptor = cipher.decryptor()

# 解密数据

padded_data = decryptor.update(encrypted_data) + decryptor.finalize()

# 去除填充

unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()

data = unpadder.update(padded_data) + unpadder.finalize()

return data

def compress_and_encrypt(text, password):

text_bytes = text.encode('utf-8')

base64_encoded = base64.b64encode(text_bytes).decode('utf-8')

encrypted_base64 = encrypt_aes(base64_encoded.encode('utf-8'), password)

encrypted_text = base64_to_egyptian(encrypted_base64)

print(f"加密文本: {encrypted_text}")

def decrypt(encrypted_text, password):

base64_decoded_str = egyptian_to_base64(encrypted_text)

decrypted_base64 = decrypt_aes(base64_decoded_str, password)

text_bytes = base64.b64decode(decrypted_base64)

original_text = text_bytes.decode('utf-8')

print(f"解密文本: {original_text}")

# 加密文件

def encrypt_file(file_path, password):

with open(file_path, 'r', encoding='utf-8') as file:

content = file.read()

encrypted_content = compress_and_encrypt(content, password)

output_file = file_path + '.enc'

with open(output_file, 'w', encoding='utf-8') as enc_file:

enc_file.write(encrypted_content)

print(f"文件已加密并保存为: {output_file}")

# 解密文件

def decrypt_file(file_path, password):

with open(file_path, 'r', encoding='utf-8') as file:

encrypted_content = file.read()

try:

decrypted_content = decrypt(encrypted_content, password)

output_file = file_path.replace('.enc', '') + '.dec'

with open(output_file, 'w', encoding='utf-8') as dec_file:

dec_file.write(decrypted_content)

print(f"文件已解密并保存为: {output_file}")

except Exception as e:

print(f"解密失败: {str(e)}")

# 主菜单

def main_menu():

while True:

print("\n请选择一个选项:")

print("1. 加密文本")

print("2. 解密文本")

print("3. 加密文件")

print("4. 解密文件")

print("5. 退出")

choice = input("输入选项 (1-5): ")

if choice == '1':

text = input("输入要加密的文本: ")

password = input("输入密码: ")

encrypted_text = compress_and_encrypt(text, password)

elif choice == '2':

encrypted_text = input("输入要解密的文本: ")

password = input("输入密码: ")

try:

decrypted_text = decrypt(encrypted_text, password)

except Exception as e:

print(f"解密失败: {str(e)}")

elif choice == '3':

file_path = input("输入要加密的文件路径: ")

password = input("输入密码: ")

encrypt_file(file_path, password)

elif choice == '4':

file_path = input("输入要解密的文件路径: ")

password = input("输入密码: ")

decrypt_file(file_path, password)

elif choice == '5':

print("退出程序")

break

else:

print("无效选项,请重新输入。")

if __name__ == "__main__":

main_menu()

JavaScript 版代码

文言文实词

一种基于奇怪字符的文本/文件加密方式

开发调优

js是可以做到的

文言文实词加密工具

文言文实词加密工具

加密文本



加密后的文言文实词文本:

解密文本



解密后的文本:

加密文件



解密文件



1 个赞

web

(Ethereum Foundation)

2024 年10 月 2 日 11:26

11

下次做一个数字签名…

给xless用

1 个赞

PlayMcBKuwu

(鹿目円香 Pro Max)

2024 年10 月 2 日 11:32

12

确实,而且相比于熊曰和佛曰等仍然有很大的提升空间。

2 个赞

PlayMcBKuwu

(鹿目円香 Pro Max)

2024 年10 月 2 日 11:33

13

肯定是我喂给 Claude 3.5 Sonnet 的时候,Prompt 没搞好(

2 个赞

PlayMcBKuwu

(鹿目円香 Pro Max)

2024 年10 月 2 日 11:34

14

还是有部分问题的,

比如 JavaScript 版转码的结果丢给 Python 版会解码失败。

似乎是 JavaScript 版本映射表的问题?

2 个赞

awz707

(awz707)

2024 年10 月 2 日 11:38

15

好玩 感谢分享!

2 个赞

sketu

(天源E兔)

2024 年10 月 2 日 11:39

16

我记得js的加密和Python的加密好像本来就有差异。

2 个赞

PlayMcBKuwu

(鹿目円香 Pro Max)

2024 年10 月 2 日 11:40

17

ゴチクマザオヘゲヂトトニクムセゲギチヨグゴメムヤヂグカコミヤセキリネゾムフキナロノツクエニケユアンアヨトコレホゴワドエギヌナタフホイフヂアノノメルジルデドノハオムヲジツギチベベ

加密使用 kana.py

密码 pwd

2 个赞

sketu

(天源E兔)

2024 年10 月 2 日 11:42

18

出门了,没电脑。懒得实现其他的,你能不能帮忙转成文言文?

3 个赞

PlayMcBKuwu

(鹿目円香 Pro Max)

2024 年10 月 2 日 11:46

19

一种基于奇怪字符的文本/文件加密方式 开发调优

ゴチクマザオヘゲヂトトニクムセゲギチヨグゴメムヤヂグカコミヤセキリネゾムフキナロノツクエニケユアンアヨトコレホゴワドエギヌナタフホイフヂアノノメルジルデドノハオムヲジツギチベベ

加密使用 kana.py

密码 pwd

君鱼云死弟上人人角鱼雷乐地暗商乐羽天近龙上下远悲退长徵智羽长暗母兄子大左战地土仁雨宫短徵死恨风火羽恨子电死地云侯天风右鱼宫长雷外母母地虎智德鸟女后生远暗恨雷上暗女下大天土外月月

加密使用 wenyan.py

密码 pwd

1 个赞

sketu

(天源E兔)

2024 年10 月 2 日 11:53

20

感谢认同 =w=

???

目前已检出的问题

打文言字符表 的时候,字符列表打错了

3 个赞

养生小贴士

两送乌龙仍绝杀以色列 意大利保住晋级世界杯希望
发通知办公软件有哪些软件
💡 小知识

发通知办公软件有哪些软件

📅 02-08 👍 907
陈楚生为什么是内地歌王
💡 小知识

陈楚生为什么是内地歌王

📅 07-13 👍 574