Aldonis secreto.py
This commit is contained in:
parent
9495dd644e
commit
4088f2d593
|
@ -0,0 +1,93 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import binascii, pyperclip, readline, gettext
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Protocol.KDF import PBKDF2
|
||||
from Crypto.Random import get_random_bytes
|
||||
from Crypto.Util.Padding import pad
|
||||
|
||||
t = gettext.translation(
|
||||
'secreto',
|
||||
'/usr/share/locale/',
|
||||
fallback=True,
|
||||
)
|
||||
|
||||
_ = t.gettext
|
||||
|
||||
zwc = ["\u2060", "\u200B", "\u200D", "\u200E",
|
||||
"\u200F", "\u200C", "\u2061", "\u180E",
|
||||
"\u202A", "\u202C", "\u202D", "\u2062",
|
||||
"\u2063", "\u2064", "\u2065", "\u2066"]
|
||||
|
||||
def encrypt(plaintext, password):
|
||||
salt = get_random_bytes(16)
|
||||
key = PBKDF2(password, salt, dkLen=16, count=10000)
|
||||
iv = get_random_bytes(16)
|
||||
cipher = AES.new(key, AES.MODE_CBC, iv)
|
||||
ciphertext = cipher.encrypt(pad(plaintext.encode('utf-8'), 16))
|
||||
encrypted_data = salt + iv + ciphertext
|
||||
return binascii.hexlify(encrypted_data).decode('utf-8')
|
||||
|
||||
def decrypt(hex_ciphertext, password):
|
||||
ciphertext = bytes.fromhex(hex_ciphertext)
|
||||
salt = ciphertext[:16]
|
||||
iv = ciphertext[16:32]
|
||||
encrypted_data = ciphertext[32:]
|
||||
key = PBKDF2(password, salt, dkLen=16, count=10000)
|
||||
cipher = AES.new(key, AES.MODE_CBC, iv)
|
||||
decrypted_data = cipher.decrypt(encrypted_data)
|
||||
secret = decrypted_data.decode('utf-8').strip()
|
||||
return secret
|
||||
|
||||
def hid2hex(hid_string):
|
||||
mappings = {zwc[i]: hex(i)[2:].upper() for i in range(16)}
|
||||
hex_string = "".join(mappings.get(char, "") for char in hid_string)
|
||||
return hex_string
|
||||
|
||||
def hex2hid(hex_string):
|
||||
mappings = {hex(i)[2:].upper(): zwc[i] for i in range(16)}
|
||||
hid_string = "".join(mappings.get(char.upper(), "") for char in hex_string)
|
||||
return hid_string
|
||||
|
||||
def wrap(text):
|
||||
wrapped_text = "\uFEFF" + text + "\uFEFF"
|
||||
return wrapped_text
|
||||
|
||||
def unwrap(text):
|
||||
unwrapped_text = text.strip('\uFEFF')
|
||||
return unwrapped_text
|
||||
|
||||
while True:
|
||||
|
||||
print()
|
||||
print(_(" [1]: Kaŝi mesaĝon en teksto"))
|
||||
print(_(" [2]: Montri kaŝitan mesaĝon"))
|
||||
print(_(" [0]: Eliri"))
|
||||
print()
|
||||
|
||||
choice = input("Secreto:~$ ")
|
||||
if choice == "1":
|
||||
secret = input(_("Sekreta mesaĝo: "))
|
||||
password = input(_("Pasvorto: "))
|
||||
pub_msg = input(_("Publika teksto: "))
|
||||
encrypt_msg = encrypt(secret, password)
|
||||
hiding = hex2hid(encrypt_msg)
|
||||
wrapping = wrap(hiding)
|
||||
result = pub_msg[:len(pub_msg) // 2] + wrapping + pub_msg[len(pub_msg) // 2:]
|
||||
pyperclip.copy(result)
|
||||
print(_("Mesaĝo kaŝita kaj kopiita!"))
|
||||
|
||||
elif choice == "2":
|
||||
result = input(_("Enigu tekston kun mesaĝo: "))
|
||||
password = input(_("Pasvorto: "))
|
||||
extraction = unwrap(result)
|
||||
revealing = hid2hex(extraction)
|
||||
try:
|
||||
secret = decrypt(revealing, password)
|
||||
print(_("Sekreta mesaĝo: "), secret)
|
||||
except:
|
||||
print(_("Vi enigis malĝustan teksto aŭ pasvorton!"))
|
||||
|
||||
elif choice == "0":
|
||||
quit()
|
||||
|
Loading…
Reference in New Issue