HarmonyOS 针对流形式文件(zip文件)AES解密,再zip解压

1,后端接口下发一个zip二进制文件,aes加密过的

2,在客户端需要用aes解密,然后再对zip解压。zip解压有api:ohos.zlib可以实现。

请问针对aes加密的zip文件有方法可以解密吗?

HarmonyOS
2024-08-12 15:57:35
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

将文件转为二进制数组传入加解密方法中可以进行加密和解密操作,具体代码参考:

/*加密*/ 
export async function aesEncrypt(text:Uint8Array,puKey:string): Promise<string>{ 
  let globalResult = "" 
  try { 
    let cipherAlgName = 'AES128|ECB|PKCS7'; 
    let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
    let symAlgName = 'AES128'; 
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
    let dataUint8Array = base.decodeSync(puKey) 
    let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array } 
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob) 
    await globalCipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, promiseSymKey, null); 
    let result = await globalCipher.doFinal({data:text}) 
    globalResult = base.encodeToStringSync(result.data); 
  } catch (err) { 
    console.log(err.message) 
  } 
  return globalResult; 
} 
 
// 解密 
export async function aesDecrypt(text: string, key: string) { 
  let globalResult = "" 
  try { 
    let cipherAlgName = 'AES128|ECB|PKCS7'; 
    let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
    let symAlgName = 'AES128'; 
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
    let dataUint8Array = base.decodeSync(key) 
    let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array } 
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob) 
    await globalCipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, promiseSymKey, null); 
    let plainText: cryptoFramework.DataBlob = { data: base.decodeSync(text) } 
    let result = await globalCipher.doFinal(plainText)//解密结果 
  } catch (err) { 
    console.log(err.message) 
  } 
  return globalResult; 
}

对于加解密的规格和密钥规格可以参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-sym-encrypt-decrypt-spec-0000001774120458-V5?catalogVersion=V5

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-aes-sym-encrypt-decrypt-ecb-0000001898674577-V5

可以读取文件信息,获知文件大小size:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-fs-V5#fsstat

再自行按定长读取文件内容,分段AES解密

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-fs-V5#readsync10

/** 
 * aes文件加密 
 */ 
function encryptFile(srcPath, outPath) { 
  return new Promise(resolve => { 
    try { 
      if (!srcPath || !outPath) { 
        return resolve({code: 0, message: '路径srcPath或outPath为空!'}); 
      } 
      /* 
      * outPath:'/Users/xxx/WorkSpace/zt-repository/offline-pack-platform/pack-server/uploads/online/encrypt/aeron/using.zip 
      * ///opt/cephfs_workspace/h5-offline/uploads/online/encrypt/commonRes/UahFgeGd/v10003 
      * 首先去除文件名再创建目录 
      * */ 
      let len = outPath.lastIndexOf('/'); 
      let outDir = outPath.substr(0, len); 
      fse.ensureDir(outDir, (err) => { 
        if (err) { 
          return resolve({code: 0, message: `创建目录${outDir}失败:${err.message}`}); 
        } 
 
        const keyVi = getCbcKeyVi(); 
        console.log('keyVi=', keyVi,srcPath,outPath); 
        const cipher = crypto.createCipheriv(__algorithm__, keyVi.key, keyVi.vi); 
        const rs = fs.createReadStream(srcPath); 
        const ws = fs.createWriteStream(outPath); 
        //console.log(111111,cipher) 
        rs.pipe(cipher) 
          .pipe(ws) 
          .on('error', function (err) { 
            console.log(`encryptFile err:${err.message}`); 
            return resolve({ 
              code: 0, 
              path: null, 
              msg: err && err.message 
            }); 
          }) 
          .on(`finish`, function () { 
            console.log(`encryptFile done!`); 
            return resolve({ 
              code: 1, 
              path: outPath 
            }); 
          }); 
      }); 
    } catch (err) { 
      return resolve({ 
        code: 0, 
        path: null, 
        msg: err.message 
      }); 
    } 
  }); 
} 
 
/** 
 * aes文件解密 
 */ 
function decryptFile(srcPath, outPath) { 
  const keyVi = getCbcKeyVi(); 
  //console.log('keyVi=', keyVi); 
 
  const deS = crypto.createCipheriv(__algorithm__, keyVi.key, keyVi.vi); 
  const rs = fs.createReadStream(srcPath); 
  const ws = fs.createWriteStream(outPath); 
 
  rs.pipe(deS) 
    .pipe(ws) 
    .on('error', function (err) { 
      console.log(`decryptFile err:${err.message}

类似JS的原生方法如上所示可以参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-fs-V5#fscreatestream

分享
微博
QQ
微信
回复
2024-08-12 19:34:12
相关问题
HarmonyOS如何解压rawfile中的zip文件
283浏览 • 1回复 待解决
HarmonyOS ZIP文件加密压缩和解压
586浏览 • 1回复 待解决
鸿蒙里如何通过代码解压zip文件
8浏览 • 0回复 待解决
zip包的解压是在主线程还是IO线程
1563浏览 • 1回复 待解决
将rawfile中zip复制并解压到沙箱路径中
1235浏览 • 1回复 待解决
是否有将文件夹压缩打包成zip的API?
795浏览 • 1回复 待解决
HarmonyOS AES解密报错
505浏览 • 1回复 待解决
实现文件解压缩和数据流解压
1231浏览 • 1回复 待解决
如何以流的形式读写文件
611浏览 • 1回复 待解决
HarmonyOS AES解密与java无法通用
387浏览 • 1回复 待解决
使用AES解密时,一直在报解密异常
98浏览 • 1回复 待解决