mirror of
https://gitee.com/willfree/min-vpn-client_v2.git
synced 2026-10-04 19:42:00 +08:00
重写sm4,似乎可支持安卓项目,有待进一步测试
This commit is contained in:
1 parent
ddc08833e3
commit
ceffcef5e2
5 files changed
+143
-51
No files matched your search
@@ -0,0 +1,53 @@
|
||||
package com.pkusz.min_vpn_client;
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 16:02 2021/7/27
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.pkusz.min_vpn_client.utils.gmutil.sm4.Android_SM4;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
|
||||
public class TestAndroidSM4 {
|
||||
// 测试安卓没有"SM4/ECB/PKCS5Padding"导致的SM4加解密问题
|
||||
@Test
|
||||
public void testVPNSM4(){
|
||||
try {
|
||||
byte[] enc= Android_SM4.encrypt_ECB_Padding("1234567812345678".getBytes(StandardCharsets.UTF_8),
|
||||
"wefree".getBytes(StandardCharsets.UTF_8));
|
||||
Log.i("fuck","enc: "+Arrays.toString(enc));
|
||||
Log.i("fuck","enc: "+new String(enc));
|
||||
byte[] dec= Android_SM4.decrypt_ECB_Padding("1234567812345678".getBytes(StandardCharsets.UTF_8),
|
||||
enc);
|
||||
Log.i("fuck","dec: "+Arrays.toString(dec));
|
||||
Log.i("fuck","dec: "+new String(dec));
|
||||
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException
|
||||
| NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
|
||||
// try {
|
||||
// byte[] enc= VPN_SM4.encrypt_ECB_Padding("1234567812345678".getBytes(StandardCharsets.UTF_8),
|
||||
// "wefree".getBytes(StandardCharsets.UTF_8));
|
||||
//// Toast.makeText(appContext,Arrays.toString(enc));
|
||||
// } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException
|
||||
// | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -23,16 +23,24 @@ import com.pkusz.min_vpn_client.utils.IPPackageUtil;
|
||||
import com.pkusz.min_vpn_client.utils.KeyManager;
|
||||
import com.pkusz.min_vpn_client.utils.RuleUtil;
|
||||
import com.pkusz.min_vpn_client.utils.SM2HelperForConnection;
|
||||
import com.pkusz.min_vpn_client.utils.gmutil.sm4.Android_SM4;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
|
||||
import common.LoggerHelper;
|
||||
import component.ComponentException;
|
||||
import component.Identifier;
|
||||
@@ -136,6 +144,21 @@ public class MINVpnConnection implements Runnable{
|
||||
// String raw=AESHelperForConnection.decryptWithAES(mSharedSecretString,res);
|
||||
// System.out.println("!!!raw: "+raw);
|
||||
|
||||
// 测试SM4
|
||||
try {
|
||||
byte[] enc= Android_SM4.encrypt_ECB_Padding("1234567812345678".getBytes(StandardCharsets.UTF_8),
|
||||
"wefree".getBytes(StandardCharsets.UTF_8));
|
||||
System.out.println("enc: "+Arrays.toString(enc));
|
||||
System.out.println("enc: "+new String(enc));
|
||||
byte[] dec= Android_SM4.decrypt_ECB_Padding("1234567812345678".getBytes(StandardCharsets.UTF_8),
|
||||
enc);
|
||||
System.out.println("dec: "+Arrays.toString(dec));
|
||||
System.out.println("dec: "+new String(dec));
|
||||
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException
|
||||
| NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 代理地址(?)
|
||||
if(!TextUtils.isEmpty(proxyHostName)) {
|
||||
mProxyHostName = proxyHostName;
|
||||
|
||||
@@ -8,71 +8,33 @@ package com.pkusz.min_vpn_client.utils;
|
||||
*/
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
|
||||
import com.pkusz.min_vpn_client.utils.gmutil.sm4.VPN_SM4;
|
||||
|
||||
import org.zz.gmhelper.SM4Util;
|
||||
|
||||
import com.pkusz.min_vpn_client.utils.gmutil.sm4.Android_SM4;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
|
||||
import VMSConnection.Security.BC_KeyManager;
|
||||
import VMSConnection.Security.SM4.BC_SM4;
|
||||
|
||||
public class SM4HelperForConnection {
|
||||
public static byte[] encryptWithSM4(String secret,byte[] rawText){
|
||||
try {
|
||||
return BC_KeyManager.SM4Encrypt(secret,rawText);
|
||||
} catch (NoSuchPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException | BadPaddingException | InvalidKeyException | NoSuchProviderException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
return encryptWithSM4(secret.getBytes(StandardCharsets.UTF_8),rawText);
|
||||
}
|
||||
|
||||
public static byte[] decryptWithSM4(String secret,byte[] encText){
|
||||
try {
|
||||
return BC_KeyManager.SM4Decrypt(secret,encText);
|
||||
} catch (IllegalBlockSizeException | NoSuchPaddingException | BadPaddingException | NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
return decryptWithSM4(secret.getBytes(StandardCharsets.UTF_8),encText);
|
||||
}
|
||||
|
||||
public static byte[] encryptWithSM4(byte[] secretkey, byte[] origData) {
|
||||
// try {
|
||||
// return VPN_SM4.encrypt_ECB_Padding(secretkey, origData);
|
||||
// } catch (Exception e){
|
||||
// e.printStackTrace();
|
||||
// return null;
|
||||
// }
|
||||
try {
|
||||
return SM4Util.encrypt_ECB_Padding(secretkey,origData);
|
||||
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
|
||||
return Android_SM4.encrypt_ECB_Padding(secretkey, origData);
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] decryptWithSM4(byte[] secretkey, byte[] crypted){
|
||||
// System.out.println("sec: " + secretkey);
|
||||
// System.out.println("cry: " + Arrays.toString(crypted));
|
||||
// try {
|
||||
// return VPN_SM4.decrypt_ECB_Padding(secretkey, crypted);
|
||||
// } catch (Exception e){
|
||||
// e.printStackTrace();
|
||||
// return null;
|
||||
// }
|
||||
try {
|
||||
return SM4Util.decrypt_ECB_Padding(secretkey,crypted);
|
||||
} catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException e) {
|
||||
return Android_SM4.decrypt_ECB_Padding(secretkey, crypted);
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
+18
-6
@@ -2,9 +2,10 @@ package com.pkusz.min_vpn_client.utils.gmutil.sm4;
|
||||
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
|
||||
import javax.vpncrypto.*;
|
||||
import javax.vpncrypto.spec.IvParameterSpec;
|
||||
import javax.vpncrypto.spec.SecretKeySpec;
|
||||
import javax.crypto.*;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import java.security.*;
|
||||
|
||||
/*
|
||||
@@ -16,16 +17,26 @@ import java.security.*;
|
||||
* @Date: 16:26 2021/03/04
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class VPN_SM4 {
|
||||
public class Android_SM4 {
|
||||
static {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
// 加载类的时候执行的静态代码块。只执行一次。
|
||||
// 参考博文:https://www.cnblogs.com/cherrylv/p/10688487.html
|
||||
// 这么做是为了解决安卓不能使用正确版本的BC的问题
|
||||
System.out.println("run static code block");
|
||||
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
|
||||
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
|
||||
System.out.println("runtime has no BouncyCastleProvider");
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
}
|
||||
}
|
||||
|
||||
public static final String ALGORITHM_NAME = "SM4";
|
||||
public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
|
||||
public static final String ALGORITHM_NAME_ECB_NOPADDING = "SM4/ECB/NoPadding";
|
||||
public static final String ALGORITHM_NAME_CBC_PADDING = "SM4/CBC/PKCS5Padding";
|
||||
public static final String ALGORITHM_NAME_CBC_NOPADDING = "SM4/CBC/NoPadding";
|
||||
|
||||
|
||||
/**
|
||||
* SM4算法目前只支持128位(即密钥16字节)
|
||||
*/
|
||||
@@ -33,12 +44,13 @@ public class VPN_SM4 {
|
||||
|
||||
/**
|
||||
* 生成16字节的sm4 key
|
||||
*
|
||||
* @param
|
||||
* @return byte[]
|
||||
* @throws NoSuchAlgorithmException
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/15
|
||||
**/
|
||||
**/
|
||||
public static byte[] generateKey() throws NoSuchAlgorithmException, NoSuchProviderException {
|
||||
return generateKey(DEFAULT_KEY_SIZE);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.pkusz.min_vpn_client.utils;
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 16:02 2021/7/27
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
import com.pkusz.min_vpn_client.utils.gmutil.sm4.Android_SM4;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
|
||||
public class TestSM4 {
|
||||
// 测试安卓没有"SM4/ECB/PKCS5Padding"导致的SM4加解密问题
|
||||
@Test
|
||||
public void testVPNSM4(){
|
||||
try {
|
||||
byte[] enc= Android_SM4.encrypt_ECB_Padding("1234567812345678".getBytes(StandardCharsets.UTF_8),
|
||||
"wefree".getBytes(StandardCharsets.UTF_8));
|
||||
System.out.println("enc: "+Arrays.toString(enc));
|
||||
System.out.println("enc: "+new String(enc));
|
||||
byte[] dec= Android_SM4.decrypt_ECB_Padding("1234567812345678".getBytes(StandardCharsets.UTF_8),
|
||||
enc);
|
||||
System.out.println("dec: "+Arrays.toString(dec));
|
||||
System.out.println("dec: "+new String(dec));
|
||||
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException
|
||||
| NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user