logging / debugging and formatting (no functional changes)
This commit is contained in:
@ -74,7 +74,8 @@ public class AESInputStream extends FilterInputStream {
|
|||||||
if (nval != null) {
|
if (nval != null) {
|
||||||
return nval.intValue();
|
return nval.intValue();
|
||||||
} else {
|
} else {
|
||||||
//_log.debug("No byte available. eof? " + _eofFound);
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
|
_log.debug("No byte available. eof? " + _eofFound);
|
||||||
if (_eofFound)
|
if (_eofFound)
|
||||||
return -1;
|
return -1;
|
||||||
else {
|
else {
|
||||||
@ -97,7 +98,8 @@ public class AESInputStream extends FilterInputStream {
|
|||||||
dest[i] = (byte) val;
|
dest[i] = (byte) val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_log.debug("Read the full buffer of size " + dest.length);
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
|
_log.debug("Read the full buffer of size " + dest.length);
|
||||||
return dest.length;
|
return dest.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,9 +135,10 @@ public class AESInputStream extends FilterInputStream {
|
|||||||
_readyBuf.clear();
|
_readyBuf.clear();
|
||||||
_encryptedBuf.reset();
|
_encryptedBuf.reset();
|
||||||
in.close();
|
in.close();
|
||||||
_log.debug("Cumulative bytes read from source/decrypted/stripped: " + _cumulativeRead + "/"
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
+ _cumulativePrepared + "/" + _cumulativePaddingStripped + "] remaining [" + ready + " ready, "
|
_log.debug("Cumulative bytes read from source/decrypted/stripped: " + _cumulativeRead + "/"
|
||||||
+ encrypted + " still encrypted]");
|
+ _cumulativePrepared + "/" + _cumulativePaddingStripped + "] remaining [" + ready + " ready, "
|
||||||
|
+ encrypted + " still encrypted]");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void mark(int readLimit) {
|
public void mark(int readLimit) {
|
||||||
@ -182,15 +185,20 @@ public class AESInputStream extends FilterInputStream {
|
|||||||
if (false) return; // true to keep the data for decrypt/display on close
|
if (false) return; // true to keep the data for decrypt/display on close
|
||||||
if (_encryptedBuf.size() > 0) {
|
if (_encryptedBuf.size() > 0) {
|
||||||
if (_encryptedBuf.size() >= DECRYPT_SIZE) {
|
if (_encryptedBuf.size() >= DECRYPT_SIZE) {
|
||||||
//_log.debug("We have " + _encryptedBuf.size() + " available to decrypt... doing so");
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
|
_log.debug("We have " + _encryptedBuf.size() + " available to decrypt... doing so");
|
||||||
decrypt();
|
decrypt();
|
||||||
//if (_encryptedBuf.size() > 0)
|
if ( (_encryptedBuf.size() > 0) && (_log.shouldLog(Log.DEBUG)) )
|
||||||
// _log.debug("Bytes left in the encrypted buffer after decrypt: " + _encryptedBuf.size());
|
_log.debug("Bytes left in the encrypted buffer after decrypt: " + _encryptedBuf.size());
|
||||||
} else {
|
} else {
|
||||||
if (_eofFound) {
|
if (_eofFound) {
|
||||||
//_log.debug("EOF and not enough bytes to decrypt [size = " + _encryptedBuf.size() + " totalCumulative: " + _cumulativeRead + "/"+_cumulativePrepared +"]!");
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
|
_log.debug("EOF and not enough bytes to decrypt [size = " + _encryptedBuf.size()
|
||||||
|
+ " totalCumulative: " + _cumulativeRead + "/"+_cumulativePrepared +"]!");
|
||||||
} else {
|
} else {
|
||||||
//_log.debug("Not enough bytes to decrypt [size = " + _encryptedBuf.size() + " totalCumulative: " + _cumulativeRead + "/"+_cumulativePrepared +"]");
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
|
_log.debug("Not enough bytes to decrypt [size = " + _encryptedBuf.size()
|
||||||
|
+ " totalCumulative: " + _cumulativeRead + "/"+_cumulativePrepared +"]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -217,10 +225,12 @@ public class AESInputStream extends FilterInputStream {
|
|||||||
byte nencrypted[] = new byte[encrypted.length - trailing];
|
byte nencrypted[] = new byte[encrypted.length - trailing];
|
||||||
System.arraycopy(encrypted, 0, nencrypted, 0, nencrypted.length);
|
System.arraycopy(encrypted, 0, nencrypted, 0, nencrypted.length);
|
||||||
encrypted = nencrypted;
|
encrypted = nencrypted;
|
||||||
_log.warn("Decrypt got odd segment - " + trailing
|
if (_log.shouldLog(Log.WARN))
|
||||||
+ " bytes pushed back for later decryption - corrupted or slow data stream perhaps?");
|
_log.warn("Decrypt got odd segment - " + trailing
|
||||||
|
+ " bytes pushed back for later decryption - corrupted or slow data stream perhaps?");
|
||||||
} else {
|
} else {
|
||||||
//_log.info(encrypted.length + " bytes makes up " + numBlocks + " blocks to decrypt normally");
|
if (_log.shouldLog(Log.INFO))
|
||||||
|
_log.info(encrypted.length + " bytes makes up " + numBlocks + " blocks to decrypt normally");
|
||||||
}
|
}
|
||||||
|
|
||||||
byte block[] = new byte[BLOCK_SIZE];
|
byte block[] = new byte[BLOCK_SIZE];
|
||||||
@ -244,9 +254,7 @@ public class AESInputStream extends FilterInputStream {
|
|||||||
int remaining = encrypted.length % BLOCK_SIZE;
|
int remaining = encrypted.length % BLOCK_SIZE;
|
||||||
if (remaining != 0) {
|
if (remaining != 0) {
|
||||||
_encryptedBuf.write(encrypted, encrypted.length - remaining, remaining);
|
_encryptedBuf.write(encrypted, encrypted.length - remaining, remaining);
|
||||||
_log
|
_log.debug("After pushing " + remaining
|
||||||
.debug("After pushing "
|
|
||||||
+ remaining
|
|
||||||
+ " bytes back onto the buffer, lets delay 1s our action so we don't fast busy until the net transfers data");
|
+ " bytes back onto the buffer, lets delay 1s our action so we don't fast busy until the net transfers data");
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
@ -273,13 +281,22 @@ public class AESInputStream extends FilterInputStream {
|
|||||||
*/
|
*/
|
||||||
private int[] stripPadding(byte data[]) throws IOException {
|
private int[] stripPadding(byte data[]) throws IOException {
|
||||||
int numPadBytes = (int) data[data.length - 1];
|
int numPadBytes = (int) data[data.length - 1];
|
||||||
if ((numPadBytes >= data.length) || (numPadBytes <= 0)) throw new IOException("Invalid number of pad bytes");
|
if ((numPadBytes >= data.length) || (numPadBytes <= 0)) {
|
||||||
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
|
_log.debug("stripPadding from block " + DataHelper.toHexString(data) + " (" + data.length + "bytes): "
|
||||||
|
+ numPadBytes + " is an invalid # of pad bytes");
|
||||||
|
throw new IOException("Invalid number of pad bytes (" + numPadBytes
|
||||||
|
+ ") for " + data.length + " bytes");
|
||||||
|
}
|
||||||
|
|
||||||
int rv[] = new int[data.length - numPadBytes];
|
int rv[] = new int[data.length - numPadBytes];
|
||||||
// optional, but a really good idea: verify the padding
|
// optional, but a really good idea: verify the padding
|
||||||
if (true) {
|
if (true) {
|
||||||
for (int i = data.length - numPadBytes; i < data.length; i++) {
|
for (int i = data.length - numPadBytes; i < data.length; i++) {
|
||||||
if (data[i] != (byte) numPadBytes) { throw new IOException("Incorrect padding on decryption: data[" + i
|
if (data[i] != (byte) numPadBytes) {
|
||||||
+ "] = " + data[i] + " not " + numPadBytes); }
|
throw new IOException("Incorrect padding on decryption: data[" + i
|
||||||
|
+ "] = " + data[i] + " not " + numPadBytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < rv.length; i++)
|
for (int i = 0; i < rv.length; i++)
|
||||||
|
@ -76,8 +76,9 @@ public class AESOutputStream extends FilterOutputStream {
|
|||||||
flush();
|
flush();
|
||||||
out.close();
|
out.close();
|
||||||
_inBuf.reset();
|
_inBuf.reset();
|
||||||
_log.debug("Cumulative bytes provided to this stream / written out / padded: " + _cumulativeProvided + "/"
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
+ _cumulativeWritten + "/" + _cumulativePadding);
|
_log.debug("Cumulative bytes provided to this stream / written out / padded: "
|
||||||
|
+ _cumulativeProvided + "/" + _cumulativeWritten + "/" + _cumulativePadding);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flush() throws IOException {
|
public void flush() throws IOException {
|
||||||
@ -111,6 +112,10 @@ public class AESOutputStream extends FilterOutputStream {
|
|||||||
byte data[] = DataHelper.xor(block, _lastBlock);
|
byte data[] = DataHelper.xor(block, _lastBlock);
|
||||||
byte encrypted[] = _context.AESEngine().encrypt(data, _key, _lastBlock);
|
byte encrypted[] = _context.AESEngine().encrypt(data, _key, _lastBlock);
|
||||||
_cumulativeWritten += encrypted.length;
|
_cumulativeWritten += encrypted.length;
|
||||||
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
|
_log.debug("Padding block " + i + " of " + numBlocks + " with 1 byte. orig= "
|
||||||
|
+ DataHelper.toHexString(data) + " (size=" + data.length + ") encrypted= "
|
||||||
|
+ DataHelper.toHexString(encrypted) + " (size=" + encrypted.length + ")");
|
||||||
out.write(encrypted);
|
out.write(encrypted);
|
||||||
System.arraycopy(encrypted, encrypted.length - BLOCK_SIZE, _lastBlock, 0, BLOCK_SIZE);
|
System.arraycopy(encrypted, encrypted.length - BLOCK_SIZE, _lastBlock, 0, BLOCK_SIZE);
|
||||||
_cumulativePadding++;
|
_cumulativePadding++;
|
||||||
@ -120,6 +125,8 @@ public class AESOutputStream extends FilterOutputStream {
|
|||||||
// we need to do non trivial padding
|
// we need to do non trivial padding
|
||||||
int remainingBytes = src.length - numBlocks * 15;
|
int remainingBytes = src.length - numBlocks * 15;
|
||||||
int paddingBytes = BLOCK_SIZE - remainingBytes;
|
int paddingBytes = BLOCK_SIZE - remainingBytes;
|
||||||
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
|
_log.debug("Padding " + src.length + " with " + paddingBytes + " bytes in " + numBlocks + " blocks");
|
||||||
System.arraycopy(src, numBlocks * 15, block, 0, remainingBytes);
|
System.arraycopy(src, numBlocks * 15, block, 0, remainingBytes);
|
||||||
Arrays.fill(block, remainingBytes, BLOCK_SIZE, (byte) paddingBytes);
|
Arrays.fill(block, remainingBytes, BLOCK_SIZE, (byte) paddingBytes);
|
||||||
byte data[] = DataHelper.xor(block, _lastBlock);
|
byte data[] = DataHelper.xor(block, _lastBlock);
|
||||||
|
Reference in New Issue
Block a user