Perl Crypt :: CBC错误处理

时间:2013-06-28 23:47:03

标签: perl encryption

我正在使用Perl CBC来加密和解密消息,但我收到的消息有时可能会格式不正确。现在,如果我对格式不正确的消息运行解密,我就会收到错误,程序就会死掉:

Ciphertext does not begin with a valid header for 'salt' header mode at /tmp/test.pl line 26.

处理此问题的正确/最佳方法是什么?我可以将它放在一个eval块中并检查错误,例如:

eval {
    my $decrypted = $cipher->decrypt_hex($malformed_message);
}

if ($@) {
    print "Well, that didn't work!\n";
}

但我想知道是否有更好的方法?如果Crypt :: CBC没有得到好的输入,而不是提供错误代码,那么它似乎并不完美。

1 个答案:

答案 0 :(得分:1)

制作加密代码,以免发生这种情况。如果不这样做,无论您如何处理错误,攻击者都可能在系统中引发错误,使用它们可以解密您的邮件。这被称为padding oracle attack,而且相当具有破坏性。

您需要使用消息身份验证代码(例如HMACSHA256)验证整个密文,或使用经过身份验证的密文文本模式(如AES GCM),以便在尝试解密之前检测损坏。

抱歉,我不知道Perl,但你想要这样的MAC

if(verifyMAC(ciphertext,message):
    //decrypt and handle 
else:
   print "While that didn't work
相关问题