Encrypt without allocating new buffers.

This commit is contained in:
akwizgran
2011-08-12 17:35:45 +02:00
parent 2c387f80b1
commit 4dcf9a70a1

View File

@@ -10,6 +10,7 @@ import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
class PacketEncrypterImpl extends FilterOutputStream
@@ -67,22 +68,35 @@ implements PacketEncrypter {
@Override
public void write(int b) throws IOException {
// FIXME: Encrypt into same buffer
byte[] ciphertext = packetCipher.update(new byte[] {(byte) b});
if(ciphertext != null) out.write(ciphertext);
byte[] buf = new byte[] {(byte) b};
try {
int i = packetCipher.update(buf, 0, buf.length, buf);
assert i <= 1;
if(i == 1) out.write(b);
} catch(ShortBufferException badCipher) {
throw new RuntimeException(badCipher);
}
}
@Override
public void write(byte[] b) throws IOException {
// FIXME: Encrypt into same buffer
byte[] ciphertext = packetCipher.update(b);
if(ciphertext != null) out.write(ciphertext);
try {
int i = packetCipher.update(b, 0, b.length, b);
assert i <= b.length;
out.write(b, 0, i);
} catch(ShortBufferException badCipher) {
throw new RuntimeException(badCipher);
}
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
// FIXME: Encrypt into same buffer
byte[] ciphertext = packetCipher.update(b, off, len);
if(ciphertext != null) out.write(ciphertext);
try {
int i = packetCipher.update(b, off, len, b, off);
assert i <= len;
out.write(b, off, i);
} catch(ShortBufferException badCipher) {
throw new RuntimeException(badCipher);
}
}
}