实验二——验收三

验收三

1. 你们小组项目中为了保护数据资产用了什么密码算法?
2. 如果用到了对称算法,提交相关生成密钥和对数据加密的代码截图
3. 如果用到了非对称算法,提交相关生成密钥对和对数据加密,签名验签的代码截图
4. 如果用到了其他算法,提交相关的代码截图

1.算法使用

①口令哈希值:sm3

②密钥生成:gmssl库中的rand()指令进行生成

③公文加解密:sm4

2.sm4介绍

代码:

public static void SM4encrypt(String filePath,String key) throws IOException {
			String cmd = "gmssl sm4 -cbc -encrypt -in " + filePath + " -out " + filePath + ".en -key " + key + " -iv " + key;
			RunCmd.run(cmd);
		}
		
public static void SM4decrypt(String filePath,String key) throws IOException {
			String cmd = "gmssl sm4 -cbc -decrypt -in " + filePath + ".en -out " + filePath + " -key " + key + " -iv " + key;
			RunCmd.run(cmd);
		}

数据库:



存储为加密后文件

用户下载后解密为明文:

使用非对称密钥实现对称密钥保密

public static String SM2encrypt(String key) throws IOException {
		List<String> commandArr = new ArrayList<>();
		commandArr.add("/bin/sh");
		commandArr.add("-c");
		String cmd = "echo " + key + " | "+ "gmssl sm2encrypt -pubkey /home/tsx/sm2pub.pem -out " +"/home/tsx/"+SM3(key)+ ".der";
		commandArr.add(cmd);
		String result = RunCmd.run(commandArr.toArray(new String[commandArr.size()]));
		System.out.println(result);
//		String cmd = "echo " + key + " | "+ "gmssl sm2encrypt -pubkey /home/tsx/sm2pub.pem -out " +"/home/tsx/"+SM3(key)+ ".der";
//		String result =RunCmd.run("echo  123 | openssl sm3");
//		System.out.println(result);
//		RunCmd.run(cmd);
		return SM3(key);
	}

	public static String SM2decrypt(String key) throws IOException {
		String cmd = "gmssl sm2decrypt -key /home/tsx/sm2.pem -pass 1234 -in "+ "/home/tsx/"+ key +".der";
		String result = RunCmd.run(cmd);
		return result;
	}

非对称密钥:

数据库中加密后的密钥:

4.sm3介绍

public static String SM3(String data) throws IOException {
			List<String> commandArr = new ArrayList<>();
			commandArr.add("/bin/sh");
			commandArr.add("-c");
			String cmd = "echo "+data+" | gmssl sm3";
			commandArr.add(cmd);
			String result = RunCmd.run(commandArr.toArray(new String[commandArr.size()]));
			System.out.println(result);
			return result;
		}

posted @ 2023-06-04 20:54  20201325my  阅读(31)  评论(0编辑  收藏  举报