use of com.github.zhenwei.core.crypto.BlockCipher in project LinLong-Java by zhenwei1108.
the class NISTCTSBlockCipher method doFinal.
/**
* Process the last block in the buffer.
*
* @param out the array the block currently being held is copied into.
* @param outOff the offset at which the copying starts.
* @return the number of output bytes copied to out.
* @exception com.github.zhenwei.core.crypto.DataLengthException if there is insufficient space in out for
* the output.
* @exception IllegalStateException if the underlying cipher is not
* initialised.
* @exception com.github.zhenwei.core.crypto.InvalidCipherTextException if cipher text decrypts wrongly (in
* case the exception will never get thrown).
*/
public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException, InvalidCipherTextException {
if (bufOff + outOff > out.length) {
throw new OutputLengthException("output buffer to small in doFinal");
}
int blockSize = cipher.getBlockSize();
int len = bufOff - blockSize;
byte[] block = new byte[blockSize];
if (forEncryption) {
if (bufOff < blockSize) {
throw new DataLengthException("need at least one block of input for NISTCTS");
}
if (bufOff > blockSize) {
byte[] lastBlock = new byte[blockSize];
if (this.type == CS2 || this.type == CS3) {
cipher.processBlock(buf, 0, block, 0);
System.arraycopy(buf, blockSize, lastBlock, 0, len);
cipher.processBlock(lastBlock, 0, lastBlock, 0);
if (this.type == CS2 && len == blockSize) {
System.arraycopy(block, 0, out, outOff, blockSize);
System.arraycopy(lastBlock, 0, out, outOff + blockSize, len);
} else {
System.arraycopy(lastBlock, 0, out, outOff, blockSize);
System.arraycopy(block, 0, out, outOff + blockSize, len);
}
} else {
System.arraycopy(buf, 0, block, 0, blockSize);
cipher.processBlock(block, 0, block, 0);
System.arraycopy(block, 0, out, outOff, len);
System.arraycopy(buf, bufOff - len, lastBlock, 0, len);
cipher.processBlock(lastBlock, 0, lastBlock, 0);
System.arraycopy(lastBlock, 0, out, outOff + len, blockSize);
}
} else {
cipher.processBlock(buf, 0, block, 0);
System.arraycopy(block, 0, out, outOff, blockSize);
}
} else {
if (bufOff < blockSize) {
throw new DataLengthException("need at least one block of input for CTS");
}
byte[] lastBlock = new byte[blockSize];
if (bufOff > blockSize) {
if (this.type == CS3 || (this.type == CS2 && ((buf.length - bufOff) % blockSize) != 0)) {
if (cipher instanceof CBCBlockCipher) {
BlockCipher c = ((CBCBlockCipher) cipher).getUnderlyingCipher();
c.processBlock(buf, 0, block, 0);
} else {
cipher.processBlock(buf, 0, block, 0);
}
for (int i = blockSize; i != bufOff; i++) {
lastBlock[i - blockSize] = (byte) (block[i - blockSize] ^ buf[i]);
}
System.arraycopy(buf, blockSize, block, 0, len);
cipher.processBlock(block, 0, out, outOff);
System.arraycopy(lastBlock, 0, out, outOff + blockSize, len);
} else {
BlockCipher c = ((CBCBlockCipher) cipher).getUnderlyingCipher();
c.processBlock(buf, bufOff - blockSize, lastBlock, 0);
System.arraycopy(buf, 0, block, 0, blockSize);
if (len != blockSize) {
System.arraycopy(lastBlock, len, block, len, blockSize - len);
}
cipher.processBlock(block, 0, block, 0);
System.arraycopy(block, 0, out, outOff, blockSize);
for (int i = 0; i != len; i++) {
lastBlock[i] ^= buf[i];
}
System.arraycopy(lastBlock, 0, out, outOff + blockSize, len);
}
} else {
cipher.processBlock(buf, 0, block, 0);
System.arraycopy(block, 0, out, outOff, blockSize);
}
}
int offset = bufOff;
reset();
return offset;
}
use of com.github.zhenwei.core.crypto.BlockCipher in project LinLong-Java by zhenwei1108.
the class OldCTSBlockCipher method doFinal.
/**
* Process the last block in the buffer.
*
* @param out the array the block currently being held is copied into.
* @param outOff the offset at which the copying starts.
* @return the number of output bytes copied to out.
* @throws com.github.zhenwei.core.crypto.DataLengthException if there is insufficient
* space in out for the output.
* @throws IllegalStateException if the underlying cipher is
* not initialised.
* @throws com.github.zhenwei.core.crypto.InvalidCipherTextException if cipher text decrypts
* wrongly (in case the
* exception will never get
* thrown).
*/
public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException, InvalidCipherTextException {
if (bufOff + outOff > out.length) {
throw new OutputLengthException("output buffer to small in doFinal");
}
int blockSize = cipher.getBlockSize();
int len = bufOff - blockSize;
byte[] block = new byte[blockSize];
if (forEncryption) {
cipher.processBlock(buf, 0, block, 0);
if (bufOff < blockSize) {
throw new DataLengthException("need at least one block of input for CTS");
}
for (int i = bufOff; i != buf.length; i++) {
buf[i] = block[i - blockSize];
}
for (int i = blockSize; i != bufOff; i++) {
buf[i] ^= block[i - blockSize];
}
if (cipher instanceof CBCBlockCipher) {
BlockCipher c = ((CBCBlockCipher) cipher).getUnderlyingCipher();
c.processBlock(buf, blockSize, out, outOff);
} else {
cipher.processBlock(buf, blockSize, out, outOff);
}
System.arraycopy(block, 0, out, outOff + blockSize, len);
} else {
byte[] lastBlock = new byte[blockSize];
if (cipher instanceof CBCBlockCipher) {
BlockCipher c = ((CBCBlockCipher) cipher).getUnderlyingCipher();
c.processBlock(buf, 0, block, 0);
} else {
cipher.processBlock(buf, 0, block, 0);
}
for (int i = blockSize; i != bufOff; i++) {
lastBlock[i - blockSize] = (byte) (block[i - blockSize] ^ buf[i]);
}
System.arraycopy(buf, blockSize, block, 0, len);
cipher.processBlock(block, 0, out, outOff);
System.arraycopy(lastBlock, 0, out, outOff + blockSize, len);
}
int offset = bufOff;
reset();
return offset;
}
Aggregations