初始化加密工具
(self, password, algorithm='AES128')
| 23 | ITER_COUNT = 7 |
| 24 | |
| 25 | def __init__(self, password, algorithm='AES128'): |
| 26 | """初始化加密工具""" |
| 27 | self.password = password |
| 28 | self.algorithm = algorithm.upper() if algorithm else 'AES128' |
| 29 | |
| 30 | # 根据算法确定密钥长度 |
| 31 | if self.algorithm == 'AES128': |
| 32 | self.key_length = 16 |
| 33 | elif self.algorithm == 'AES256': |
| 34 | self.key_length = 32 |
| 35 | elif self.algorithm == 'DES': |
| 36 | self.key_length = 8 |
| 37 | raise ValueError("DES算法已弃用,请使用AES128或AES256") |
| 38 | else: |
| 39 | raise ValueError(f"不支持的加密算法: {algorithm}") |
| 40 | |
| 41 | # 生成密钥 |
| 42 | self.key = self._derive_key(password) |
| 43 | |
| 44 | def _derive_key(self, password): |
| 45 | """从密码派生密钥""" |
nothing calls this directly
no test coverage detected