为什么 C# 无法解密 Perl 的 Crypt::Rijndael 的输出?
文件已被 Perl 加密。 初始解密尝试失败,我现在试图确定是否有任何 hoojoo 发生(需要一些其他设置)
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);
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 Code:
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# Decryption Implementation
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();
}
How Perl Code should read
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我是 Perl 的 Crypt::Rijndael 的维护者。 我没有编写原始代码,但当它对其他人来说失败时,我会尝试使其工作。
我收到了另一份类似的报告 RT #27632。 模块中的问题是一个有符号整型,它本应是一个无符号整型。 最新版本的 Crypt::Rijndael 1.07 应该已修复。 您使用的是哪个版本?
此外,其中一些问题与平台相关。 如果您查看发行版中的 rijndael.h 代码,您会发现我必须跳过才能为各种平台获得正确的字体大小。 我想你在 Windows 上(但不是 Cygwin)。 您使用的是哪个版本的 Windows?
正如 RT 票证中所述,第一步是使用 Crypt::Rijndael 和 C# 实现。 您应该得到相同的输出。 如果你没有得到相同的密文,那就有问题了。
让我知道这对您来说效果如何,以便我可以将其作为 Crypt::Rijndael bug,如果我需要的话。
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.
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?
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?
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.
Let me know how this works out for you so I can track it as a Crypt::Rijndael bug if I need to.
我假设你在 Windows 上运行它。 您正在处理二进制数据,因此需要考虑到这一点。 在 Perl 中,您必须使用 binmode。 我认为你需要在 C# 中使用 BinaryReader (但我不是 C# 程序员,所以我不确定)。
I'm assuming you run this on Windows. You're dealing with binary data, so you need to take that into account. In Perl, you have to use binmode. I think you need to use BinaryReader in C# (but I'm no C# programmer, so I'm not sure).
你们有相同的 IV 尺寸吗? 我问这个问题是因为我在 Perl 到 C# 加密转换方面遇到了类似的问题,其中 Perl 中的 IV 大小不同。
您的 KeySize 是 256(位),但 Perl 代码中的密钥是 3 个字节,12 位,如果我记得 Perl 会用空格填充它。
Have you got the same IV sizes? I'm asking as I've had similar problems with Perl to C# encryption conversion, where the IV size was different in Perl.
Your KeySize is 256 (bits) but the key you have in the Perl code is 3 bytes, 12 bits which if I remember Perl pads it with white space.
您是否尝试过加密/解密简单的字符串?
这样您就可以验证加密中是否存在某些内容(而不是读取文件)。
Have you tried encrypting / decrypting a simple string?
With that you could verify that the there is something from in the encryption (and not with reading the file).