近日,自己在OPENSSL的EVP_CIPHER的基础上,封装了一个通用的加密和解密的DLL,这个DLL可以支持openssl 3.0支持的各种Chiper进行加密和解密。然后利用这个DLL,用Python在编写一段封装功能的函数。
CryptoTool.py
封装并提供2对加密和解密函数:
encryptString_ex/decryptString_ex
encryptString/decryptString
采用的加密算法是aes-128-cbc
'''
Crypto lib encrypting and decrypting program
The dependency:
1. cryptolib.dll wrapped the openssl API
2. libcrypto-3-x64.dll openssl dependency
'''
import ctypes as C
import msvcrt
CIPHER_NAME='aes-128-cbc'
def encryptString_ex(byteofText,byteofpass):
dll = C.cdll.LoadLibrary('cryptolib.dll')
text=C.c_buffer(1024)
text.value=byteofText #it must be bytes
result=C.c_buffer(1024) #init result of encryption or decryption
error=C.c_buffer(1024) #init error message to take back message of encryption or decryption
ok=C.c_int(0) #Init ok to accept the return flag of encryption or decryption
pwd=C.c_buffer(1024)
pwd.value=byteofpass
ok=dll.encryptString_bypassword(CIPHER_NAME.encode(),text,C.byref(result),C.byref(error),pwd,0) #advanced encryption
if(ok==1):
return True,result.value.decode()
else:
return False,