MCPcopy Create free account
hub / github.com/MearaY/StegaPy / encrypt

Method encrypt

StegaPy/util/crypto_util.py:55–85  ·  view source on GitHub ↗

加密数据

(self, data)

Source from the content-addressed store, hash-verified

53 return kdf.derive(password.encode())
54
55 def encrypt(self, data):
56 """加密数据"""
57 try:
58 # 生成随机IV
59 iv = os.urandom(16)
60
61 # 创建加密器
62 if self.algorithm.startswith('AES'):
63 cipher = Cipher(
64 algorithms.AES(self.key),
65 modes.CBC(iv),
66 backend=default_backend()
67 )
68 else:
69 raise ValueError(f"不支持的算法: {self.algorithm}")
70
71 encryptor = cipher.encryptor()
72
73 # 填充数据
74 padder = padding.PKCS7(128).padder()
75 padded_data = padder.update(data)
76 padded_data += padder.finalize()
77
78 # 加密
79 ciphertext = encryptor.update(padded_data) + encryptor.finalize()
80
81 # 返回: IV长度(1字节) + IV + 加密数据
82 result = bytes([len(iv)]) + iv + ciphertext
83 return result
84 except Exception as e:
85 raise Exception(f"加密失败: {str(e)}")
86
87 def decrypt(self, data):
88 """解密数据"""

Callers 2

embed_dataMethod · 0.95
embed_dataFunction · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected