任务详情
- 上周任务完成情况(代码链接,所写文档等)
- 本周计划
上周任务完成情况
- 采用sm4对存储的公文进行加密处理,密钥随机生成,乱序存储在数据库中。其中sm4采用cbc模式,iv固定,跟随密文一起存储。解密的时候读取密文并分离密文和iv,然后解密。
- SM3加盐存储
代码
| package cn.edu.nuc.article.util; |
| |
| import org.bouncycastle.crypto.digests.SM3Digest; |
| import org.bouncycastle.util.encoders.Hex; |
| |
| import java.security.Security; |
| import java.util.Arrays; |
| import java.util.HashMap; |
| import java.util.Map; |
| import java.util.Random; |
| |
| public class SM3SaltHelper { |
| public static void main(String[] args) { |
| Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); |
| |
| |
| byte[] data = "Hello, World!".getBytes(); |
| |
| |
| byte[] salt = generateSalt(); |
| |
| |
| byte[] dataWithSalt = concatBytes(data, salt); |
| |
| |
| byte[] hash = calculateHash(dataWithSalt); |
| |
| |
| String saltHex = bytesToHex(salt); |
| String hashHex = bytesToHex(hash); |
| |
| System.out.println("Salt: " + saltHex); |
| System.out.println("Hash: " + hashHex); |
| } |
| |
| public static String encrypt(String paramStr,byte[] salt){ |
| Map<String,String> resultMap=new HashMap<>(); |
| Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); |
| |
| byte[] data = paramStr.getBytes(); |
| |
| |
| byte[] dataWithSalt = concatBytes(data, salt); |
| |
| |
| byte[] hash = calculateHash(dataWithSalt); |
| |
| |
| String hashHex = bytesToHex(hash); |
| return hashHex; |
| } |
| |
| |
| public static String entryptSM3Password(String plainPassword) { |
| byte[] bytesSalt = generateSalt(); |
| String sm3Password= encrypt(plainPassword,bytesSalt); |
| return bytesToHex(bytesSalt)+sm3Password; |
| } |
| |
| public static byte[] generateSalt() { |
| byte[] salt = new byte[8]; |
| new Random().nextBytes(salt); |
| return salt; |
| } |
| |
| private static byte[] concatBytes(byte[] a, byte[] b) { |
| byte[] result = Arrays.copyOf(a, a.length + b.length); |
| System.arraycopy(b, 0, result, a.length, b.length); |
| return result; |
| } |
| |
| private static byte[] calculateHash(byte[] input) { |
| SM3Digest digest = new SM3Digest(); |
| digest.update(input, 0, input.length); |
| byte[] result = new byte[digest.getDigestSize()]; |
| digest.doFinal(result, 0); |
| return result; |
| } |
| |
| public static String bytesToHex(byte[] bytes) { |
| return Hex.toHexString(bytes); |
| } |
| |
| public static byte[] HexTobytes(String hexStr) { |
| |
| return Hex.decode(hexStr); |
| } |
| |
| |
| |
| |
| } |
| package cn.edu.nuc.article.util; |
| |
| import cn.hutool.core.io.FileUtil; |
| import cn.hutool.core.io.IoUtil; |
| import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| import org.bouncycastle.pqc.math.linearalgebra.ByteUtils; |
| |
| import javax.crypto.Cipher; |
| import javax.crypto.CipherInputStream; |
| import javax.crypto.CipherOutputStream; |
| import javax.crypto.NoSuchPaddingException; |
| import javax.crypto.spec.IvParameterSpec; |
| import javax.crypto.spec.SecretKeySpec; |
| import java.io.*; |
| import java.nio.charset.StandardCharsets; |
| import java.nio.file.Files; |
| import java.nio.file.Paths; |
| import java.security.*; |
| import java.util.Arrays; |
| import java.util.Random; |
| |
| public class SM4Tools { |
| private static final String name="SM4"; |
| private static final String transformation="SM4/CBC/PKCS5Padding"; |
| private static final String Default_iv="0123456789abcdef"; |
| |
| |
| |
| |
| |
| |
| |
| |
| public static void encodeFile(String inputFile, String outputFile, String key) throws Exception { |
| |
| byte [] inputBytes = Files.readAllBytes(Paths.get(inputFile)); |
| |
| byte [] encodeByte = encode(inputBytes, key.getBytes(StandardCharsets.UTF_8)); |
| |
| Files.write(Paths.get(outputFile),encodeByte); |
| System.out.println("File encoded successfully."); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public static byte [] encode(byte [] inputByte, byte [] key) throws Exception { |
| |
| Cipher c = Cipher.getInstance(transformation); |
| |
| SecretKeySpec secretKeySpec = new SecretKeySpec(key, name); |
| |
| IvParameterSpec ivParameterSpec = new IvParameterSpec(Default_iv.getBytes(StandardCharsets.UTF_8)); |
| |
| c.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); |
| |
| return c.doFinal(inputByte); |
| } |
| |
| public static void decodeFile(String inputFilePath, String outputFilePath, String key) throws Exception { |
| byte[] inputBytes = Files.readAllBytes(Paths.get(inputFilePath)); |
| byte[] decodeBytes = decode(inputBytes, key.getBytes(StandardCharsets.UTF_8)); |
| Files.write(Paths.get(outputFilePath), decodeBytes); |
| System.out.println("File decode successfully."); |
| } |
| |
| |
| |
| public static byte[] decode(byte[] inputBytes, byte[] key) throws Exception { |
| Cipher cipher = Cipher.getInstance(transformation); |
| SecretKeySpec secretKeySpec = new SecretKeySpec(key, name); |
| IvParameterSpec ivParameterSpec = new IvParameterSpec(Default_iv.getBytes(StandardCharsets.UTF_8)); |
| cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); |
| return cipher.doFinal(inputBytes); |
| } |
| |
| public static String generateRandomString(int length) { |
| Random random = new Random(); |
| StringBuffer string = new StringBuffer(); |
| |
| for (int i = 0; i < length; i++) { |
| |
| int randomChar = random.nextInt(91); |
| if (randomChar >= 48 && randomChar <= 57 || |
| randomChar >= 65 && randomChar <= 90 || |
| randomChar >= 97 && randomChar <= 122) { |
| string.append((char) randomChar); |
| } else { |
| i--; |
| } |
| } |
| |
| return string.toString(); |
| } |
| |
| public static void main(String[] args) throws Exception { |
| Security.addProvider(new BouncyCastleProvider()); |
| String inputFile="D:\\data\\test01.docx"; |
| String enFile="D:\\data\\test01Encode.docx"; |
| String deFile="D:\\data\\test01Decode.docx"; |
| |
| String key= generateRandomString(16) ; |
| System.out.println(key); |
| encodeFile(inputFile,enFile,key); |
| decodeFile(enFile,deFile,key); |
| |
| |
| } |
| |
| } |
实现效果:


本周计划
- 最后调整优化代码
- 边界测试
- 增加用户数据量测试
- 验收
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)