解密数据
(self, data)
| 85 | raise Exception(f"加密失败: {str(e)}") |
| 86 | |
| 87 | def decrypt(self, data): |
| 88 | """解密数据""" |
| 89 | try: |
| 90 | # 读取IV长度 |
| 91 | iv_len = data[0] |
| 92 | # 读取IV |
| 93 | iv = data[1:1+iv_len] |
| 94 | # 读取密文 |
| 95 | ciphertext = data[1+iv_len:] |
| 96 | |
| 97 | # 创建解密器 |
| 98 | if self.algorithm.startswith('AES'): |
| 99 | cipher = Cipher( |
| 100 | algorithms.AES(self.key), |
| 101 | modes.CBC(iv), |
| 102 | backend=default_backend() |
| 103 | ) |
| 104 | else: |
| 105 | raise ValueError(f"不支持的算法: {self.algorithm}") |
| 106 | |
| 107 | decryptor = cipher.decryptor() |
| 108 | |
| 109 | # 解密 |
| 110 | padded_data = decryptor.update(ciphertext) + decryptor.finalize() |
| 111 | |
| 112 | # 去除填充 |
| 113 | unpadder = padding.PKCS7(128).unpadder() |
| 114 | plaintext = unpadder.update(padded_data) |
| 115 | plaintext += unpadder.finalize() |
| 116 | |
| 117 | return plaintext |
| 118 | except Exception as e: |
| 119 | if "Bad" in str(e) or "Invalid" in str(e): |
| 120 | raise Exception("密码错误或数据损坏") |
| 121 | raise Exception(f"解密失败: {str(e)}") |
| 122 |
no outgoing calls
no test coverage detected