生成IPv6后缀

2024-10-03 11:21:59 发布

您现在位置:Python中文网/ 问答频道 /正文

请在IPv6项目上工作:我必须使用哈希函数、XOR和DES加密生成加密的IPv6,以生成IPv6后缀地址,我可以将其添加到已知前缀以形成IPv6地址,因此我的代码为:

import hashlib
from datetime import datetime
from pyDes import *
import ipaddress

# This is the source IPv6 Address SA :
SA = "200104701f1300ae0000000000000010".encode()

# Here is the HASH for the SA to get the H_SA :
HA_MD5 = hashlib.md5()
HA_MD5.update(SA)
H_SA = HA_MD5.digest()
print("The hashed IPv6 @ H_SA is : ", H_SA, len(H_SA))
# output is : The hashed IPv6 @ H_SA is :  b'\r\xee\x0f\tJb\x08\xee\x02\x1c\x04nE\xfe\x16\xfa' 16

# Generating the timestamp to form the salt :
now = datetime.now()
timestamp = str(datetime.timestamp(now)).encode()
print("The timestamp is : ", timestamp, len(timestamp))
# output is : The timestamp is :  b'1616255227.435759' 17

# The XOR function :
def xor_bytes(H_SA, timestamp):
    lenght = min(len(H_SA), len(timestamp))
    return bytes([H_SA[i] ^ timestamp[i] for i in range(lenght)])

# XOR the timestamp (salt) with the H_SA to get the P_SA:
P_SA = xor_bytes(H_SA, timestamp)
print("The XOR of H_SA & timestamp is P_SA : ", P_SA, len(P_SA))
# output is : The XOR of H_SA & timestamp is P_SA :  b'<\xd8>?xW=\xdc0+*Zv\xcb!\xcf' 16

# Encrypt the P_SA with DES encryption :
key = "IoT_IPv6"
IV = bytes([0]*8)
ENCRYPT_DES = des(key, CBC, IV, pad=None, padmode=PAD_PKCS5)
DA_SFX = ENCRYPT_DES.encrypt(P_SA)
print(DA_SFX, len(DA_SFX))
# output is : b'\xe3\xf0\xec\x97\x85=M\x95\x00\xc14\x9aw\xa2\x8b\xb9\xb0y\x1eY\xdf\r\xc1,' 24

我的问题是:在知道必须保留所有DA_SFX的情况下,如何从最后的输出(DA_SFX)生成后缀IPv6地址,以允许目标服务器反转所有进程


Tags: theimportoutputdatetimelenissatimestamp