我需要从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的较大数据进行某种额外的处理? 采用大型双元数据或许不合适,如果是,你如何防止出现不安全的传染特性的潜在问题?