誰かがこれを行う方法についていくつかの光を提供できますか? 通常のテキストまたはバイト配列に対してこれを行うことができますが、pdf にアプローチする方法がわかりません。最初にpdfをバイト配列に詰め込みますか?
2 に答える
を使用File.ReadAllBytes
して PDF ファイルをロードし、 を使用して通常どおりバイト配列をエンコードしますConvert.ToBase64String(bytes)
。
一度に大量のメモリを燃やす必要がないように、これをチャンクで行う方法があります。
.Net には、チャンキングを実行できるエンコーダーが含まれていますが、それは奇妙な場所にあります。彼らはそれを System.Security.Cryptography 名前空間に入れました。
以下のコード例をテストしましたが、私の方法または上記のアンドリューの方法を使用して同じ出力が得られました。
仕組みは次のとおりです。CryptoStream と呼ばれるクラスを起動します。これは、別のストリームに接続する一種のアダプターです。CryptoTransform というクラスを CryptoStream (ファイル/メモリ/ネットワーク ストリームに接続) にプラグインすると、ストリームからの読み取りまたはストリームへの書き込み中にデータのデータ変換が実行されます。
通常、変換は暗号化/復号化ですが、.net には ToBase64 および FromBase64 変換も含まれているため、暗号化は行わず、エンコードのみを行います。
これがコードです。出力を比較できるように、Andrew の提案の (おそらく不適切な名前の) 実装を含めました。
class Base64Encoder
{
public void Encode(string inFileName, string outFileName)
{
System.Security.Cryptography.ICryptoTransform transform = new System.Security.Cryptography.ToBase64Transform();
using(System.IO.FileStream inFile = System.IO.File.OpenRead(inFileName),
outFile = System.IO.File.Create(outFileName))
using (System.Security.Cryptography.CryptoStream cryptStream = new System.Security.Cryptography.CryptoStream(outFile, transform, System.Security.Cryptography.CryptoStreamMode.Write))
{
// I'm going to use a 4k buffer, tune this as needed
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inFile.Read(buffer, 0, buffer.Length)) > 0)
cryptStream.Write(buffer, 0, bytesRead);
cryptStream.FlushFinalBlock();
}
}
public void Decode(string inFileName, string outFileName)
{
System.Security.Cryptography.ICryptoTransform transform = new System.Security.Cryptography.FromBase64Transform();
using (System.IO.FileStream inFile = System.IO.File.OpenRead(inFileName),
outFile = System.IO.File.Create(outFileName))
using (System.Security.Cryptography.CryptoStream cryptStream = new System.Security.Cryptography.CryptoStream(inFile, transform, System.Security.Cryptography.CryptoStreamMode.Read))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = cryptStream.Read(buffer, 0, buffer.Length)) > 0)
outFile.Write(buffer, 0, bytesRead);
outFile.Flush();
}
}
// this version of Encode pulls everything into memory at once
// you can compare the output of my Encode method above to the output of this one
// the output should be identical, but the crytostream version
// will use way less memory on a large file than this version.
public void MemoryEncode(string inFileName, string outFileName)
{
byte[] bytes = System.IO.File.ReadAllBytes(inFileName);
System.IO.File.WriteAllText(outFileName, System.Convert.ToBase64String(bytes));
}
}
また、CryptoStream を接続する場所をいじっています。Encode メソッドでは、出力 (書き込み) ストリームにアタッチしているので、CryptoStream をインスタンス化するときに、その Write() メソッドを使用します。
読むときは入力(読み込み)ストリームにアタッチしているので、CryptoStreamでreadメソッドを使います。どのストリームにアタッチするかは問題ではありません。適切な Read または Write 列挙型メンバーを CryptoStream のコンストラクターに渡すだけです。