最近,我正在考虑构建一个小型,安全的服务,该服务允许用户上传文件并使用接收者的公钥对该文件进行编码。关键是我想即时执行此操作,因此上传之前不进行编码,而只是其中的一部分。我开始研究Internet,但找不到直接答案。 这有可能吗?
答案 0 :(得分:1)
想到了Streams API(尤其是TransformStream),但是我没有使用此新平台功能的经验。
您可能仍需要为其他浏览器构建一个先编码后上传。
该解决方案可能已经足够了。
但是将其用作附魔可能很好,检测该功能很简单:
if ('TransformStream' in window) {
答案 1 :(得分:1)
您必须先执行加密,然后再上传文件。
我提供了一个简单的客户端加密/解密示例(使用Caesar加密)。
基本上,它使用FileReader
API将文件的内容转换为ArrayBuffer
。使用类型化数组(在本例中为Uint8Array
),您可以修改原始ArrayBuffer
的每个字节,并执行加密。
加密完成后,您就可以继续发送数据了。
let input = document.getElementById('input');
let img = document.getElementById('myImg');
let caesarKey = document.getElementById('caesarKey');
let log = document.getElementById('log');
document.getElementById('encrypt').addEventListener('click',encrypt);
document.getElementById('decrypt').addEventListener('click',decrypt);
// variables to store the image data, a d the encrypted image data
let imgData;
let encryptedData;
// prepare the file reader, and the onload callback
let reader = new FileReader();
reader.onload = e=>{
log.innerHTML='Image ready! Press encrypt button...'
imgData = reader.result;
}
// declare the file input onchangen handler
input.addEventListener('change', e=> {
log.innerHTML='...processing image'
reader.readAsArrayBuffer(e.target.files[0]);
});
// caesar encrypt function
function encrypt(){
log.innerHTML='Encrypting...'
let key = Number( caesarKey.value )
encryptedData = new Uint8Array(imgData);
for(let i=0;i<encryptedData.byteLength;i++){
encryptedData[i] = encryptedData[i]+key;
}
log.innerHTML='Image encrypted! Press decrypt...'
}
// caesar decrypt function, and image output
function decrypt(){
log.innerHTML='Decrypting...'
let key = Number( caesarKey.value );
for(let i=0;i<encryptedData.byteLength;i++){
encryptedData[i] = encryptedData[i]-key;
}
// output the image
var blob = new Blob( [ encryptedData ], { type: "image/jpeg" } );
var urlCreator = window.URL || window.webkitURL;
var imageUrl = urlCreator.createObjectURL( blob );
img.src = imageUrl;
log.innerHTML='Image decrypted! '
}
#log{
padding:5px;
text-align:center;
background-color:blue;
color:white;
margin:5px;
}
<div id="log">--Select a jpg Image--</div>
<input type="file" accept=".jpg" id="input"><br>
Caesar Shift value:
<input type="text" id="caesarKey" value="5"><br>
<input type="button" value="Caesar Encrypt" id="encrypt">
<input type="button" value="Caesar Decrypt" id="decrypt">
<img id="myImg">
答案 2 :(得分:0)
例如在服务器端,您可以将下一个简单代码用于node.js
// Dependencies
const crypto = require('crypto');
const fs = require('fs');
const secret = 'password';
/*
* Crypt file with aes192
* @param {String} fileName to crypt with aes192 alghoritm and secret that contains password
* function create file with name fileName.enc
*
*/
const cryptFile = (fileName) => {
const cipher = crypto.createCipher('aes192', secret);
const input = fs.createReadStream(fileName);
const output = fs.createWriteStream(`${fileName}.enc`);
input.pipe(cipher).pipe(output);
};
/*
* Decrypt file with aes192
* @param {String} fileName to decrypt with aes192 alghoritm and secret that contains password
* function create file with name fileName
*
*/
const decryptFile = (fileName) => {
const decipher = crypto.createDecipher('aes192', secret);
const input = fs.createReadStream(`${fileName}.enc`);
const output = fs.createWriteStream(fileName);
input.pipe(decipher).pipe(output);
};