报错AttributeError: 'str' object has no attribute 'copy'
时间: 2024-04-10 10:26:02 浏览: 35
根据提供的引用内容,报错"AttributeError: 'str' object has no attribute 'copy'"是因为在代码中使用了字符串对象的copy方法,而字符串对象没有copy方法。要解决这个问题,你可以使用字符串的切片操作来复制字符串。下面是一个示例代码:
```python
def encrypt(m, k):
k_sub = generate_k_sub(k)
ip_displace = displace(m, IP_substitution) # 初始置换
ipl = [] # L列表
r = [] # R列表
for i in range(0, 16):
l_temp = r[i - 1]
r_temp = xor(l_temp, f(r[i - 1], k_sub[i]))
ipl.append(l_temp)
r.append(r_temp)
result = r[16] + l[16]
c = displace(result, IP_inverse_substitution)
return c
def decrypt(c, k):
k_sub = generate_k_sub(k)
ip_displace = displace(c, IP_substitution) # 初始置换
ipl = [] # L列表
r = [] # R列表
for i in range(0, 16):
l_temp = r[i - 1]
r_temp = xor(l_temp, f(r[i - 1], k_sub[15 - i]))
ipl.append(l_temp)
r.append(r_temp)
result = r[16] + l[16]
m = displace(result, IP_inverse_substitution)
return m
def bin_to_hex(bin_str):
# 将64位二进制字符串分割为每个字节(8位)
bytes_list = [bin_str[i:i+8] for i in range(0, len(bin_str), 8)]
# 每个字节转换为对应的十六进制表示
hex_str = ''.join(format(int(byte, 2), '02X') for byte in bytes_list)
return hex_str
if __name__ == '__main__':
num = int(input().strip())
M = input().strip()[2:]
K = input().strip()[2:]
plaintext = encrypt(M, K)
plaintext = plaintext.zfill(64)
print('0x' + bin_to_hex(plaintext).lower())
```