vue引入sm-crypto通过sm4对文件进行加解密,用户输入密码

对文件加密并保存:

javascript 复制代码
import { sm4 } from 'sm-crypto'

fetch("你的文件地址") .then(response => response.blob()) .then(byteStream => {
        const reader2 = new FileReader();
        reader2.onload = function(event) {
            const arrayBuffer = event.target.result;
            let keyBytes = new Uint8Array(arrayBuffer);
            let password = stringToHex("用户输入的密码");
            let code = sm4.encrypt(keyBytes,password, {output: 'array'})  
            let byteStream1 = new Blob([code], { type: 'application/octet-stream' });
            let link = document.createElement('a');
            link.href = URL.createObjectURL(byteStream1);
            link.download = "你保存的文件名";
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        };
        // 以ArrayBuffer形式读取文件
        reader2.readAsArrayBuffer(byteStream);
      })
	function stringToHex(str) {
	  let hex = '';
	  for (let i = 0; i < str.length; i++) {
	    hex += str.charCodeAt(i).toString(16);
	  }
	  // 如果转换后的16进制字符串长度不足32,则在前面填充0
	  while (hex.length < 32) {
	    hex = '0' + hex;
	  }
	  return hex;
	}

读取文件并解密:

javascript 复制代码
<input type="file" id="keyInput" accept="*" @change="readKeyFile" />

function readKeyFile() {
	    const keyInput = document.getElementById('keyInput');
	    const file = keyInput.files[0];
	    const reader = new FileReader();
	    reader.onload = function(event) {
	        const arrayBuffer = event.target.result;
	        let plainByte = JSON.parse("["+arrayBuffer+"]");
	        let password = stringToHex("用户输入的密码");
	        try{
	        	let pBytes = sm4.decrypt(plainByte,password, {output: 'array'});
	        	let prBytes = arrayToBuffer(pBytes);
	        	privateKeyBytes = new Uint8Array(prBytes);
	        }catch(e){
	        	alert('文件或密码错误');
	        	return;
	        }
			let decoder = new TextDecoder();
		    let alltext = decoder.decode(privateKeyBytes);
            console.log(alltext)//你的文件内容
	    };
	    // 读取文件
	    reader.readAsText(file);
	}
function stringToHex(str) {
    let hex = '';
    for (let i = 0; i < str.length; i++) {
      hex += str.charCodeAt(i).toString(16);
    }
    // 如果转换后的16进制字符串长度不足32,则在前面填充0
    while (hex.length < 32) {
      hex = '0' + hex;
    }
    return hex;
  }
  function arrayToBuffer(arr) {
      let buffer = new ArrayBuffer(arr.length);
      let view = new Uint8Array(buffer);
      for (let i = 0; i < arr.length; i++) {
          view[i] = arr[i];
      }
      return buffer;
  }
相关推荐
simon_luv_pho1 小时前
一行代码把网页变成 AI Agent?
前端
兆子龙1 小时前
模块联邦(Module Federation)详解:从概念到手把手 Demo
前端·架构
ZFSS1 小时前
Kimi Chat Completion API 申请及使用
前端·人工智能
没想好d1 小时前
通用管理后台组件库-8-表格组件
前端
前端Hardy1 小时前
HTML&CSS&JS:打造丝滑的3D彩纸飘落特效
前端·javascript·css
前端Hardy1 小时前
HTML&CSS&JS:丝滑无卡顿的明暗主题切换
javascript·css·html
布列瑟农的星空2 小时前
rsbuild mock 插件开发指南
前端
用泥种荷花2 小时前
【LangChain.js学习】 文档加载(Loader)与文本分割全解析
前端
cxxcode3 小时前
Vite 热更新(HMR)原理详解
前端
HelloReader3 小时前
Tauri 架构从“WebView + Rust”到完整工具链与生态
前端