Pandaaaa906

帆软report用户名加密

Oct 19th, 2022
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. from base64 import b64encode
  2. from typing import Union
  3.  
  4. from Crypto.PublicKey import RSA
  5. from Crypto.Cipher import PKCS1_v1_5
  6.  
  7. pub_key = """-----BEGIN PUBLIC KEY-----
  8. MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiqrhXVeGCMp6024Eeb0J+t8OlnoMNFeG
  9. sM7svvlBIgkUAZM42B1nVeRtSqKgT06UqFWrormMH62RHGnDYlA+r08RUDVnwvIFIXiyFngaX91M
  10. e474yk+SESvzFKyVSLCutAr3QF4Roa7qFxANEPeW/vP7nd34uktEfomWxi69honNokjpfYRIry7e
  11. Y42IeKugX7uEVdz+m+3Y5G1vMabEp2UvG8DCNH4cpMm5z69FINdHSA79WhUgl5ncV3jHYFFEDejX
  12. AcV717eyr1LOScjuxHY/D5SslV+J4CQQmaXTP+AVa9xLYCUbyXS8ps9/w+yKdXJK60MD+Nm+y2rZ
  13. bbKpfQIDAQAB
  14. -----END PUBLIC KEY-----"""
  15.  
  16.  
  17. def encrypt_username(username: Union[str, bytes], pubkey: Union[str, bytes], encoding='utf-8'):
  18.     if isinstance(username, str):
  19.         username = username.encode(encoding)
  20.     key = RSA.import_key(pubkey)
  21.     rsa = PKCS1_v1_5.new(key)
  22.     encrypted_data = rsa.encrypt(username)
  23.     return b64encode(encrypted_data).decode(encoding)
  24.  
  25.  
  26. if __name__ == '__main__':
  27.     print(encrypt_username(
  28.         'c-yejunan',
  29.         pub_key
  30.     ))
  31.     pass
  32.  
Add Comment
Please, Sign In to add comment