English 中文(简体)
产出无效
原标题:Output is invalid

我有一个PHP方案,将PDF文档加密为xxx, 存档,这一产出正通过一个C#方案阅读,该方案将这xxx号档案重新贴上PDF档案。

我的问题是,当我打开C# 加密的档案时,人民抵抗力量的读者告诉我,档案是腐败的。 当我在PHP的加密文本和C# I的加密中,档案被加密时,问题只是出现在人民抵抗力量的档案中,或者说出现在BINaire的档案中。

任何建议!

注:

  1. In PHP I use mcrypt extension Rijndael algorithm CBC PKCS7 padding (padding is done manually)
  2. In C# I use RijndaelManaged class to encrypt and decrypt data

http://www.ohchr.org。

Here is encryption method that I use in 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 ), "" );
        $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();
最佳回答

我认为,问题在于,你把双向的PDF数据作为购买力平价和C#方面的文本处理。

 decrypted = Encoding.UTF8.GetString(encoded);

如果<代码>encoded表示双重数据,则无意义。 您或许应当绕过这一步,并将<代码>DecodeString(>定义为byte[>。 之后也重新命名。

如果你真的想把它当作你可能更喜欢ASCII或ANSI编码:

 decrypted = Encoding.ASCII.GetString(encoded);

but the error may already be happening on the PHP side, I can t tell.

此外,我刚刚指出:

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

This is a very over-complicated way to create a BinaryWriter. The Encoding will not be used. And

 bw.Write(decrypted);

这将用长篇的序号书写,这肯定会使贵国的国防军失效。

当您将加密作为<条码>载<>,使用时

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

当你作为<条码>逐(>(建议)使用时

 File.WriteAllBytes("C:\Windows\Temp\" + outputFilename, decrypted);
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签