pyhcrypt
A simple and secure password-based encryption & decryption algorithm based on hash functions, implemented solely based on python.
Usage
Python
Main API
encrypt(input, password, use_rand=True)
Encrypt input (either bytes or an opened file object) with password (either bytes or str), return the encrypted result (bytes). If input is an opened file object, this function behaves as a generator and encrypts in a 64 bytes by 64 bytes manner to be memory friendly.
If use_rand is set to True (default), 64 random bytes will be encrypted before processing input, and the encryption result is also 64 bytes longer than input. This helps protect the password, especially for cases when the input starts with a specific pattern.
decrypt(input, passwd, use_rand=True)
Decrypt input (either bytes or an opened file object) with password (either bytes or str), return the decryption result (bytes). use_rand shall be consistent with the setting of encrypt.
Example
Encrypt and decrypt bytes.
from pyhcrypt import encrypt, decrypt
from os import urandom
bytes_ori = urandom(64)
passwd = "a_password"
bytes_enc = encrypt(bytes_ori, passwd)
bytes_dec = decrypt(bytes_enc, passwd)
print(bytes_ori == bytes_dec)
Encrypt or decrypt a file.
from pyhcrypt import encrypt, decrypt
def handle(cmd, passwd, srcf, rsf):
func = decrypt if cmd.find("d") >= 0 else encrypt
with open(srcf, "rb") as rf, open(rsf, "wb") as wf:
for _ in func(rf, passwd):
wf.write(_)
Command Line Interface
Execute either
python -m pyhcrypt action password input_file output_file
or
pyhcrypt action password input_file output_file
where action can be either e (for encryption) or d (for decryption), password is the password, input_file and output_file are the corresponding input and output file respectively.
Performance
Test with CPython 3.9.7, using a single Intel Core M3-7Y30 CPU thread. Even though python is slower than C, this library still achieves encryption/decryption speeds of 25.6 MB/s.
A puzzle
We encrypt a UTF-8 encoded message with the encrypt function for 16 individual times with the below code.
from gzip import compress
from pyhcrypt import encrypt
def puzzle(msg, n):
gz_msg = compress(msg)
for i in range(n):
print(encrypt(gz_msg, msg, use_rand=True))
Following are the encryption results.
b"\xde]\xdf\x91+\xfc\xef\x99=\xc6\xbe\x19\xb0\x168\xc7i\xc9\xc5\xb1\x87\xdb\x9b'(\xfb\x13\xfdo\xb6{[A\xb8*\xd2\xb7\x05\x8b>7o\x0f\xd8>F\x9e\xbb`\xf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T\xa9\xcc\x96qnk\xa9\xa8&\xb0#E\xc7\xe8\x81x\xdf}<\x82\x837j\xef'
b"\x8eyv)\xb1\x19\x04\x90\x1e\xb9\xa8ri\xe3\x83\x15\xba\xf0\x83-\x07\xc1$<\x80\x04\x90\xb0d.\x16\x91\x15\xdbsj\xf4H\xa2/\xd3\x85\xde\x02\xf3\xb8f\x06\xd9\x93B\x85\xfbl\xa6\xef\xa1\x19+G\\\xb3\\\x9aG\xd9p\xc6\xf0\xc7\x18\x10\xb3\[email protected]\xc7\xbd\x1a\xf2\x05\xef\r>h\x112\xa6+\xfe\xef\xb4hKa\xe0\x1e\x08W\xda\xa4'\xe2[\xaer\xd1\xf2\xe9\x8b\xc9\xdf\xb0\xef\xe1A\xceyu\xa5<\xb7\x07\xd17\xc3s8\x03\xb3\xf3U\x19\x16\xfb\xbdrrNh{\xadB\xe9G\xaf8\x0c\x9a\xfc\xada\xef\x05\xce\xd9`,&\xedI\x89>0\xe0\xdf\x90=i\xe0\xfeD\x01\x89\xc3b#+\xa6\xda\x10"
b"\x05\x89k_\xfd\x89|\\Q\xeb\x98\xde\x80\xbek\xbf\x1f\xca\x0e\x04\xd9_\xf8L\x1dM[\x147\x0epb\xc2&\xd4\xf8$\xeea\xdd+_\xfca\x8f\xc3\xc4\x0e\xc2\xa8'\x15\xaf\xb1\xf9\xf4\xa59\x131\xc8\x9e;\x17[\xd6\x9e\x0f\x8c\x86\xb6\xb8?\xc6\xcf\xe6\x8c\xb6=\xd7t\x860\xf0\xe0\xe2J\x1cH\xc0\xa7\xbf_\xf3ae\xc8\xafJs\xb1\xe2\x9d\xf6\xae\x92\xf7\xfd\xa4\xadf\xa8\x8d%id\x14K\x81\xa3\xf2\n4T\xb7\xb9Hr\x04\xdd\x93U7'\xff\x89\xd5\x90?MS\x14L\\\xcb+\x92o:\xa8!Q;\xb7\xd6\x06\xbe\x8c\x14h\xc4\xddS\xfb6\x932\x93(\xe95&\x96\xddzU\x05\xa1\xc3+\x15\xd5"
b'=\x05\xc9\xf7@\x84\xb7\xae\x8d\xac^\xcbtm\x81+\xb7Yf\xb4uh\xffq\xd1+q\xb6Zb\x9a\x07\xddky\xd7\x85\xdf\x1d\xd1J\x9d\xdb\xa9\xa5qH"Z&[\xdc\x14Q~\xff\xe4;-\x17+\xdb\x1e\x1a\x80=F\xbfO\r\xbd\xc2x\xa5E0,8\x15\x1ee\xa5YS\x84\xeb\x84\x84\x91\xca\xde\xe34Qa\x1e\xf3\x1d\xd1+S\xfd6\xfcH\xdf\xa9\x88\xae\x1b\xa4AI\xadL\x99D\xe4v3;\xc7\xee\xfa\x1a\x1948\xb9(\xe3\xfa\xcd\':=\'\xe5Y\xec\x83\xde\xe2u6\x1aN\xd5\x82\xe3\x913Q1+\xc7\x98V\xa7\xd4G\xdd0\xc2\x0b\xbe\xb72;\xa3.c~T\x15\xaa\xa7\xdah\xe5w\xb6'
Can you figure out the original message?