Python替换16进制

# -*- coding: utf-8 -*-

from Crypto import Random
from Crypto.PublicKey import RSA
import binascii

def ByteToHex( bins ):
    """
    Convert a byte string to it's hex string representation e.g. for output.
    """
    return ''.join( [ "%02X" % x for x in bins ] ).strip()

def HexToByte( hexStr ):
    """
    Convert a string hex byte values into a byte string. The Hex Byte values may
    or may not be space separated.
    """
    return bytes.fromhex(hexStr)

def generate():
    random_generator = Random.new().read
    rsa = RSA.generate(1024, random_generator)
    private_pem = rsa.exportKey()
    with open('private.pem', 'wb') as prikey:
        prikey.write(private_pem)
    with open('private.pem', 'rb') as prikey:
        rsa=RSA.importKey(prikey.read())
        data=rsa.publickey().exportKey()
        with open('public.pem','wb') as pubkey:
            pubkey.write(data)

def str_to_hex(s):
    return ' '.join([hex(ord(c)).replace('0x', '') for c in s])

if __name__ == '__main__':
    pubkey = b'''-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEfqttNKA2ZkBkcFIBwd0+NmWI
+9E3128tp0cquT5NToxvMh4clyu1sWSouR7xxkXnTM2lqAF0Yt81iWOz13C7CNi7
tUoveN5iGqes3aLlj7Ee0T3SiEI1LISPRP4iy60lIBuG9ICi+dxREoVEdj6ub5zL
tG3idI93u4MI9APdAwIDAQAB
-----END PUBLIC KEY-----'''
    #generate()
    pubkey1=binascii.hexlify(pubkey)

    with open ('public.pem','rb') as file:
        data=file.read()
        pubkey2=binascii.hexlify(data)

    with open ("ObjectModule.dll","rb") as file1:
        data=(file1.read())
        data=binascii.hexlify(data)
        data.replace(pubkey1,pubkey2)
        with open("ObjectModule_new.dll", "wb") as file2:
            file2.write(data)

  

posted @ 2020-03-23 22:27  heycomputer  阅读(22)  评论(0)    收藏  举报