zoukankan      html  css  js  c++  java
  • 加密

    # 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="))
  • 相关阅读:
    CDOJ 1270 Playfair(模拟)
    HDU 2209 翻纸牌游戏(DFS)
    HDU 1241 Oil Deposits(DFS)
    pta 01-复杂度2 Maximum Subsequence Sum (25分)
    poj 1469 COURSES 二分匹配 dfs
    01-复杂度1 最大子列和问题 (20分)分治
    poj 1325 Machine Schedule 二分图匹配+DFS实现
    zoj 1654 Place the Robots 二分图匹配DFS实现
    图论笔记-第七章
    hdu 5423 Rikka with Tree DFS
  • 原文地址:https://www.cnblogs.com/sea-stream/p/11495209.html
Copyright © 2011-2022 走看看