我正在尝试将下面的程序翻译成Python。 最后,Python代码应该生成与Perl变体相同的密码短语输出。
#!/usr/bin/env perl
use Crypt::CBC;
my $key = 'key to the gates';
my $cipher = Crypt::CBC->new(
-key => $key,
-cipher => 'Blowfish',
-salt => '12341234'
);
my $pass_phrase = "secret text";
print $cipher->encrypt_hex($pass_phrase),"\n";
print unpack('H*', $cipher->key()), "\n";
print unpack('H*', $cipher->iv()), "\n";
print unpack('H*', $cipher->salt()), "\n";
print unpack('H*', $cipher->keysize()), "\n";
#output:
#pass:53616c7465645f5f31323334313233344c0ad60f0eb9fdffc46b5cc02d76d473 <- hex enc "Salted__12341234<gibberish>"
#key:031f2cc96d063cf836ce42c77a8a3d25bdd959659d00a892a02b13930e92f47c82a7054256be4a0f1b3771bd36c07fe3ea4f6900f8ddebe5
#iv:f4d50b2385a2a996
#salt:3132333431323334
#keysize:3536
下面是我的python代码,而不是成功解密,但加密失败。 perl密码的成功解密主要用于验证输入参数。 (从perl直接采取IV并删除Crypt :: CBC在解密之前添加了16个盐的焦点使其工作,SO上的各种帖子帮助了我......)。
然后是尝试加密passhphrase并生成与perl相同的输出失败。 我想我只需要使用经过验证的输入参数的正确组合来使其工作......也许盐应该填充?或填充一般是错的?或者iv输入不应该是己烷化的? (它需要8个字符..)
感谢任何输入!
!/usr/bin/env python
from Crypto.Cipher import Blowfish
from binascii import hexlify, unhexlify
from struct import pack
import base64
# working decryption
passphrase = unhexlify("53616c7465645f5f31323334313233344c0ad60f0eb9fdffc46b5cc02d76d473"[32:])
key = unhexlify("031f2cc96d063cf836ce42c77a8a3d25bdd959659d00a892a02b13930e92f47c82a7054256be4a0f1b3771bd36c07fe3ea4f6900f8ddebe5")
iv = unhexlify('f4d50b2385a2a996')
num_padding = ord(Blowfish.new(key, Blowfish.MODE_CBC, iv).decrypt(passphrase)[-1])
print Blowfish.new(key, Blowfish.MODE_CBC, iv).decrypt(passphrase)[:(-1*num_padding)]
# --- non working encryption!
passphrase2 = "secret text"
key2 = 'key to the gates'
iv2 = unhexlify('f4d50b2385a2a996')
plength = Blowfish.block_size - len(passphrase2) % Blowfish.block_size
padding = [plength] * plength
pad_str = passphrase2 + pack('b' * plength, *padding)
cipher = Blowfish.new(key2, Blowfish.MODE_CBC, iv2)
print hexlify("Salted__12341234"+cipher.encrypt(pad_str))
#output:
#secret text
#53616c7465645f5f31323334313233346aa3f2169677cbf282b1330b46da3114
答案 0 :(得分:0)
我想你不应该回答你自己的问题,但我还是留在这里:) 我想在原始代码中填充可能不合适,不太确定。 这次我使用了很好的&#34; Padding&#34; lib https://pypi.python.org/pypi/Padding
from Crypto.Cipher import Blowfish
from binascii import hexlify, unhexlify
import Padding
class BlowFishCipher:
def __init__( self, key,iv,salt ):
self.key = unhexlify(key)
self.iv = unhexlify(iv)
self.salt = unhexlify(salt)
def encrypt( self, raw ):
raw = Padding.appendPadding(raw, BS)
cipher = Blowfish.new( self.key, Blowfish.MODE_CBC, self.iv )
return hexlify("Salted__"+self.salt+cipher.encrypt(raw))
def decrypt( self, enc):
enc = unhexlify(enc)
cipher = Blowfish.new(self.key, Blowfish.MODE_CBC, self.iv )
return Padding.removePadding(cipher.decrypt( enc), BS)
if __name__== "__main__":
BS = Blowfish.block_size
key_perl = "031f2cc96d063cf836ce42c77a8a3d25bdd959659d00a892a02b13930e92f47c82a7054256be4a0f1b3771bd36c07fe3ea4f6900f8ddebe5"
iv_perl = "f4d50b2385a2a996"
salt_perl= "3132333431323334"
passphrase_perl = "53616c7465645f5f31323334313233344c0ad60f0eb9fdffc46b5cc02d76d473"
# remove "Salted__12341234" from passhphrase_perl by [32:]
passphrase = passphrase_perl[32:]
decryptor = BlowFishCipher(key_perl,iv_perl,salt_perl)
plaintext = decryptor.decrypt(passphrase)
print "decrypted {:>70}".format(plaintext)
ciphertext = "secret text"
encodedtext = decryptor.encrypt(ciphertext)
print "encrypted pyhton {:>70}".format(encodedtext)
print "encrypted perl {:>70}".format(passphrase_perl)
解密的秘密文本
加密pyhton 53616c7465645f5f31323334313233344c0ad60f0eb9fdffc46b5cc02d76d473
加密perl 53616c7465645f5f31323334313233344c0ad60f0eb9fdffc46b5cc02d76d473