• 加密


    # coding=utf-8
    
    from Crypto.PublicKey import RSA
    from Crypto.Cipher import PKCS1_OAEP, PKCS1_v1_5
    import base64
    from urllib import parse
    
    import Crypto.Cipher.AES
    import Crypto.Random
    import os
    import sys
    from Crypto.Cipher import AES
    from Crypto.Cipher import AES
    from Crypto import Random
    import binascii
    import hashlib
    
    def decrypt_data(fp,inputdata, code="123456"):
        # URLDecode
        data = parse.unquote(inputdata)
        # base64decode
        data = base64.b64decode(data)
        private_key = RSA.import_key(open(fp+"/my_private_rsa_key.bin").read(),passphrase=code)
        # 使用 PKCS1_v1_5,不要用 PKCS1_OAEP
        # 使用 PKCS1_OAEP 的话,前端 jsencrypt.js 加密的数据解密不了
        cipher_rsa = PKCS1_v1_5.new(private_key)
        # 当解密失败,会返回 sentinel
        sentinel = None
        ret = cipher_rsa.decrypt(data, sentinel)
        return ret
    
    def encrypt_data(fp,content=b"123456"):
        # 加载公钥
        recipient_key = RSA.import_key(open(fp+"/my_rsa_public.pem").read())
        cipher_rsa = PKCS1_v1_5.new(recipient_key)
        en_data = cipher_rsa.encrypt(content)
        c=base64.b64encode(en_data)
        return c
    
    
    fp="/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-1])+"/lib"
    
    en=encrypt_data(fp)
    text = en.decode()  # 待加密文本
    print(text)
    hash = hashlib.md5()
    hash.update(text.encode('utf-8'))
    print(hash.hexdigest())
    
    print("*"*20)
    
    #编码
    encodestr = base64.b64encode(text.encode())
    print(encodestr.decode())
    #解码
    decodestr = base64.b64decode(encodestr)
    print(decodestr.decode())
    
    print("*"*20)
    
    
    print(decrypt_data(fp,"fhZf1TKEk2HiAG3BW/kuhuSXdCvSXzxW29re1OGgtQVUyO++l6KwxorpPQdZRGgu7E4muN5HyCBRBI6QTth+RGaNnlh/7DP8y3SmHjWyiYbXxAGCY5b8Vzog3IAqOZWF1QTrU7Azn+HoMe4MO4MXmH3uNXEi63nUMJlytcEpi14="))
  • 相关阅读:
    HDU 1495 非常可乐
    ja
    Codeforces Good Bye 2016 E. New Year and Old Subsequence
    The 2019 Asia Nanchang First Round Online Programming Contest
    Educational Codeforces Round 72 (Rated for Div. 2)
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises)
    AtCoder Regular Contest 102
    AtCoder Regular Contest 103
    POJ1741 Tree(点分治)
    洛谷P2634 [国家集训队]聪聪可可(点分治)
  • 原文地址:https://www.cnblogs.com/sea-stream/p/11495209.html
Copyright © 2020-2023  润新知