を返します。次のルーチンで.NETのRijndaelManagedクラスを使用してこのファイルを復号化します。OpenSSLは、私は、バイナリAES暗号化されたファイルを持っている私のLinuxマシンでは不正なマジックナンバー
public static bool decryptFile(string inputFile, string outputFile)
{
string s = "h3y_gUyZ";
UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
byte[] bytes = unicodeEncoding.GetBytes(s);
FileStream fileStream = new FileStream(inputFile, FileMode.Open);
RijndaelManaged rijndaelManaged = new RijndaelManaged();
CryptoStream cryptoStream = new CryptoStream(fileStream, rijndaelManaged.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
FileStream fileStream2 = new FileStream(outputFile, FileMode.Create);
try
{
int num;
while ((num = cryptoStream.ReadByte()) != -1)
{
fileStream2.WriteByte((byte)num);
}
fileStream2.Close();
cryptoStream.Close();
fileStream.Close();
}
catch
{
fileStream2.Close();
fileStream.Close();
File.Delete(outputFile);
return true;
}
return false;
}
Linux私はこのコードをC#プログラムに埋め込みました。モノで、mcsでコンパイルした後です。 Dパラメータは、前述の機能を実行
mcs *.cs -out:mybinary.exe
mono mybinary.exe d Leela_Turanga.plr outputfile.dat
、Leela_Turanga.plrを解読するファイルと outputfile.dat で得decrypedファイルです。 これはうまくいきます:私はファイルを正しく解読することができ、人間が読めるテキストが解読されたファイルに現れるので、これを言うことができます。 今、私はopensslと同じファイルを解読したいと思います。
- キーサイズ
- AESの動作モード(CBC、ECB、PCBC、CFB:私は、アルゴリズムパラメータを取得する必要があり、そして作品上記のコード以来、私は私にこれらの情報を与えるためにそれを変更されたすべての ファーストいくつかのコードを追加することで...)
- パディング(それが何であるか見当がつかない)
:
public static bool decryptFile(string inputFile, string outputFile)
{
string s = "h3y_gUyZ";
UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
byte[] bytes = unicodeEncoding.GetBytes(s);
FileStream fileStream = new FileStream(inputFile, FileMode.Open);
RijndaelManaged rijndaelManaged = new RijndaelManaged();
CryptoStream cryptoStream = new CryptoStream(fileStream, rijndaelManaged.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
FileStream fileStream2 = new FileStream(outputFile, FileMode.Create);
//=======DEBUG INFO=======
//PRINT ALGORITHM SETTINGS
Console.WriteLine(rijndaelManaged.Mode); //what AES mode are we using?
Console.WriteLine(rijndaelManaged.KeySize); //what is the keysize?
Console.WriteLine(rijndaelManaged.Padding); //what is the padding?
//========================
try
{
int num;
while ((num = cryptoStream.ReadByte()) != -1)
{
fileStream2.WriteByte((byte)num);
}
fileStream2.Close();
cryptoStream.Close();
fileStream.Close();
}
catch
{
fileStream2.Close();
fileStream.Close();
File.Delete(outputFile);
return true;
}
return false;
}
RijndaelManagedがCBCモードで256ビット鍵とPKCS7パディング(重要であるかどうかはもう分かりません)を使って作業しています。 は、今私は解読するためにOpenSSLを使用して試すことができます:
openssl enc -aes-256-cbc -d -in Leela_Turanga.plr -out out.bin
そして、パスワードプロンプトは、私はパスを入力し、表示され、「悪いマジックナンバー」エラーが
enter aes-256-cbc decryption password:
bad magic number
」返さないと私は何を取得しています復号化されたファイル
なぜOpenSSLは、私はインターネット上で検索しましたが、私は私の答えを見つけていないことを言うん
も:。?。 C#の文字列はUTF-16(したがって16ビットのchar)でエンコードされ、キーは8文字である "h3y_gUyZ"であるため、16 x 8 = 128ビットの幅にするべきではありませんか? 256 rijndaelManaged.KeySizeの代わりに戻ります。
パスワードをキーとして使用し、キーをIVとして使用することは安全ではないことに注意してください。キーはパスワードなどから導き出されるべきです。 'Rfc2898DeriveBytes'を使用し、IVはランダムと区別できません。 –