Skip to content

Commit

Permalink
Merge pull request #3 from caikun233/dev
Browse files Browse the repository at this point in the history
add 2 func
  • Loading branch information
caikun233 authored Jun 11, 2024
2 parents 6203282 + 737e16e commit 1168975
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 34 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ Currently only Simplified Chinese and English are supported.
3. The program will automatically create a folder named "RSAkeys" in the same level directory, and create a folder of "my" and "friend" in it to distinguish the key between you and the chat partner.
4. The program only support zh-CN till y2023/m07/d01. I will upload EN version soon, so no more explanation here.
4. ~~The program only support zh-CN till y2023/m07/d01. I will upload EN version soon, so no more explanation here.~~ I uploaded EN version.
### Windows GUI 🔨
### Windows GUI 🔨
1. **The GUI version developed, you can now enjoy releases.**
1. **The GUI beta version completed.**
2. No more EXE file in releases! Download from Actions! Thanks GitHub!**No x86 support, x64 only**.
3. Double click exe file, it looks ugly, right? I am not good at any art, but I will try my best to make it more beautiful.
4. The first text entry box is your friend's public key's path you want to input. The 2nd text entry box is where you input words to encrypt or decrypt.
Expand All @@ -51,8 +51,8 @@ Currently only Simplified Chinese and English are supported.
- [x] Add English support for releases and source code.
- [x] Finish GUI version development.
- [ ] Make RSA key length optional.
- [x] Make RSA key length optional.(only CLI completed)
- [ ] Make GUI more beautiful.
- [ ] Try to add images base64 encode.
- [x] Let the encrypted text be copied automatically.
- [ ] According to a Twitterer, we can add a function to calculate some hash of images to avoid the platform adding tracking watermarks to the image.
- [x] According to a Twitterer, we can add a function to calculate some hash of images to avoid the platform adding tracking watermarks to the image, also we can check them.(Now supports any file)
9 changes: 4 additions & 5 deletions README.zhcn.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@

3. 程序将自动在同级目录下新建名为“RSAkeys”的文件夹,并且在其中创建名为“my”“friend”的文件夹来区分你和你的聊天对象。

4. 程序至少在2023年7月1日前仅支持简体中文,我将很快上传英文版,所以在这里暂时没有各选项的描述
4. 程序至少在2023年7月1日前仅支持简体中文,我已经上传英文版

### Windows 图形化界面(GUI) 🔨

1. **GUI版本已经开发完成,有release可用。**
~~2. 从 [releases](https://github.com/caikun233/CryphoCat/releases) 下载最新的发行版,**不提供x86_x32的技术支持,仅支持x64。要是32位系统用不了别提issue,提一个删一个。**~~
1. **GUI beta 已经开发完成.**
2. releases页面中没有可执行文件!从Actions下载!Thanks GitHub for CPU.**不提供x32的技术支持,仅支持x64。要是32位系统用不了别提issue,提一个删一个。**
3. 双击打开文件。看起来很丑对吧?我没有一点点艺术功底,但我会尽量给它搞得好看点。
4. 第一个文本输入框是输入你朋友公钥的路径的,第二个文本输入框是输入你需要加/解密的文本的。
Expand All @@ -52,8 +51,8 @@

- [x] 在源代码和release中加入英文支持。
- [x] 完成GUI版本的开发。
- [ ] 使RSA密钥长度可选。
- [x] 使RSA密钥长度可选。
- [ ] 让GUI看起来好看点。
- [ ] 尝试加入对图片的base64编码。
- [x] 自动复制加密后的文本。
- [ ] 受推友指点,可以添加一个计算图片哈希的功能,防止平台偷偷在图片中添加追踪水印。
- [x] 受推友指点,可以添加一个计算图片哈希的功能,防止平台偷偷在图片中添加追踪水印,同时也添加比对哈希的功能(现在实际支持任何文件)
116 changes: 95 additions & 21 deletions source/CLI_EN.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import os
import time
import hashlib
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.backends import default_backend
import pyperclip
import colorama
from colorama import Fore, Style

# 初始化 colorama
colorama.init()

current_dir = os.path.dirname(os.path.abspath(__file__))

if not os.path.exists('RSAkeys'):
print('Detected no `RSAkeys` folder, creating it automatically.\n')
print('no path found like RSAKeys, creating\n')
os.makedirs('RSAkeys')

if not os.path.exists('RSAkeys/my'):
Expand All @@ -18,9 +24,21 @@
os.makedirs('RSAkeys/friend')

def generate_key_pair():
input_key_size = input('input key length(1024/2048/4096 default):')
if not input_key_size:
input_key_size = 4096
else:
try:
input_key_size = int(input_key_size)
if input_key_size not in [1024, 2048, 4096]:
print('you can only choose one in 1024/2048/4096')
return
except ValueError:
print('are you inputing numbers?')
return
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=4096,
key_size=input_key_size,
backend=default_backend()
)

Expand All @@ -41,82 +59,138 @@ def generate_key_pair():
with open('RSAkeys/my/public.txt', 'wb') as f:
f.write(public_pem)

print('RSA key pair generated successfully! Saved to RSAkeys/my/public.txt and RSAkeys/my/private.txt')
print('RSA key pair generated, saved at RSAkeys/my/public.txt and RSAkeys/my/private.txt')

def import_public_key():
path = input('Input the path to pubkey file you want to import: ')
path = input('input path of public_key you want import')
if os.path.isfile(path):
with open(path, 'rb') as f:
public_pem = f.read()
with open('RSAkeys/friend/public.txt', 'wb') as fw:
fw.write(public_pem)
print('SUCCESS!')
print('public key import success!')
else:
print('NO FILE DETECTED')
print('not found, check the path')

def encrypt_data():
if os.path.exists('RSAkeys/friend/public.txt'):
with open('RSAkeys/friend/public.txt', 'rb') as f:
public_pem = f.read()
public_key = serialization.load_pem_public_key(public_pem, backend=default_backend())

data = input('Input data you want ro encrypt:').encode('utf-8')
data = input('input the text you want encrypt: ').encode('utf-8')
ciphertext = public_key.encrypt(data, padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(), label=None))
print("Here's your encrypted data: (auto copied)", ciphertext.hex())
print('hex text after encrypting(auto copied): ', ciphertext.hex())
else:
use_own_public_key = input("Your friend's public key was not found. Do you want to use your own public key for encryption? (1=Yes,2=No): ")
if use_own_public_key == '1':
use_own_public_key = input('no imported public_key,Use the key generated? (T=True,F=False): ')
if use_own_public_key == 'T':
with open('RSAkeys/my/public.txt', 'rb') as f:
public_pem = f.read()
public_key = serialization.load_pem_public_key(public_pem, backend=default_backend())

data = input('Input data you want ro encrypt:').encode('utf-8')
data = input('input the text you want encrypt: ').encode('utf-8')
ciphertext = public_key.encrypt(data, padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(), label=None))
print("Here's your encrypted data: (auto copied):", ciphertext.hex())
print('hex text after encrypting(auto copied): ', ciphertext.hex())
else:
print("You didn't import or generate any publickey.")
print('plz import others public_key or generate your key pair')
pyperclip.copy(ciphertext.hex())

def decrypt_data():
with open('RSAkeys/my/private.txt', 'rb') as f:
private_pem = f.read()
private_key = serialization.load_pem_private_key(private_pem, password=None, backend=default_backend())

ciphertext = bytes.fromhex(input('Input data you want DECRYPT:'))
ciphertext = bytes.fromhex(input('input the text you want decrypt: '))

data = private_key.decrypt(ciphertext, padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(), label=None))
print('Here is your data decrypted: ', data.decode('utf-8'))
print('text after decrypting: ', data.decode('utf-8'))

def calculate_hashes():
path = input('input file path: ')
if os.path.isfile(path):
with open(path, 'rb') as f:
data = f.read()

md5_hash = hashlib.md5(data).hexdigest()
sha1_hash = hashlib.sha1(data).hexdigest()
sha256_hash = hashlib.sha256(data).hexdigest()

print('MD5:', md5_hash)
print('SHA1:', sha1_hash)
print('SHA256:', sha256_hash)
else:
print('not found, check path')

def compare_hashes():
path = input('input file path:')
file_hash = input('input HEX hash(MD5/SHA1/SHA256): ')
if os.path.isfile(path):
if file_hash:
with open(path, 'rb') as f:
data = f.read()

md5_hash = hashlib.md5(data).hexdigest()
sha1_hash = hashlib.sha1(data).hexdigest()
sha256_hash = hashlib.sha256(data).hexdigest()

match_found = False
if file_hash == md5_hash:
print(Fore.GREEN + 'MD5 MATCH FOUND:' + Style.RESET_ALL, path)
print(Fore.GREEN + 'with value:' + Style.RESET_ALL, md5_hash)
match_found = True
if file_hash == sha1_hash:
print(Fore.GREEN + 'SHA1 MATCH FOUND:' + Style.RESET_ALL, path)
print(Fore.GREEN + 'with value:' + Style.RESET_ALL, sha1_hash)
match_found = True
if file_hash == sha256_hash:
print(Fore.GREEN + 'SHA256 MATCH FOUND:' + Style.RESET_ALL, path)
print(Fore.GREEN + 'with value:' + Style.RESET_ALL, sha256_hash)
match_found = True
if not match_found:
print(Fore.RED + 'HASH CHECK FAILED, FILE MAY BE TAMPERED' + Style.RESET_ALL)
else:
print('you really inputted hash?')
else:
print('file not found, check path')

def main_menu():
print("----------------")
print("1. Generate RSA key pair.")
print("2. Import public key from others.")
print("3. Encrypt data.")
print("4. Decrypt data.")
print("5. Say Goodbye.")
print("5. Calculate file hash")
print("6. Check file hash")
print("7. Say Goodbye")
print("----------------")

while True:
main_menu()
choice = input('So what is your choice?')
choice = input('what is your choice')

if choice == '1':
generate_key_pair()
time.sleep(3)
time.sleep(1)
elif choice == '2':
import_public_key()
elif choice == '3':
encrypt_data()
input("Press ENTER continue.")
input("press enter to continue...")
elif choice == '4':
decrypt_data()
input("Press ENTER continue.")
input("press enter to continue...")
elif choice == '5':
calculate_hashes()
input("press enter to continue...")
elif choice == '6':
compare_hashes()
input("press enter to continue...")
elif choice == '7':
print('Never gonna tell↓ a↑ liiiie↓ and hurt you.')
break
else:
print('? what did you just press')
input("Press ENTER continue.")
input("press enter to continue...")
80 changes: 77 additions & 3 deletions source/CLI_ZhCN.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import os
import time
import hashlib
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.backends import default_backend
import pyperclip
import colorama
from colorama import Fore, Style

# 初始化 colorama
colorama.init()

current_dir = os.path.dirname(os.path.abspath(__file__))

Expand All @@ -18,9 +24,21 @@
os.makedirs('RSAkeys/friend')

def generate_key_pair():
input_key_size = input('输入密钥长度(1024/2048/默认4096):')
if not input_key_size:
input_key_size = 4096
else:
try:
input_key_size = int(input_key_size)
if input_key_size not in [1024, 2048, 4096]:
print('三个里面选一个,别的不认')
return
except ValueError:
print('?你输的是数字?')
return
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=4096,
key_size=input_key_size,
backend=default_backend()
)

Expand Down Expand Up @@ -90,13 +108,63 @@ def decrypt_data():
algorithm=hashes.SHA256(), label=None))
print('解密后的数据为:', data.decode('utf-8'))

def calculate_hashes():
path = input('请输入文件路径:')
if os.path.isfile(path):
with open(path, 'rb') as f:
data = f.read()

md5_hash = hashlib.md5(data).hexdigest()
sha1_hash = hashlib.sha1(data).hexdigest()
sha256_hash = hashlib.sha256(data).hexdigest()

print('MD5:', md5_hash)
print('SHA1:', sha1_hash)
print('SHA256:', sha256_hash)
else:
print('文件不存在,请检查路径是否正确。')

def compare_hashes():
path = input('请输入文件路径:')
file_hash = input('请输入哈希(MD5/SHA1/SHA256):')
if os.path.isfile(path):
if file_hash:
with open(path, 'rb') as f:
data = f.read()

md5_hash = hashlib.md5(data).hexdigest()
sha1_hash = hashlib.sha1(data).hexdigest()
sha256_hash = hashlib.sha256(data).hexdigest()

match_found = False
if file_hash == md5_hash:
print(Fore.GREEN + 'MD5校验通过:' + Style.RESET_ALL, path)
print(Fore.GREEN + '哈希值:' + Style.RESET_ALL, md5_hash)
match_found = True
if file_hash == sha1_hash:
print(Fore.GREEN + 'SHA1校验通过:' + Style.RESET_ALL, path)
print(Fore.GREEN + '哈希值:' + Style.RESET_ALL, sha1_hash)
match_found = True
if file_hash == sha256_hash:
print(Fore.GREEN + 'SHA256校验通过:' + Style.RESET_ALL, path)
print(Fore.GREEN + '哈希值:' + Style.RESET_ALL, sha256_hash)
match_found = True
if not match_found:
print(Fore.RED + '哈希校验失败,文件可能已被修改' + Style.RESET_ALL)
else:
print('输哈希了吗你就点啊?')
else:
print('文件不存在,请检查路径是否正确。')

def main_menu():
print("----------------")
print("1. 生成RSA密钥对")
print("2. 导入他人公钥")
print("3. 加密数据")
print("4. 解密数据")
print("5. 退出")
print("5. 计算文件哈希值")
print("6. 校验哈希")
print("7. 退出")
print("----------------")

while True:
Expand All @@ -105,7 +173,7 @@ def main_menu():

if choice == '1':
generate_key_pair()
time.sleep(3)
time.sleep(1)
elif choice == '2':
import_public_key()
elif choice == '3':
Expand All @@ -115,6 +183,12 @@ def main_menu():
decrypt_data()
input("按下回车继续...")
elif choice == '5':
calculate_hashes()
input("按下回车继续...")
elif choice == '6':
compare_hashes()
input("按下回车继续...")
elif choice == '7':
print('程序已退出。')
break
else:
Expand Down

0 comments on commit 1168975

Please sign in to comment.