Python调用-基于OpenSSL自己封装对称加密的DLL

近日,自己在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 Fals
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值