Skip to content

Commit

Permalink
修复一些bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouxuanyi-zxy committed Jan 31, 2023
1 parent 0c16846 commit af1b276
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 31 deletions.
87 changes: 57 additions & 30 deletions Old-Driver-Tools-CLI.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Old-Driver-Tools Python Edition
# Author: zhouxuanyi-zxy
# Licence: MIT Licence
# Version: v0.3b
# Version: v0.4b
# Language: Python
# 2023.1.3-v0.1b
# 2023.1.23-v0.2b
# 2023.1.23-v0.3b
# 2023.1.31-v0.4b

import base64
import os
Expand All @@ -14,28 +15,37 @@
try:
import pyperclip # 复制到剪切板
import gmssl
import chardet
except ModuleNotFoundError: # 如果没有此包,则使用国内pip源安装
os.system("pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyperclip")
os.system("pip install -i https://pypi.tuna.tsinghua.edu.cn/simple gmssl")
os.system("python Old_Driver_Tools.py") # 自动重启程序
exit()
os.system("pip install -i https://pypi.tuna.tsinghua.edu.cn/simple chardet")
os.system("python Old-Driver-Tools-CLI.py") # 自动重启程序
sys.exit()
from gmssl import sm2

# 导入结束,程序开始
base64_decode_encode = "encode" # base64模式判断
utf_8_decode_encode = "decode" # UTF-8模式判断
base64_decode_encode = "" # base64模式判断
utf_8_decode_encode = "" # UTF-8模式判断
sm2_decrypt_encrypt = "encrypt"
user_input = "" # 用户输入

def base64_de_en(CLI_base64_input=None): # base64编/解码具体实现
global user_input,base64_decode_encode
user_input = str(CLI_base64_input)
user_input = CLI_base64_input
user_input = bytes(user_input,encoding="utf-8") # 转换为bytes类型
print(user_input)
try:
if base64_decode_encode == "encode": # 编码
base64_out = base64.b64encode(user_input) # 编码
elif base64_decode_encode == "decode": # 解码
base64_out = base64.b64decode(user_input) # 解码
# pyperclip.copy(str(base64_out)[2:-1:])
print(str(base64_out)[2:-1:])
if chardet.detect(base64_out)["encoding"] == "utf-8": # 有可能出错,后续增加可信度检测以及跳过检测
# pyperclip.copy(base64_out.decode("utf-8"))
print(base64_out.decode("utf-8"))
else:
# pyperclip.copy(str(base64_out)[2:-1:])
print(str(base64_out)[2:-1:])
except:
print("Error!")

Expand All @@ -55,29 +65,46 @@ def utf_8_de_en(CLI_utf8_input=None): # UTF-8编/解码
utf_8_out = utf_8_out.decode("utf-8") # 终于结束了
# pyperclip.copy(utf_8_out)
print(utf_8_out)
# utf_8_de_en()
# base64_de_en()

def sm2_decrypt_encrypt():
SM2_PUBLIC_KEY = "" # 公钥
SM2_PRIVATE_KEY = "" # 私钥
sm2_crypt = sm2.CryptSM2(public_key=SM2_PUBLIC_KEY,private_key=SM2_PRIVATE_KEY)
def sm2_de_en(): # 初步框架,无法使用
global sm2_decrypt_encrypt,user_input
# user_input = input()
user_input = b'123'
# user_input = bytes(user_input,encoding="utf-8")
print(user_input)
sm2_output = ""
pubric_key = '00B9AB0B828FF68872F21A837FC303668428DEA11DCD1B24429D0C99E24EED83D5' # 公钥
SM2_PUBLIC_KEY = '00B9AB0B828FF68872F21A837FC303668428DEA11DCD1B24429D0C99E24EED83D5' # 公钥
SM2_PRIVATE_KEY = 'B9C9A6E04E9C91F7BA880429273747D7EF5DDEB0BB2FF6317EB00BEF331A83081A6994B8993F3F5D6EADDDB81872266C87C018FB4162F5AF347B483E24620207' # 私钥
private_key = 'B9C9A6E04E9C91F7BA880429273747D7EF5DDEB0BB2FF6317EB00BEF331A83081A6994B8993F3F5D6EADDDB81872266C87C018FB4162F5AF347B483E24620207' # 私钥
sm2_crypt = sm2.CryptSM2(public_key=pubric_key,private_key=private_key)
if sm2_decrypt_encrypt == "encrypt":
sm2_output = sm2_crypt.encrypt(user_input.encode("utf-8"))
elif sm2_decrypt_encrypt == "decrypt":
sm2_output = sm2_crypt.decrypt(user_input).decode("utf-8")
print(sm2_output)

# sm2_de_en()
if __name__ == "__main__":
CLI_input = ""
if sys.argv[1] == "base64":
if sys.argv[2] == "decode":
CLI_input = sys.argv[3]
base64_decode_encode = "decode"
if sys.argv[2] == "encode":
CLI_input = sys.argv[3]
base64_decode_encode = "encode"
base64_de_en(CLI_input)
elif sys.argv[1] == "utf-8":
if sys.argv[2] == "decode":
CLI_input = sys.argv[3]
utf_8_decode_encode = "decode"
if sys.argv[2] == "encode":
CLI_input = sys.argv[3]
utf_8_decode_encode = "encode"
utf_8_de_en(CLI_input)
try:
if sys.argv[1] == "base64":
if sys.argv[2] == "decode":
CLI_input = sys.argv[3]
base64_decode_encode = "decode"
if sys.argv[2] == "encode":
CLI_input = sys.argv[3]
base64_decode_encode = "encode"
base64_de_en(CLI_input)
elif sys.argv[1] == "utf-8":
if sys.argv[2] == "decode":
CLI_input = sys.argv[3]
utf_8_decode_encode = "decode"
if sys.argv[2] == "encode":
CLI_input = sys.argv[3]
utf_8_decode_encode = "encode"
utf_8_de_en(CLI_input)
except IndexError:
print('''用法:
python Old-Driver-Tools-CLI.py [base64/utf-8] [decode/encode] [内容]
''')
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,21 @@

1、~~加入命令行调用~~ (已基本实现)

2、支持SM2加/解密
2、支持SM2加/解密 (在了解原理和具体命令使用)

3、将[utf-8编/解码]再套一层base64,以增加英文编码支持

4、增加使用base64对图片进行编/解码

#### 更新历史

v0.4b (2023.1.31)

1、修复[base64解码]是非英文时无法直接输出结果

2、增加没有输入模式时,显示程序内命令行用法说明

3、将pip安装缺失库后,自动重启所指向的文件名改正(所以之前测试时是怎么跑起来的?后续可能会删除自重启)

总结:本项目技术力低下,代码稀碎,但是还会优化

Expand Down

0 comments on commit af1b276

Please sign in to comment.