-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
176 lines (156 loc) · 7.1 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import re
import base64
import hashlib
from cryptography.fernet import Fernet
import streamlit as st
from zipfile import ZipFile
from io import BytesIO
def contains_japanese(text):
# This regex includes the Unicode ranges for Hiragana, Katakana, and CJK (which includes Kanji)
japanese_regex = re.compile(r'[\u3040-\u30FF\u4E00-\u9FFF]')
return bool(japanese_regex.search(text))
# Function to generate a key from a passphrase
def generate_key(passphrase: str) -> bytes:
return base64.urlsafe_b64encode(hashlib.sha256(passphrase.encode()).digest())
def encrypt_comment(comment: str, key: bytes) -> str:
fernet = Fernet(key)
encrypted_comment = fernet.encrypt(comment.encode())
return encrypted_comment.decode()
def encrypt_comments_in_file(file_content: str):
# with open(file_path, 'r') as file:
# lines = file.readlines()
lines = file_content.split('\n')
encrypted_lines = []
for line in lines:
match = re.search('|'.join(['//', '#region ', '#endregion ']), line, re.IGNORECASE)
match2 = re.search('\$".*"', line)
matches1 = re.finditer("'(.*?)'", line) # The ? makes the * non-greedy for single-quoted string matches
matches2 = re.finditer('"(.*?)"', line) # The ? makes the * non-greedy for double-quoted string matches
if match:
space_or_code, comment = line.split(match.group(0), 1)
if contains_japanese(comment):
encrypted_comment = encrypt_comment(comment, key)
encrypted_lines.append(f'{space_or_code}{match.group(0)}{encrypted_comment}')
else:
encrypted_lines.append(line)
elif match2:
matches = re.finditer('|'.join(['^.*?{', '}.*?{', '}.*?$']), match2.group(0)[1:])
for match in matches:
literal = match.group(0)[1:-1]
if contains_japanese(literal):
encrypted_literal = encrypt_comment(literal, key)
line = line.replace(literal, encrypted_literal)
encrypted_lines.append(line)
else:
literals = [match.group(1) for match in matches1]
for literal in literals:
if contains_japanese(literal):
encrypted_literal = encrypt_comment(literal, key)
line = line.replace(literal, encrypted_literal)
literals = [match.group(1) for match in matches2]
for literal in literals:
if contains_japanese(literal):
encrypted_literal = encrypt_comment(literal, key)
line = line.replace(literal, encrypted_literal)
encrypted_lines.append(line)
# with open(file_path, 'w') as file:
# file.writelines(encrypted_lines)
return '\n'.join(encrypted_lines)
# # Usage
# file_path = 'your_source_code.cs'
# passphrase = 'your_secure_passphrase'
# encrypt_comments_in_file(file_path)
def decrypt_comment(encrypted_comment: str, key: bytes) -> str:
fernet = Fernet(key)
decrypted_comment = fernet.decrypt(encrypted_comment.encode())
return decrypted_comment.decode()
def decrypt_comments_in_file(file_content: str):
# with open(file_path, 'r') as file:
# lines = file.readlines()
lines = file_content.split('\n')
decrypted_lines = []
for line in lines:
match = re.search('|'.join(['//', '#region ', '#endregion ']), line, re.IGNORECASE)
match2 = re.search('\$".*"', line)
matches1 = re.finditer("'(.*?)'", line) # The ? makes the * non-greedy for single-quoted string matches
matches2 = re.finditer('"(.*?)"', line) # The ? makes the * non-greedy for double-quoted string matches
if match:
space_or_code, comment = line.split(match.group(0), 1)
try:
decrypted_comment = decrypt_comment(comment, key)
decrypted_lines.append(f'{space_or_code}{match.group(0)}{decrypted_comment}')
except:
decrypted_lines.append(line)
elif match2:
matches = re.finditer('|'.join(['^.*?{', '}.*?{', '}.*?$']), match2.group(0)[1:])
for match in matches:
literal = match.group(0)[1:-1]
try:
decrypted_literal = decrypt_comment(literal, key)
line = line.replace(literal, decrypted_literal)
except:
pass
decrypted_lines.append(line)
else:
literals = [match.group(1) for match in matches1]
for literal in literals:
try:
decrypted_literal = decrypt_comment(literal, key)
line = line.replace(literal, decrypted_literal)
except:
pass
literals = [match.group(1) for match in matches2]
for literal in literals:
try:
decrypted_literal = decrypt_comment(literal, key)
line = line.replace(literal, decrypted_literal)
except:
pass
decrypted_lines.append(line)
# with open(file_path, 'w') as file:
# file.writelines(decrypted_lines)
return '\n'.join(decrypted_lines)
# # Usage
# file_path = 'your_source_code.cs'
# passphrase = 'your_secure_passphrase'
# decrypt_comments_in_file(file_path)
st.title('C#中日韓註解字串加密/解密工具')
passphrase = st.text_input("金鑰(自訂)")
key = generate_key(passphrase)
process = {'加密': encrypt_comments_in_file, '解密': decrypt_comments_in_file}
mode = st.selectbox('模式', process.keys())
uploaded_file = st.file_uploader("上傳", type=['cs', 'zip'])
if uploaded_file is not None and passphrase:
file_name, ext = uploaded_file.name.split('.')
if ext.lower() == 'cs':
file_name = f'{file_name}.cs'
mime_type = 'text/plain'
file_content = uploaded_file.read().decode()
download = processed_content = process[mode](file_content)
if ext.lower() == 'zip':
file_name = f'{file_name}_{mode}.zip'
mime_type = 'application/zip'
target_zip_buffer = BytesIO()
with ZipFile(target_zip_buffer, 'w') as target_zip:
with ZipFile(uploaded_file, 'r') as source_zip:
for name in source_zip.namelist():
with source_zip.open(name) as file:
try:
file_content = file.read().decode()
if name.endswith('.cs'):
processed_content = process[mode](file_content)
target_zip.writestr(name, processed_content)
else:
target_zip.writestr(name, file_content)
except:
pass
target_zip_buffer.seek(0)
download = target_zip_buffer
# processed_content = process_script(file_content, passphrase, salt, mode)
# st.text_area("Processed Script", value=processed_content, height=400)
st.download_button(
label="下載",
data=download,
file_name=file_name,
mime=mime_type
)