私たちは、ストリームにデータを暗号化するためにはBouncyCastle(Android用実際SpongyCastle)フグをラップするクラスを持っている:私たちの元のコードはに書き込む前に、全体のバイト配列を暗号化 ふぐのコードは同等であるが、ではないはずです
public class BlowfishOutputStream extends OutputStream
{
private final OutputStream os;
private final PaddedBufferedBlockCipher bufferedCipher;
単一操作
public void write(byte[] raw, int offset, int length) throws IOException
{
byte[] out = new byte[bufferedCipher.getOutputSize(length)];
int result = this.bufferedCipher.processBytes(raw, 0, length, out, 0);
if (result > 0)
{
this.os.write(out, 0, result);
}
}
で出力ストリーム(一度のデータ、すなわち大量の)画像を送信することは一度にメモリ内に保持されている2枚のコピーになります。
次のコードは同等であることを意図していますが、そうではありません。理由はわかりません。私はデータが送信されていることを確認することができます(c2
の合計はlength
と同等です)が、私たちのサーバーで受信された中間プロセスは、到着したものを見る前にイメージを破棄します。この段階で私が知っているのは、初期コードを使用すると応答が受信され、インクルードされた画像が抽出され、置換コードが使用されたときに応答が受信され(受け入れられる)画像は抽出されないように見えるということです。ストリームが閉じられたときに呼び出されるような問題は、ないによるdoFinal
に欠けているの呼び出しであることが
public void write(byte[] raw, int offset, int length) throws IOException
{
// write to the output stream as we encrypt, not all at once.
final byte[] inBuffer = new byte[Constants.ByteBufferSize];
final byte[] outBuffer = new byte[Constants.ByteBufferSize];
ByteArrayInputStream bis = new ByteArrayInputStream(raw);
// read into inBuffer, encrypt into outBuffer and write to output stream
for (int len; (len = bis.read(inBuffer)) != -1;)
{
int c2 = this.bufferedCipher.processBytes(inBuffer, 0, len, outBuffer, 0);
this.os.write(outBuffer, 0, c2);
}
}
注意。確認
public void close() throws IOException
{
byte[] out = new byte[bufferedCipher.getOutputSize(0)];
int result = this.bufferedCipher.doFinal(out, 0);
if (result > 0)
{
this.os.write(out, 0, result);
}
*nb try/catch omitted*
}
私は第2のアルゴリズムは、渡された 'length'を尊重し、代わりにraw''の全体を想定していない溶液を、持っていると思いますメッセージです。今すぐテスト。 –