实验二验收3

你们小组项目中为了保护数据资产用了什么密码算法?

  • 如果用到了对称算法,提交相关生成密钥和对数据加密的代码截图
  • 如果用到了非对称算法,提交相关生成密钥对和对数据加密,签名验签的代码截图
  • 如果用到了其他算法,提交相关的代码截图

算法使用

  1. 口令哈希值:sm3

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

  3. 公文加解密:sm4

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);
		}

img

数据库:

img

img

img

存储为加密后文件

img

用户下载后解密为明文:

img

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

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;
	}

非对称密钥:

img

数据库中加密后的密钥:

img

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;
		}

img

posted @ 2023-06-04 22:51  B1smarck  阅读(15)  评论(0编辑  收藏  举报