对需要加密的明文进行填充补位 @param text: 需要进行填充补位操作的明文 @return: 补齐明文字符串
(cls, text)
| 9 | |
| 10 | @classmethod |
| 11 | def encode(cls, text): |
| 12 | """ |
| 13 | 对需要加密的明文进行填充补位 |
| 14 | @param text: 需要进行填充补位操作的明文 |
| 15 | @return: 补齐明文字符串 |
| 16 | """ |
| 17 | text_length = len(text) |
| 18 | # 计算需要填充的位数 |
| 19 | amount_to_pad = cls.block_size - (text_length % cls.block_size) |
| 20 | if amount_to_pad == 0: |
| 21 | amount_to_pad = cls.block_size |
| 22 | # 获得补位所用的字符 |
| 23 | pad = to_binary(chr(amount_to_pad)) |
| 24 | return text + pad * amount_to_pad |
| 25 | |
| 26 | @classmethod |
| 27 | def decode(cls, decrypted): |
no test coverage detected