相同的代码,不同的错误:Python init()

2024-09-29 19:35:05 发布

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

我和我的朋友正在为一个大学项目工作。 试图执行完全相同的代码,我得到一个错误,他没有

我的错误是:TypeError: __init__() missing 1 required positional argument: 'backend'

我认为这是Python版本的问题,但实际上是同一个版本。有什么想法吗

enter image description here

文件1:

from pwn import *
from binascii import hexlify,unhexlify
import ciphersuite_aesnotrand as ciphersuite

port = input()

r = remote("localhost", port)

print(r.recvline().decode())
print(r.recvline().decode())

r.sendline('0')

print(r.recvline().decode())

m = hexlify(b'Test message 12!').decode()
r.sendline(m)

cph = r.recvline()
print("Received ciphertext:")
print(cph)

print("\n")

# Security parameter (fixed)
KEYLEN = 16
# Contol final decision
result = 1

for i in range(16777216): # 2^3^8 possible keys
    
    keyNumberI = bytearray(b'\x00'*(KEYLEN-3))
    keyNumberI.extend((i).to_bytes(3, byteorder = 'big'))
    
    if(i==1000000):
        print("1 milhão de chaves testadas.")
    if(i==2000000):
        print("2 milhões de chaves testadas.")
    if(i==3000000):
        print("3 milhões de chaves testadas.")
    if(i==4000000):
        print("4 milhões de chaves testadas.")
    if(i==5000000):
        print("5 milhões de chaves testadas.")
    if(i==6000000):
        print("6 milhões de chaves testadas.")
    if(i==7000000):
        print("7 milhões de chaves testadas.")
    if(i==8000000):
        print("8 milhões de chaves testadas.")
    if(i==9000000):
        print("9 milhões de chaves testadas.")
    if(i==10000000):
        print("10 milhões de chaves testadas.")
    if(i==11000000):
        print("11 milhões de chaves testadas.")
    if(i==12000000):
        print("12 milhões de chaves testadas.")
    if(i==13000000):
        print("13 milhões de chaves testadas.")
    if(i==14000000):
        print("14 milhões de chaves testadas.")
    if(i==15000000):
        print("15 milhões de chaves testadas.")
    if(i==16000000):
        print("16 milhões de chaves testadas.")
    
    cphTest = ciphersuite.enc(keyNumberI, unhexlify(m))
    
    if(cphTest==cph[:-1]):
        mTest = ciphersuite.dec(keyNumberI, cph[:-1])
        print("Mensagem resultante: ")
        print(mTest)
        result = 0
        break

r.sendline('1')

print(r.recvline())

if(result==1):
    print(1)
    r.sendline('1')
else:
    print(0)
    r.sendline('0')

print(r.recvline().decode())

r.close()

这是一个服务器/客户端程序。但是错误来自文件2

文件2:

# Python Module ciphersuite
import os
import random
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

# Security parameter (fixed)
KEYLEN = 16

# Use crypto random generation to get a key with up to 3 random bytes
def gen(): 
    sysrand = random.SystemRandom()
    offset = sysrand.randint(1,3)
    key = bytearray(b'\x00'*(KEYLEN-offset)) 
    key.extend(os.urandom(offset))
    return bytes(key)

def enc(k, m):
    cipher = Cipher(algorithms.AES(k), modes.ECB())
    encryptor = cipher.encryptor()
    cph = b""
    cph += encryptor.update(m)
    cph += encryptor.finalize()
    return cph

def dec(k, c):
    cipher = Cipher(algorithms.AES(k), modes.ECB())
    decryptor = cipher.decryptor()
    msg = b""
    msg += decryptor.update(c)
    msg += decryptor.finalize()
    return msg

谢谢:)


Tags: importifesdeprintdecodesendlinecph
2条回答

加密技术将“后端”从3.1版的强制参数更改为可选参数。当然,最好使用最新版本,尤其是对于加密库。如果由于某种原因,您无法升级到最新版本,那么您可以进行一些小的更改来修复它

您可以通过调用帮助来检查哪些参数是必需的:

>>> from cryptography.hazmat.primitives.ciphers import Cipher
>>> help(Cipher.__init__)
Help on function __init__ in module cryptography.hazmat.primitives.ciphers.base:

__init__(self, algorithm, mode, backend)
    Initialize self.  See help(type(self)) for accurate signature.

从加密文档: https://cryptography.io/en/latest/hazmat/backends/index.html#getting-a-backend

Cryptography was designed to support multiple cryptographic backends, but consumers rarely need this flexibility. Starting with version 3.1 backend arguments are optional and the default backend will automatically be selected if none is specified.

On older versions you can get the default backend by calling default_backend().

因此,您可以更改file2以包括默认后端:

# Python Module ciphersuite
import os
import random
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
# get the default backend
from cryptography.hazmat.backends import default_backend

# Security parameter (fixed)
KEYLEN = 16

# Use crypto random generation to get a key with up to 3 random bytes
def gen(): 
    sysrand = random.SystemRandom()
    offset = sysrand.randint(1,3)
    key = bytearray(b'\x00'*(KEYLEN-offset)) 
    key.extend(os.urandom(offset))
    return bytes(key)

def enc(k, m):
    cipher = Cipher(algorithms.AES(k), modes.ECB(), default_backend())
    encryptor = cipher.encryptor()
    cph = b""
    cph += encryptor.update(m)
    cph += encryptor.finalize()
    return cph

def dec(k, c):
    cipher = Cipher(algorithms.AES(k), modes.ECB(), default_backend())
    decryptor = cipher.decryptor()
    msg = b""
    msg += decryptor.update(c)
    msg += decryptor.finalize()
    return msg

我相信这是加密技术的版本问题。试一试

pip install cryptography upgrade

编辑:如果没有,请尝试卸载pip

相关问题 更多 >

    热门问题