Python 调用CAPICOM 读取 USB-KEY数字证书

该博客介绍了如何使用Python调用CAPICOM库来打开证书存储区,查找并获取USB-KEY中的数字证书信息,包括颁发者、接收者、电子邮件等,并进行数据签名和签名验证的操作。

 

 

import win32com.client

store = win32com.client.Dispatch('CAPICOM.Store')

# 打开证书存储区

# 2 -> CAPICOM_CURRENT_USER_STORE

# 0 -> CAPICOM_STORE_OPEN_READ_ONLY

store.Open(2, "My", 0)

 

# 查找有效证书

# 12 -> CAPICOM_CERTIFICATE_FIND_KEY_USAGE

# 0x00000080 -> CAPICOM_DIGITAL_SIGNATURE_KEY_USAGE

# 9 -> CAPICOM_CERTIFICATE_FIND_TIME_VALID

# 6,6 -> CAPICOM_CERTIFICATE_FIND_EXTENDED_PROPERTY,CERT_KEY_SPEC_PROP_ID

stores = store.Certificates.Find(12,0x00000080).Find(9).Find(6,6)

 

stores = store.Certificates.Find(0,szThumbprint) # szThumbprint -> 按缩略图查询

# 0 -> CAPICOM_CERTIFICATE_FIND_SHA1_HASH

# 找到证书计数

stores.Count

 

# 获取证书信息

 

for data in stores:

    data.GetInfo(0) # 颁发给

    data.GetInfo(1) # 颁发者

 &nbs

from Crypto.Cipher import AES from Crypto.Random import get_random_bytes import os def encrypt_lsp_file(input_path, output_path, key=None): """ 加密.LSP文件生成乱码文件 参数: input_path: 原始.LSP文件路径 output_path: 加密后的乱码文件路径 key: 可选的密钥(16/24/32字节),默认自动生成 """ # 读取原始文件内容 with open(input_path, 'rb') as f: plaintext = f.read() # 生成随机密钥(如果未提供) key = key or get_random_bytes(32) # 生成随机初始化向量(IV) iv = get_random_bytes(16) # 创建AES加密器(CFB模式) cipher = AES.new(key, AES.MODE_CFB, iv) # 加密内容(自动处理填充) ciphertext = cipher.encrypt(plaintext) # 将IV和密文写入新文件 with open(output_path, 'wb') as f: f.write(iv + ciphertext) return key # 返回密钥用于后续解密 def decrypt_lsp_file(input_path, output_path, key): """ 解密乱码文件恢复原始内容 参数: input_path: 加密文件路径 output_path: 解密后的文件路径 key: 加密时使用的密钥 """ # 读取加密文件 with open(input_path, 'rb') as f: data = f.read() # 分离IV和密文 iv = data[:16] ciphertext = data[16:] # 创建AES解密器 cipher = AES.new(key, AES.MODE_CFB, iv) # 解密内容 plaintext = cipher.decrypt(ciphertext) # 写入解密后的文件 with open(output_path, 'wb') as f: f.write(plaintext) # 使用示例 if __name__ == "__main__": # 原始文件和加密文件路径 original_file = "example.lsp" encrypted_file = "encrypted.lsp" decrypted_file = "decrypted.lsp" # 加密文件(保留原始文件example.lsp不变) secret_key = encrypt_lsp_file(original_file, encrypted_file) print(f"加密完成!密钥请妥善保存: {secret_key.hex()}") print(f"原始文件: {original_file} (未修改)") print(f"加密文件: {encrypted_file} (乱码内容)") # 解密示例(需要时使用) # decrypt_lsp_file(encrypted_file, decrypted_file, secret_key) 改成“.lsp文件加密工具.vbs”
最新发布
10-19
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值