-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from dawei101/dawei/rc4md5_n_chacha20
Dawei/rc4md5 n chacha20
- Loading branch information
Showing
12 changed files
with
219 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
app/src/main/java/com/vm/shadowsocks/tunnel/shadowsocks/Chacha20Crypt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.vm.shadowsocks.tunnel.shadowsocks; | ||
|
||
import org.bouncycastle.crypto.StreamCipher; | ||
import org.bouncycastle.crypto.engines.ChaChaEngine; | ||
import org.bouncycastle.crypto.engines.ChaCha7539Engine; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.security.InvalidAlgorithmParameterException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
public class Chacha20Crypt extends CryptBase { | ||
public final static String CIPHER_CHACHA20 = "chacha20"; | ||
public final static String CIPHER_CHACHA20_IETF = "chacha20-ietf"; | ||
|
||
public static Map<String, String> getCiphers() { | ||
Map<String, String> ciphers = new HashMap<>(); | ||
ciphers.put(CIPHER_CHACHA20, Chacha20Crypt.class.getName()); | ||
ciphers.put(CIPHER_CHACHA20_IETF, Chacha20Crypt.class.getName()); | ||
return ciphers; | ||
} | ||
|
||
public Chacha20Crypt(String name, String password) { | ||
super(name, password); | ||
} | ||
|
||
@Override | ||
protected StreamCipher getCipher(boolean isEncrypted) throws InvalidAlgorithmParameterException { | ||
if (_name.equals(CIPHER_CHACHA20)) { | ||
return new ChaChaEngine(); | ||
} | ||
else if (_name.equals(CIPHER_CHACHA20_IETF)) { | ||
return new ChaCha7539Engine(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
protected SecretKey getKey() { | ||
return new SecretKeySpec(_ssKey.getEncoded(), "AES"); | ||
|
||
} | ||
|
||
@Override | ||
protected void _encrypt(byte[] data, ByteArrayOutputStream stream) { | ||
int noBytesProcessed; | ||
byte[] buffer = new byte[data.length]; | ||
|
||
noBytesProcessed = encCipher.processBytes(data, 0, data.length, buffer, 0); | ||
stream.write(buffer, 0, noBytesProcessed); | ||
} | ||
|
||
@Override | ||
protected void _decrypt(byte[] data, ByteArrayOutputStream stream) { | ||
int BytesProcessedNum; | ||
byte[] buffer = new byte[data.length]; | ||
BytesProcessedNum = decCipher.processBytes(data, 0, data.length, buffer, 0); | ||
stream.write(buffer, 0, BytesProcessedNum); | ||
|
||
} | ||
|
||
@Override | ||
public int getKeyLength() { | ||
if (_name.equals(CIPHER_CHACHA20) || _name.equals(CIPHER_CHACHA20_IETF)) { | ||
return 32; | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int getIVLength() { | ||
if (_name.equals(CIPHER_CHACHA20)) { | ||
return 8; | ||
} | ||
else if (_name.equals(CIPHER_CHACHA20_IETF)) { | ||
return 12; | ||
} | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
app/src/main/java/com/vm/shadowsocks/tunnel/shadowsocks/Rc4Md5Crypt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.vm.shadowsocks.tunnel.shadowsocks; | ||
|
||
import org.bouncycastle.crypto.CipherParameters; | ||
import org.bouncycastle.crypto.StreamCipher; | ||
import org.bouncycastle.crypto.engines.RC4Engine; | ||
import org.bouncycastle.crypto.params.KeyParameter; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.security.InvalidAlgorithmParameterException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
|
||
public class Rc4Md5Crypt extends CryptBase { | ||
public static String CIPHER_RC4_MD5 = "rc4-md5"; | ||
|
||
public static Map<String, String> getCiphers() { | ||
Map<String, String> ciphers = new HashMap<String, String>(); | ||
ciphers.put(CIPHER_RC4_MD5, Rc4Md5Crypt.class.getName()); | ||
return ciphers; | ||
} | ||
|
||
public Rc4Md5Crypt(String name, String password) { | ||
super(name, password); | ||
} | ||
|
||
@Override | ||
protected StreamCipher getCipher(boolean isEncrypted) throws InvalidAlgorithmParameterException { | ||
return new RC4Engine(); | ||
} | ||
|
||
@Override | ||
protected SecretKey getKey() { | ||
return new SecretKeySpec(_ssKey.getEncoded(), "AES"); | ||
} | ||
|
||
@Override | ||
protected void _encrypt(byte[] data, ByteArrayOutputStream stream) { | ||
int noBytesProcessed; | ||
byte[] buffer = new byte[data.length]; | ||
|
||
noBytesProcessed = encCipher.processBytes(data, 0, data.length, buffer, 0); | ||
stream.write(buffer, 0, noBytesProcessed); | ||
} | ||
|
||
@Override | ||
protected void _decrypt(byte[] data, ByteArrayOutputStream stream) { | ||
int noBytesProcessed; | ||
byte[] buffer = new byte[data.length]; | ||
|
||
noBytesProcessed = decCipher.processBytes(data, 0, data.length, buffer, 0); | ||
stream.write(buffer, 0, noBytesProcessed); | ||
} | ||
|
||
@Override | ||
public int getIVLength() { | ||
return 16; | ||
} | ||
|
||
@Override | ||
public int getKeyLength() { | ||
return 16; | ||
} | ||
|
||
@Override | ||
protected CipherParameters getCipherParameters(byte[] iv) { | ||
byte[] bts = new byte[_keyLength + _ivLength]; | ||
System.arraycopy(_key.getEncoded(), 0, bts, 0, _keyLength); | ||
System.arraycopy(iv, 0, bts, _keyLength, _ivLength); | ||
return new KeyParameter(md5Digest(bts)); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.