首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用browserpgp javascript工具加密zip文件

用browserpgp javascript工具加密zip文件
EN

Stack Overflow用户
提问于 2022-06-13 16:09:19
回答 1查看 193关注 0票数 1

我想使用OpenPGP.js (https://browserpgp.github.io/)加密压缩文件。所有处理都需要在客户端完成,而不需要Node.js的参与。使用基于javascript的browserpgp.js,明文文件的加密很容易,但是压缩文件的加密是很困难的。我正在附加用于加密zip文件的代码。因为zip文件的内容不是文本,所以我使用指南来处理来自https://github.com/openpgpjs/openpgpjs的二进制数据。我认为压缩文件加密后的结果文件应该是原始/二进制格式,从下面的加密代码中保存的结果文件应该是二进制文件。但是,我验证了它,它不能被解密。

我正在使用Kleopatra桌面OpenPGP工具(https://www.openpgp.org/software/kleopatra/)来验证所得到的加密文件。使用该工具,我测试了是否可以使用我的私钥解密加密文件。生成的二进制文件不能使用Kleopatra工具解密。因此,我想知道这段代码有什么问题,因为生成的文件应该可以使用Kleopatra.解密。

您可以使用以下工具创建两个公钥:https://browserpgp.github.io

代码语言:javascript
复制
function openpgp_encryptZIPFile(){  
  var zip = new JSZip();
  zip.file("Hello.txt", "Hello World\n");
  var img = zip.folder("images");
  zip.generateAsync({type:"blob"})
  .then(function(content) {
      console.log('contents: ' + content);     
      encryptedZipFile = OpenPGPEncryptDataZipFile(content);
  });  
}

async function OpenPGPEncryptDataZipFile(zipBlob) 
{    
  //This is my public key
  const key1 = `somekey1`;  //you would have to put a public key here 
    //This is the testuser public key
  const key2 = `somekey2`; //you would have to put another public key here 
  const publicKeysArmored = [key1, key2];
  //create a combined key
  const publicKeys = await Promise.all(publicKeysArmored.map(armoredKey => openpgp.readKey({ armoredKey }))); 
    
  var binaryData = new Uint8Array(zipBlob);  
  
  //https://github.com/openpgpjs/openpgpjs/blob/main/README.md
  //For OpenPGP.js v4 syntax is:  const message = openpgp.Message.fromBinary(binaryData); 
  //For OpenPGP.js v5 syntax is:  const message2 = await openpgp.createMessage({ binary: binaryData });
  //`const {data: encrypted}' OR `const { message }' OR just `const encrypted'
  const  encrypted  = await openpgp.encrypt({
  message: await openpgp.createMessage({ binary: binaryData }),      
     encryptionKeys: publicKeys,
     //signingKeys: privateKey // optional
     format: 'binary'
  });
  console.log('encrypted: ' + encrypted);
  var encryptedBlob = new Blob([encrypted],{type: 'text/plain'});    
  //var encryptedBlob = new Blob([encrypted], {type: "octet/stream"});
  saveAs(encryptedBlob, 'test.zip.enc' ); 
}
EN

回答 1

Stack Overflow用户

发布于 2022-06-15 06:45:07

OpenPGPjs github回购公司的人回答了这个问题

new Uint8Array(zipBlob)很可能不会像你想的那样做。也许尝试一下new Uint8Array(await zipBlob.arrayBuffer()),或者直接向JSZip要一个Uint8Array,而不是一个Blob。

此外,八进制/流不是有效的MIME类型。也许你指的是应用程序/八位流?

所以我把new Uint8Array(zipBlob)改成了new Uint8Array(await zipBlob.arrayBuffer()),它起了作用。

https://github.com/openpgpjs/openpgpjs/discussions/1535

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72605881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档