代码之家  ›  专栏  ›  技术社区  ›  David Christiansen

为什么不能解密Perl crypt::rijndael的输出?

  •  1
  • David Christiansen  · 技术社区  · 16 年前

    文件已由Perl加密。初始解密尝试失败,我现在正在尝试确定是否有任何Hoojoo正在进行(需要一些其他设置)

    达夫Perl代码:

    use strict;
    
    use Crypt::Rijndael;
    
    my $key ='...';
    
    my $rcipher = Crypt::Rijndael->new ($key, Crypt::Rijndael::MODE_CBC());
    
    undef $/;
    my $encrypted = <>;
    
    print $rcipher->decrypt($encrypted);
    

    C解密实现

            CryptoStream decryptor = null;
            StreamReader srDecrypt = null;
            FileStream fsIn = null;
            RijndaelManaged rijndaelCipher = null;
            string fileContents;
            try
            {
                rijndaelCipher = new RijndaelManaged();
                rijndaelCipher.Mode = CipherMode.CBC;
                rijndaelCipher.Key = Encoding.UTF8.GetBytes(Password);
                rijndaelCipher.IV = Encoding.UTF8.GetBytes(Password);
                rijndaelCipher.Padding = PaddingMode.None;
    
                fsIn = new FileStream(FilePath, FileMode.Open);
                decryptor = new CryptoStream(fsIn, rijndaelCipher.CreateDecryptor(), CryptoStreamMode.Read);
                srDecrypt = new StreamReader(decryptor);
                fileContents = srDecrypt.ReadToEnd();
            }
            finally
            {
                if (decryptor != null)
                    decryptor.Close();
                if (fsIn != null)
                    fsIn.Close();
                if (srDecrypt != null)
                    srDecrypt.Close();
    
                if (rijndaelCipher != null)
                    rijndaelCipher.Clear();
            }
    

    Perl代码应该如何读取

    binmode OUTF;
    
    my $key ="..."; # Your secret key
    
    my $rcipher = Crypt::Rijndael->new ($key, Crypt::Rijndael::MODE_CBC());
    
    $rcipher->set_iv($key); # You may wish this IV to be something different from the Secret Key
    
    my $plaintext = "Please encrypt me"; # Your string to be encrypted
    
    if(length($plaintext) % 16 != 0 ) {
    
    $plaintext .= ' ' x (16 - (length($plaintext) % 16)); } 
    
    my $rencrypted = $rcipher->encrypt($plaintext);
    
    4 回复  |  直到 16 年前
        1
  •  11
  •   brian d foy    16 年前

    我是Perl的维护者 Crypt::Rijndael . 我没有写原始代码,但当它对其他人失败时,我试图使它工作。

    我收到了另一份这样的报告 RT #27632 . 模块中的问题是有符号int,它应该是无符号int。的最新版本 隐窝::Rijndel. ,1.07,应该有修复。您使用的是哪个版本?

    此外,其中一些问题依赖于平台。如果您在发行版中查看rijndael.h代码,您将看到我必须跳过的箍环,以获得各种平台的正确类型大小。我觉得你在窗户上(但不是Cygwin)。您使用的是哪个版本的Windows?

    如RT票据中所述,第一步是使用相同的初始化向量对相同的消息进行加密,同时使用这两种方法 隐窝::Rijndel. 以及C实现。您应该得到相同的输出。如果你没有得到相同的密码文本,就有问题了。

    让我知道这对你来说是怎样的,这样我就可以把它作为 隐窝::Rijndel. 如果我需要的话。

    谢谢,

        2
  •  4
  •   Leon Timmermans    16 年前

    我假设你在Windows上运行这个。您处理的是二进制数据,因此需要考虑到这一点。在Perl中,必须使用 binmode . 我认为你需要在C语言中使用BinaryReader(但我不是C语言程序员,所以我不确定)。

        3
  •  4
  •   Chris S    16 年前

    你有同样大小的静脉输液吗?我在问,因为我在Perl到C加密转换方面遇到了类似的问题,在Perl中,IV大小是不同的。

    您的键大小是256(位),但是您在Perl代码中拥有的键是3个字节,12个位,如果我记得Perl会用空格填充它。

        4
  •  2
  •   Bart S.    16 年前

    你试过加密/解密一个简单的字符串吗? 这样,您就可以验证加密中是否存在来自的内容(而不是读取文件)。