如何解密在Pycrypto中使用Blowfish?(How to decrypt using Blowfish in Pycrypto?)

编程入门 行业动态 更新时间:2024-10-28 00:16:13
如何解密在Pycrypto中使用Blowfish?(How to decrypt using Blowfish in Pycrypto?)

我发现了一个加密数据的例子,但我无法找到解密的例子。

加密示例:

>>> from Crypto.Cipher import Blowfish >>> from Crypto import Random >>> from struct import pack >>> >>> bs = Blowfish.block_size >>> key = b'An arbitrarily long key' >>> iv = Random.new().read(bs) >>> cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv) >>> plaintext = b'docendo discimus ' >>> plen = bs - divmod(len(plaintext),bs)[1] >>> padding = [plen]*plen >>> padding = pack('b'*plen, *padding) >>> msg = iv + cipher.encrypt(plaintext + padding)

我没有找到关于如何解密的例子。

I found one example to encrypt the data but I am unable to find any example on how to decrypt it.

Encryption Example:

>>> from Crypto.Cipher import Blowfish >>> from Crypto import Random >>> from struct import pack >>> >>> bs = Blowfish.block_size >>> key = b'An arbitrarily long key' >>> iv = Random.new().read(bs) >>> cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv) >>> plaintext = b'docendo discimus ' >>> plen = bs - divmod(len(plaintext),bs)[1] >>> padding = [plen]*plen >>> padding = pack('b'*plen, *padding) >>> msg = iv + cipher.encrypt(plaintext + padding)

I did not find any example on how to decrypt.

最满意答案

我们来看一下:

CBC模式需要一个具有与块大小相同长度的初始化向量(IV) 完整明文是包含填充的实际消息( RFC 2898 Sec。6.1.1步骤4中的 PKCS#5填充) IV被预置为密文

需要做什么:

使用相同的密钥 在创建解密器之前阅读IV 通过查看最后一个字节解密后删除填充,将其评估为整数并从明文末尾删除尽可能多的字节

码:

from Crypto.Cipher import Blowfish from struct import pack bs = Blowfish.block_size key = b'An arbitrarily long key' ciphertext = b'\xe2:\x141vp\x05\x92\xd7\xfa\xb5@\xda\x05w.\xaaRG+U+\xc5G\x08\xdf\xf4Xua\x88\x1b' iv = ciphertext[:bs] ciphertext = ciphertext[bs:] cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv) msg = cipher.decrypt(ciphertext) last_byte = msg[-1] msg = msg[:- (last_byte if type(last_byte) is int else ord(last_byte))] print(repr(msg))

Let's make some observations:

CBC mode needs an Initialization Vector (IV) that has the same length as the block size the full plaintext is the actual message including padding (PKCS#5 padding in RFC 2898 Sec. 6.1.1 Step 4) the IV is prepended to the ciphertext

What needs to be done:

Use the same key Read the IV before creating the decryptor Remove padding after decryption by looking at the last byte, evaluate that as an integer and remove as many bytes from the end of the plaintext

Code:

from Crypto.Cipher import Blowfish from struct import pack bs = Blowfish.block_size key = b'An arbitrarily long key' ciphertext = b'\xe2:\x141vp\x05\x92\xd7\xfa\xb5@\xda\x05w.\xaaRG+U+\xc5G\x08\xdf\xf4Xua\x88\x1b' iv = ciphertext[:bs] ciphertext = ciphertext[bs:] cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv) msg = cipher.decrypt(ciphertext) last_byte = msg[-1] msg = msg[:- (last_byte if type(last_byte) is int else ord(last_byte))] print(repr(msg))

更多推荐

本文发布于:2023-08-02 20:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Pycrypto   Blowfish   decrypt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!