Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from base64 import b64encode
- from typing import Union
- from Crypto.PublicKey import RSA
- from Crypto.Cipher import PKCS1_v1_5
- pub_key = """-----BEGIN PUBLIC KEY-----
- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiqrhXVeGCMp6024Eeb0J+t8OlnoMNFeG
- sM7svvlBIgkUAZM42B1nVeRtSqKgT06UqFWrormMH62RHGnDYlA+r08RUDVnwvIFIXiyFngaX91M
- e474yk+SESvzFKyVSLCutAr3QF4Roa7qFxANEPeW/vP7nd34uktEfomWxi69honNokjpfYRIry7e
- Y42IeKugX7uEVdz+m+3Y5G1vMabEp2UvG8DCNH4cpMm5z69FINdHSA79WhUgl5ncV3jHYFFEDejX
- AcV717eyr1LOScjuxHY/D5SslV+J4CQQmaXTP+AVa9xLYCUbyXS8ps9/w+yKdXJK60MD+Nm+y2rZ
- bbKpfQIDAQAB
- -----END PUBLIC KEY-----"""
- def encrypt_username(username: Union[str, bytes], pubkey: Union[str, bytes], encoding='utf-8'):
- if isinstance(username, str):
- username = username.encode(encoding)
- key = RSA.import_key(pubkey)
- rsa = PKCS1_v1_5.new(key)
- encrypted_data = rsa.encrypt(username)
- return b64encode(encrypted_data).decode(encoding)
- if __name__ == '__main__':
- print(encrypt_username(
- 'c-yejunan',
- pub_key
- ))
- pass
Add Comment
Please, Sign In to add comment