mirror of
https://gitee.com/willfree/min-vpn-client_v2.git
synced 2026-10-04 19:42:00 +08:00
身份数据的加密与解密
This commit is contained in:
1 parent
97387511b5
commit
9a0ac52fdd
1 file changed
+87
@@ -0,0 +1,87 @@
|
||||
package com.pkusz.min_vpn_client.utils;
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 用于支持身份(包含公私钥)的加密
|
||||
* 加密方法:1. 用户输入用户名、明文密码,分别为 user、1234;
|
||||
2. 将用户名和明文密码的的组合字符串”user1234”进行 sm3 哈
|
||||
希,得到 32 字节长度的哈希结果 hash32;
|
||||
3. 取 hash32 的前 16 字节,称之为 hash16;
|
||||
4. 将长度为 16 字节的字节数组 hash16 作为 sm4 密钥,将用户
|
||||
私钥 PrivateKey(或包含私钥信息的用户身份)进行 sm4 加
|
||||
密,得到加密后的结果 EncryptedPrivateKey。
|
||||
* @Version: 1.0.0
|
||||
* @Date: 14:53 2021/8/24
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
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 javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
|
||||
public class IdentityEncryptedUtil {
|
||||
/**
|
||||
* 利用用户名和用户登录VMS的原生密码,对身份信息进行加密
|
||||
* @param username 用户名
|
||||
* @param password 原生密码(未进行md5哈希)
|
||||
* @param rawIdentity 身份数据
|
||||
* @return 加密后的身份数据
|
||||
*/
|
||||
public static byte[] encryptedIdentity(String username,String password,byte[] rawIdentity){
|
||||
return encryptedIdentity((username+password).getBytes(StandardCharsets.UTF_8),rawIdentity);
|
||||
}
|
||||
/**
|
||||
* 主要加密过程
|
||||
* @param rawKey 用户名和原生密码(未经过哈希)组成的原生密钥
|
||||
* @param rawIdentity 加密前的身份数据
|
||||
* @return 加密后的身份数据
|
||||
*/
|
||||
public static byte[] encryptedIdentity(byte[] rawKey,byte[] rawIdentity){
|
||||
// 1. 对原生密钥进行sm3哈希,取前16字节
|
||||
byte[] sm3Hash16Key=SM3HelperForConnection.getSM3Hash16(rawKey);
|
||||
// 2. 将sm3Hash16Key作为密钥,对身份数据进行sm4加密,加密模式为ECB_Padding
|
||||
try {
|
||||
return Android_SM4.encrypt_ECB_Padding(sm3Hash16Key,rawIdentity);
|
||||
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException
|
||||
| NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 利用用户名和用户登录VMS的原生密码,对身份信息进行解密
|
||||
* @param username 用户名
|
||||
* @param password 原生密码(未进行md5哈希)
|
||||
* @param encryptedIdentity 加密的身份数据
|
||||
* @return 解密后的身份数据
|
||||
*/
|
||||
public static byte[] decryptedIdentity(String username,String password,byte[] encryptedIdentity){
|
||||
return decryptedIdentity((username+password).getBytes(StandardCharsets.UTF_8),encryptedIdentity);
|
||||
}
|
||||
/**
|
||||
* 主要解密过程
|
||||
* @param rawKey 用户名和原生密码(未经过哈希)组成的原生密钥
|
||||
* @param encryptedIdentity 解密前的身份数据
|
||||
* @return 解密后的身份数据
|
||||
*/
|
||||
public static byte[] decryptedIdentity(byte[] rawKey,byte[] encryptedIdentity){
|
||||
// 1. 对原生密钥进行sm3哈希,取前16字节
|
||||
byte[] sm3Hash16Key=SM3HelperForConnection.getSM3Hash16(rawKey);
|
||||
// 2. 将sm3Hash16Key作为密钥,对身份数据进行sm4解密,模式为ECB_Padding
|
||||
try {
|
||||
return Android_SM4.decrypt_ECB_Padding(sm3Hash16Key,encryptedIdentity);
|
||||
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException
|
||||
| NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user