📄 cbccipher.java
字号:
/*Christoforos Pirillos @ Villanova University - May 1999based on code from the book "Java Network Programming" by Hughes*/package encryption;/**/**The CBCCipher class is a Cipher that attaches to an existingblock-cipher and applies CBC stream-encryption to it.*/public class CBCCipher extends Cipher {/**The Cipher used for encryption*/protected Cipher c;protected byte[] oldEncipher, oldDecipher;/**The block size of the cipher used*/protected int bS;/**Accepts a Cipher to be used and an initialization vector that will beused to initialize the cipher-block-chaining*/public CBCCipher (Cipher c, byte[] iv) { this.c=c; bS=c.blockSize(); oldEncipher=new byte[bS]; oldDecipher=new byte[bS]; if (iv!= null) { System.arraycopy (iv,0,oldEncipher,0,bS); System.arraycopy (iv,0,oldDecipher,0,bS); }}/**same as CBCCipher (Cipher, byte[]) but assumes byte[]=0*/public CBCCipher (Cipher c) { this (c,null);}/**Encrypts a block using X-OR on the plaintext with the previousciphertext block and then encrypt this with the Cipher*/public void encipherBlock (byte[] plain, int po, byte[] cipher, int co) { for (int i=0;i<bS;++i) cipher[co+i]=(byte)(oldEncipher[i]^plain[po+i]); c.encipherBlock(cipher,co,cipher,co); System.arraycopy (cipher, co, oldEncipher, 0, bS);}/**The reverse procedure of encipherBlock method */public void decipherBlock (byte[] cipher, int co, byte[] plain, int po) { c.decipherBlock (cipher, co, plain, po); for (int i=0;i<bS;++i) { byte o = cipher[co+i]; plain[po+i] ^= oldDecipher[i]; oldDecipher[i]=o; }}/**Returns the block size of the Cipher being used*/public int blockSize() { return bS;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -