Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

javascript - NodeJS加密/解密文件(NodeJS Encryption/Decryption of Files)

I am currently working on an application which is supposed to become some sort of dropbox clone.

(我目前正在开发应成为某种保管箱克隆的应用程序。)

The idea is that I can upload and download files, but while the file is stored on the server it should be encrypted.

(我的想法是可以上传和下载文件,但是当文件存储在服务器上时,应该对其进行加密。)

I have already developed a nodejs backend.

(我已经开发了一个nodejs后端。)

The backend uses express to communicate with the frontend, which is a React application with axios.

(后端使用express与前端进行通信,前端是带有axios的React应用程序。)

The up- and download works fine.

(上载和下载工作正常。)

I use js-file-download and a simple input tag to upload files.

(我使用js-file-download和一个简单的输入标签来上传文件。)

My problem is now the encrypting/decrypting part.

(我的问题现在是加密/解密部分。)

All the libraries and examples I found only work with strings or objects but not with files.

(我发现的所有库和示例仅适用于字符串或对象,不适用于文件。)

Somehow I don't understand how to take a whole file like I have it in my code, extract the information, encrypt it and store it again in the file.

(不知何故,我不明白如何像在代码中一样获取整个文件,提取信息,对其进行加密并将其再次存储在文件中。)

Could somebody help me a little bit?

(有人可以帮我一点吗?)

Below you can see the code snippets to up- and download code from the frontend.

(在下面,您可以看到用于从前端升级和下载代码的代码片段。)

I think there are the best places to en- and decrypt.

(我认为有最好的加密和解密地方。)

Code to upload files

(代码上传文件)

 const uploadHandler = async (event) => { event.preventDefault() if(files.length === 0){ props.handleError('No files!', 5000) }else if(checkMimeType()){ console.log('start uploading') const data = new FormData() for(var x = 0; x<files.length; x++) { data.append('file', files[x]) } const response = await fileService.sendFiles(data) if(response.status === 200){ props.handleNotification(response.data, 5000) props.getFiles() } else{ props.handleError(response.data, 5000) } setFiles([]) } window.location.reload() } const onChangeHandler = (event) => { event.preventDefault() setFiles(event.target.files) } return ( <div className='container'> <div className='row'> <div className='col-md-15'> <div className='form-group files' > <Form method='POST' encType='multipart/form-data' onSubmit={uploadHandler} > <input type='file' autoComplete='off' name='files' multiple onChange= {onChangeHandler}/> <Button className='button' type="submit">Upload</Button> </Form> </div> </div> </div> </div> ) 

Code to download files:

(下载文件的代码:)

  const fileDownload = require('js-file-download') const handleSingleDownload = async (file) => { const response = await fileService.getFile(file.id) console.log(response.data) fileDownload(response.data, file.name) props.handleNotification('Download started...', 2500) } 

  ask by canon translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...