English 中文(简体)
PHP和C#之间的基64文档内容下降
原标题:Decoding base64 file contents between PHP and C#

我需要从PHP向C#客户(Mono,各平台)提供AES加密材料。 我已成功地将AES加密/加密工作罚款,但一旦我试图将第64基地围起来,我就会遇到麻烦。 以下两个例子都使残疾人协会残疾,因此,不应成为因素。

我最简单的测试案例,Hello World string, 奏效:

PHP 产出服务

// Save encoded data to file
$data = base64_encode("Hello encryption world!!");
$file = fopen($targetPath,  w );
fwrite($file, $data);
fclose($file);

// Later on, serve the file
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=".basename($product->PackageFilename($packageId)));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($targetPath));
ob_clean();
flush();
$handle = fopen($targetPath, "r");
fpassthru($handle);
fclose($handle);

C# 编码和使用

StreamReader reader = new StreamReader(stream);
char[] buffer = DecodeBuffer;
string decoded = "";
int read = 0;
while (0 < (read = reader.Read(buffer, 0, DecodeBufferSize)))
{
    byte[] decodedBytes = Convert.FromBase64CharArray(buffer, 0, read);
    decoded += System.Text.Encoding.UTF8.GetString(decodedBytes);
}
Log(decoded); // Correctly logs "Hello encryption world!!"

然而,在我开始尝试对档案内容做同样的事情时,Convert.FromBase64CharArray投掷了FormatException: Invalid nature found:

PHP 产出服务

// Save encoded data to file
$data = base64_encode(file_get_contents($targetPath));
$file = fopen($targetPath,  w );
fwrite($file, $data);
fclose($file);

// Later on, serve the file
// Same as above

C# 编码和使用

using (Stream file = File.Open(zipPath, FileMode.Create))
{
using (StreamReader reader = new StreamReader(stream))
{
    char[] buffer = DecodeBuffer;
    byte[] decodedBytes;
    int read = 0;
    while (0 < (read = reader.Read(buffer, 0, DecodeBufferSize)))
    {
        // Throws FormatException: Invalid character found
                    decodedBytes = Convert.FromBase64CharArray(buffer, 0, read);
        file.Write(decodedBytes, 0, decodedBytes.Length);
    }
}
}

是否应当对基础64的较大数据进行某种额外的处理? 采用大型双元数据或许不合适,如果是,你如何防止出现不安全的传染特性的潜在问题?

问题回答

你阅读的《基地64》文本不正确。

  • Base64 is text, so consider using text reader instead
  • Base64 may contain new lines/white spaces. I.e. it is custom to have split whole Base64 encoded value into 70-80 character long strings.

核实档案中的数据是否正确为整个卷宗(

如果档案中含有有效的基地64数据,你可以将这些数据作为单一指示阅读,你应执行自己的基地64 编码或人工阅读非白色空间特征的正确数字(四倍多)和编码这种空白。

Base64 encoding transformations 3 octets into 4 encoded natures. 因此,你提供的编码数据长度需要超过4份。

首先,确保<代码>DecodeBufferSize为4的多个。 其次,由于<条码>StreamReader.Read 不能保证所有申请的要约都将读到buffer,直至填满为止,或到下游为止。





相关问题
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. ...

热门标签