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RroXTS39PZMbGcdCZ
eGhVT2PEewws13Z3SNsNFtt+Px6pFZhddG1XLv93AoGBAIgilae7PfHMFdaIzE0V
1qk90JrT5gLieVyC0H1OTPl+J5lTYR79Tb0J7GC/MOZChi0p25duUUv3V3AdeaTi
iRinBHWR8Pzon7jgVvD+jbaqjj6KkEmqluAc67fvTIgk3ZlIoFPxb/gQb8qef00v
Fmj1LWwtb+N29ruI3f+k7gKrAoGBAI6c5duzJOzlsuFoIE3m5I8Lj8zI4JZDxBPV
spQqyamGUg1JU31za49yjDN/3B7Ch5TsGVQSE0vgYYiGMZxYSBC1g/0la1Qfv7a0
O4HRxlmF/5lyIy7OEhiTCj/PVFnZJC2GGImX7AmNwBPbWw75HoKHl87BIfEa1SwZ
wVYBjksvAoGBAJHazTKGMZbi8p4rq9feUy4beLfySHVdmBDJquDEmJvzRtegzioX
OKfOEBbsktRfwKTzCVQrqY4P0t2jZB19fUkMBzrmwcsAzlOQVQQsHnqWVFNIj51l
+fSN3trj0Hd+KKAA5oXSzWNYrJ7fKDNIkU/f5rzvMlDM14GZ39dVELs+
-----END RSA PRIVATE KEY-----
"""

from Cryptodome.PublicKey import RSA
import rrsa, base64
import sys

if len(sys.argv) != 2:
    print('./tool_encrypt_cfg.py <plain.cfg>')
    exit(1)
with open(sys.argv[1], 'r') as f:
    plain = f.read()

priv = RSA.importKey(priv_pem.strip())
encrypted_bytes = rrsa.encryptDecryptBytes(rrsa._decrypt, priv, plain.encode('utf-8'))
encoded = base64.b64encode(encrypted_bytes)
final_b64str = str(encoded)[2:-1]
def insert_newlines(string, every=64):
    lines = []
    for i in range(0, len(string), every):
        lines.append(string[i:i+every])
    return '\n'.join(lines)
final_b64str = insert_newlines(final_b64str)
print(final_b64str)
