超长文本加密 :param message: 需要加密的字符串 :param public_key 公钥 :param length: 1024bit的证书用100, 2048bit的证书用 200 :return: 加密后的数据
(message, public_key: str | None = None, length=200)
| 98 | |
| 99 | |
| 100 | def rsa_long_encrypt(message, public_key: str | None = None, length=200): |
| 101 | """ |
| 102 | 超长文本加密 |
| 103 | |
| 104 | :param message: 需要加密的字符串 |
| 105 | :param public_key 公钥 |
| 106 | :param length: 1024bit的证书用100, 2048bit的证书用 200 |
| 107 | :return: 加密后的数据 |
| 108 | """ |
| 109 | if public_key is None: |
| 110 | public_key = get_key_pair().get('key') |
| 111 | |
| 112 | cipher = _get_encrypt_cipher(public_key) |
| 113 | |
| 114 | if len(message) <= length: |
| 115 | result = base64.b64encode(cipher.encrypt(message.encode('utf-8'))) |
| 116 | else: |
| 117 | rsa_text = [] |
| 118 | for i in range(0, len(message), length): |
| 119 | cont = message[i:i + length] |
| 120 | rsa_text.append(cipher.encrypt(cont.encode('utf-8'))) |
| 121 | cipher_text = b''.join(rsa_text) |
| 122 | result = base64.b64encode(cipher_text) |
| 123 | |
| 124 | return result.decode() |
| 125 | |
| 126 | |
| 127 | @lru_cache(maxsize=2) |
no test coverage detected