Python中的DES加密

问题描述 投票:0回答:1

pypyododome工作正常,但出现错误消息。

这是我的代码:

from Crypto.Cipher import DES

key = 'hello123'

def pad(text):
        while len(text) % 8 != 0:
            text += ''
        return text

des = DES.new(key, DES.MODE_ECB)

text1 = 'Python is the Best Language!'

padded_text = pad(text1)

encrypted_text = des.encrypt(padded_text)

print(encrypted_text)

print(des.decrypt(encrypted_text))

这是我的错误消息:

追踪(最近通话):第10行中的文件“ C:\ Users \ Raj_7 \ Desktop \ des.py”des = DES.new(key,DES.MODE_ECB)

文件“ D:\ Python \ lib \ site-packages \ Crypto \ Cipher \ DES.py”,第145行,在新返回_create_cipher(sys.modules [name],密钥,模式,* args,** kwargs]

文件“ D:\ Python \ lib \ site-packages \ Crypto \ Cipher__init __。py”,行_create_cipher中的第79行返回模式[模式](工厂,** kwargs)

文件“ D:\ Python \ lib \ site-packages \ Crypto \ Cipher_mode_ecb.py”,第215行,在_create_ecb_cipher中cipher_state = factory._create_base_cipher(kwargs)

文件“ D:\ Python \ lib \ site-packages \ Crypto \ Cipher \ DES.py”,行_create_base_cipher中的第76行结果= start_operation(c_uint8_ptr(key),

文件“ D:\ Python \ lib \ site-packages \ Crypto \ Util_raw_api.py”,第234行,在c_uint8_ptr中

raise TypeError(“对象类型%s无法传递给C代码”%type(data))TypeError:无法将对象类型传递给C代码

python-3.x des
1个回答
0
投票

如果我使用bytes而不是字符串,则在这里有效

from Crypto.Cipher import DES

def pad(text):
    n = len(text) % 8
    return text + (b' ' * n)


key = b'hello123'
text1 = b'Python is the Best Language!'

des = DES.new(key, DES.MODE_ECB)

padded_text = pad(text1)
encrypted_text = des.encrypt(padded_text)

print(encrypted_text)
print(des.decrypt(encrypted_text))
© www.soinside.com 2019 - 2024. All rights reserved.