为什么C#解密Perl的Crypt :: Rijndael的输出? [英] Why can't C# decrypt the output from Perl's Crypt::Rijndael?
问题描述
文件已由Perl加密。初始解密尝试失败,我现在正在尝试确定是否有任何hoojoo(需要其他一些设置)
A file has been encrypted by Perl. Initial decrypt attempts failed and I am now trying to ascertain whether there is any hoojoo going on (some other settings required)
Duff 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);
推荐答案
我是Perl的维护者 Crypt :: Rijndael 。我没有写原始代码,但是当其他人失败时,我会尝试使其工作。
I'm the maintainer of Perl's Crypt::Rijndael. I didn't write the original code, but I try to make it work when it fails for other people.
我收到另一个报告,如 RT#27632 。该模块中的问题是一个已签名的int应该是一个unsigned int。最新版本的 Crypt :: Rijndael ,1.07应该有修复。您使用哪个版本?
I received another report like this as RT #27632. The problem in the module was a signed int that should have been an unsigned int. The latest version of Crypt::Rijndael, 1.07, should have the fix. Which version are you using?
此外,其中一些问题依赖于平台。如果您在发行版中查看rijndael.h代码,您将看到我必须跳过的圈,以获得各种平台的正确类型大小。我想你在Windows上(而不是Cygwin)。您正在使用哪个版本的Windows?
Also, some of these problems are platform dependent. If you look in the rijndael.h code in the distribution, you'll see the hoops I have to jump through to get the right type sizes for various platforms. I figure you're on Windows (but not Cygwin). Which version of Windows are you using?
如RT票证所示,第一步是使用相同的初始化向量来加密相同的消息,使用 Crypt :: Rijndael 和C#实现。你应该得到相同的输出。如果你没有得到相同的密文,那就有问题。
As noted in the RT ticket, the first step is to encrypt the same messages with the same initialization vectors using both Crypt::Rijndael and the C# implementation. You should get the same output. If you don't get the same crypttext, there is a problem.
让我知道这是如何工作的,所以我可以跟踪它作为一个 Crypt :: Rijndael 错误,如果我需要。
Let me know how this works out for you so I can track it as a Crypt::Rijndael bug if I need to.
谢谢,
这篇关于为什么C#解密Perl的Crypt :: Rijndael的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!