2

PDFファイルを.xxxファイルに暗号化するPHPプログラムがあります。この出力は、この.xxxファイルを復号化してPDFファイルに戻すC#プログラムによって読み取られています。

私の問題は、C# で復号化されたファイルを開くと、PDF リーダーがファイルが破損していることを通知することです。PHP でプレーンテキストを暗号化し、C# で復号化すると、暗号化したファイルを取得したため、問題が発生するだけです。 PDFファイル、つまりBINARYファイルに表示されます

助言がありますか ?!

ノート:

  1. PHPでは、mcrypt拡張RijndaelアルゴリズムCBC PKCS7パディングを使用します(パディングは手動で行われます)
  2. C# では、RijndaelManaged クラスを使用してデータの暗号化と復号化を行います

編集

PHPで使用する暗号化方法は次のとおりです。

    function encrypt($key, $iv, $text) {
        ini_set ( 'memory_limit', '-1' );
        $mcrypt_cipher = MCRYPT_RIJNDAEL_256;
        $mcrypt_mode = MCRYPT_MODE_CBC;
        $text=addpadding($text,mcrypt_get_block_size($mcrypt_cipher,'cbc'));
        $encrypted = rtrim ( mcrypt_encrypt ( $mcrypt_cipher, $key, $text, $mcrypt_mode, $iv ), "\0" );
        $encrypted = base64_encode ( $encrypted );
        return $encrypted;
    }

C# での復号化方法は次のとおりです。

    public static string DecryptString(string message, string KeyString, string IVString)
    {
        byte[] Key = Encoding.UTF8.GetBytes(KeyString);
        byte[] IV = Encoding.UTF8.GetBytes(IVString);

        string decrypted = null;
        RijndaelManaged rj = new RijndaelManaged();
        rj.BlockSize = 256;
        rj.Key = Key;
        rj.IV = IV;
        rj.Mode = CipherMode.CBC;
        rj.Padding = PaddingMode.PKCS7;
        try
        {
            MemoryStream ms = new MemoryStream();
            //Encoding enc = new UTF8Encoding();
            byte[] messageBytes = Convert.FromBase64String(message);
            using (CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Write))
            {
                //byte[] messageBytes = enc.GetBytes(message);

                cs.Write(messageBytes, 0, messageBytes.Length);
                cs.Close();
            }
            byte[] encoded = ms.ToArray();
            decrypted = Encoding.UTF8.GetString(encoded);

            ms.Close();
        }
        catch (Exception e)
        {
            MessageBox.Show("An error occurred:"+ e.Message);
        }
        finally
        {
            rj.Clear();
        }

        return decrypted;
    }

C# で復号化を呼び出す方法と、出力を書き込む方法は次のとおりです。

                string Key = cryptography.MD5("X-Ware" + cryptography.MD5("123"));
                string IV = cryptography.MD5("XWare");
                string decrypted = cryptography.DecryptString(contents, Key, IV);
                string outputFilename = cryptography.MD5(OFD.FileName) + ".tmp";

                StreamWriter sw = new StreamWriter("C:\\Windows\\Temp\\" + outputFilename, false, Encoding.UTF8);
                BinaryWriter bw = new BinaryWriter(sw.BaseStream, Encoding.UTF8);
                //sw.Write(decrypted);
                bw.Write(decrypted);
                sw.Close();
                bw.Close();
4

1 に答える 1

5

問題は、バイナリ PDF データを PHP 側と C# 側の両方でテキストとして扱うことだと思います。

 decrypted = Encoding.UTF8.GetString(encoded);

encodedバイナリデータを表す場合は意味がありません。おそらく、このステップをスキップして、 returnDecryptString()として定義する必要がありますbyte[]。そして、それも改名します。

文字列として使用したい場合は、ASCII または ANSI エンコーディングを使用した方がうまくいく可能性があります。

 decrypted = Encoding.ASCII.GetString(encoded);

しかし、エラーは PHP 側で既に発生している可能性がありますが、私にはわかりません。

さらに、私はちょうど注意しました:

    StreamWriter sw = new StreamWriter("C:\\Windows\\Temp\\" + outputFilename,  
           false, Encoding.UTF8);
    BinaryWriter bw = new BinaryWriter(sw.BaseStream, Encoding.UTF8);

これは、BinaryWriter を作成する非常に複雑な方法です。エンコーディングは使用されません。と

 bw.Write(decrypted);

これにより、長さの接頭辞を含む文字列が書き込まれ、PDF が無効になります。

Decrypt の戻り値を のままにしておく場合はstring

  File.WriteAllText("C:\\Windows\\Temp\\" + outputFilename, decrypted);

byte[]そして、 (推奨)として返すときは、

 File.WriteAllBytes("C:\\Windows\\Temp\\" + outputFilename, decrypted);
于 2012-01-14T13:59:43.690 に答える