3131
3232package com .vm .shadowsocks .tunnel .shadowsocks ;
3333
34- import org .bouncycastle .crypto .StreamBlockCipher ;
34+ import org .bouncycastle .crypto .CipherParameters ;
35+ import org .bouncycastle .crypto .StreamCipher ;
3536import org .bouncycastle .crypto .params .KeyParameter ;
3637import org .bouncycastle .crypto .params .ParametersWithIV ;
3738
3839import java .io .ByteArrayOutputStream ;
3940import java .io .IOException ;
4041import java .security .InvalidAlgorithmParameterException ;
42+ import java .security .MessageDigest ;
4143import java .security .SecureRandom ;
4244import java .util .concurrent .locks .Lock ;
4345import java .util .concurrent .locks .ReentrantLock ;
5052 */
5153public abstract class CryptBase implements ICrypt {
5254
53- protected abstract StreamBlockCipher getCipher (boolean isEncrypted ) throws InvalidAlgorithmParameterException ;
55+ protected abstract StreamCipher getCipher (boolean isEncrypted ) throws InvalidAlgorithmParameterException ;
5456
5557 protected abstract SecretKey getKey ();
5658
5759 protected abstract void _encrypt (byte [] data , ByteArrayOutputStream stream );
5860
5961 protected abstract void _decrypt (byte [] data , ByteArrayOutputStream stream );
6062
63+ protected CipherParameters getCipherParameters (byte [] iv ){
64+ _decryptIV = new byte [_ivLength ];
65+ System .arraycopy (iv , 0 , _decryptIV , 0 , _ivLength );
66+ return new ParametersWithIV (new KeyParameter (_key .getEncoded ()), _decryptIV );
67+ }
68+
6169 protected final String _name ;
6270 protected final SecretKey _key ;
6371 protected final ShadowSocksKey _ssKey ;
@@ -69,8 +77,8 @@ public abstract class CryptBase implements ICrypt {
6977 protected byte [] _decryptIV ;
7078 protected final Lock encLock = new ReentrantLock ();
7179 protected final Lock decLock = new ReentrantLock ();
72- protected StreamBlockCipher encCipher ;
73- protected StreamBlockCipher decCipher ;
80+ protected StreamCipher encCipher ;
81+ protected StreamCipher decCipher ;
7482 private Logger logger = Logger .getLogger (CryptBase .class .getName ());
7583
7684 public CryptBase (String name , String password ) {
@@ -86,26 +94,26 @@ protected void setIV(byte[] iv, boolean isEncrypt) {
8694 return ;
8795 }
8896
97+ CipherParameters cipherParameters = null ;
98+
8999 if (isEncrypt ) {
90- _encryptIV = new byte [_ivLength ];
91- System .arraycopy (iv , 0 , _encryptIV , 0 , _ivLength );
100+ cipherParameters = getCipherParameters (iv );
92101 try {
93102 encCipher = getCipher (isEncrypt );
94- ParametersWithIV parameterIV = new ParametersWithIV (new KeyParameter (_key .getEncoded ()), _encryptIV );
95- encCipher .init (isEncrypt , parameterIV );
96103 } catch (InvalidAlgorithmParameterException e ) {
97104 logger .info (e .toString ());
98105 }
106+ encCipher .init (isEncrypt , cipherParameters );
99107 } else {
100108 _decryptIV = new byte [_ivLength ];
101109 System .arraycopy (iv , 0 , _decryptIV , 0 , _ivLength );
110+ cipherParameters = getCipherParameters (iv );
102111 try {
103112 decCipher = getCipher (isEncrypt );
104- ParametersWithIV parameterIV = new ParametersWithIV (new KeyParameter (_key .getEncoded ()), _decryptIV );
105- decCipher .init (isEncrypt , parameterIV );
106113 } catch (InvalidAlgorithmParameterException e ) {
107114 logger .info (e .toString ());
108115 }
116+ decCipher .init (isEncrypt , cipherParameters );
109117 }
110118 }
111119
@@ -174,4 +182,14 @@ public void decrypt(byte[] data, int length, ByteArrayOutputStream stream) {
174182 System .arraycopy (data , 0 , d , 0 , length );
175183 decrypt (d , stream );
176184 }
185+
186+
187+ public static byte [] md5Digest (byte [] input ) {
188+ try {
189+ MessageDigest md5 = MessageDigest .getInstance ("MD5" );
190+ return md5 .digest (input );
191+ } catch (Exception e ) {
192+ throw new RuntimeException (e );
193+ }
194+ }
177195}
0 commit comments