Mercurial > 510Connectbot
diff src/ch/ethz/ssh2/crypto/cipher/CBCMode.java @ 308:42b15aaa7ac7 ganymed
merge
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 30 Jul 2014 14:21:50 -0700 |
parents | 071eccdff8ea |
children |
line wrap: on
line diff
--- a/src/ch/ethz/ssh2/crypto/cipher/CBCMode.java Wed Jul 30 13:38:04 2014 -0700 +++ b/src/ch/ethz/ssh2/crypto/cipher/CBCMode.java Wed Jul 30 14:21:50 2014 -0700 @@ -6,77 +6,66 @@ /** * CBCMode. - * + * * @author Christian Plattner * @version 2.50, 03/15/10 */ -public class CBCMode implements BlockCipher -{ - BlockCipher tc; - int blockSize; - boolean doEncrypt; +public class CBCMode implements BlockCipher { + BlockCipher tc; + int blockSize; + boolean doEncrypt; - byte[] cbc_vector; - byte[] tmp_vector; + byte[] cbc_vector; + byte[] tmp_vector; + + public void init(boolean forEncryption, byte[] key) { + } - public void init(boolean forEncryption, byte[] key) - { - } - - public CBCMode(BlockCipher tc, byte[] iv, boolean doEncrypt) - throws IllegalArgumentException - { - this.tc = tc; - this.blockSize = tc.getBlockSize(); - this.doEncrypt = doEncrypt; + public CBCMode(BlockCipher tc, byte[] iv, boolean doEncrypt) + throws IllegalArgumentException { + this.tc = tc; + this.blockSize = tc.getBlockSize(); + this.doEncrypt = doEncrypt; - if (this.blockSize != iv.length) - throw new IllegalArgumentException("IV must be " + blockSize - + " bytes long! (currently " + iv.length + ")"); + if (this.blockSize != iv.length) + throw new IllegalArgumentException("IV must be " + blockSize + + " bytes long! (currently " + iv.length + ")"); - this.cbc_vector = new byte[blockSize]; - this.tmp_vector = new byte[blockSize]; - System.arraycopy(iv, 0, cbc_vector, 0, blockSize); - } + this.cbc_vector = new byte[blockSize]; + this.tmp_vector = new byte[blockSize]; + System.arraycopy(iv, 0, cbc_vector, 0, blockSize); + } - public int getBlockSize() - { - return blockSize; - } + public int getBlockSize() { + return blockSize; + } - private void encryptBlock(byte[] src, int srcoff, byte[] dst, int dstoff) - { - for (int i = 0; i < blockSize; i++) - cbc_vector[i] ^= src[srcoff + i]; + private void encryptBlock(byte[] src, int srcoff, byte[] dst, int dstoff) { + for (int i = 0; i < blockSize; i++) + cbc_vector[i] ^= src[srcoff + i]; - tc.transformBlock(cbc_vector, 0, dst, dstoff); + tc.transformBlock(cbc_vector, 0, dst, dstoff); + System.arraycopy(dst, dstoff, cbc_vector, 0, blockSize); + } - System.arraycopy(dst, dstoff, cbc_vector, 0, blockSize); - } + private void decryptBlock(byte[] src, int srcoff, byte[] dst, int dstoff) { + /* Assume the worst, src and dst are overlapping... */ + System.arraycopy(src, srcoff, tmp_vector, 0, blockSize); + tc.transformBlock(src, srcoff, dst, dstoff); - private void decryptBlock(byte[] src, int srcoff, byte[] dst, int dstoff) - { - /* Assume the worst, src and dst are overlapping... */ - - System.arraycopy(src, srcoff, tmp_vector, 0, blockSize); - - tc.transformBlock(src, srcoff, dst, dstoff); - - for (int i = 0; i < blockSize; i++) - dst[dstoff + i] ^= cbc_vector[i]; + for (int i = 0; i < blockSize; i++) + dst[dstoff + i] ^= cbc_vector[i]; - /* ...that is why we need a tmp buffer. */ - - byte[] swap = cbc_vector; - cbc_vector = tmp_vector; - tmp_vector = swap; - } + /* ...that is why we need a tmp buffer. */ + byte[] swap = cbc_vector; + cbc_vector = tmp_vector; + tmp_vector = swap; + } - public void transformBlock(byte[] src, int srcoff, byte[] dst, int dstoff) - { - if (doEncrypt) - encryptBlock(src, srcoff, dst, dstoff); - else - decryptBlock(src, srcoff, dst, dstoff); - } + public void transformBlock(byte[] src, int srcoff, byte[] dst, int dstoff) { + if (doEncrypt) + encryptBlock(src, srcoff, dst, dstoff); + else + decryptBlock(src, srcoff, dst, dstoff); + } }