实现数据DLP加密鸿蒙示例代码 原创

鸿蒙场景化示例代码技术工程师
发布于 2025-2-27 16:37
浏览
0收藏

本文原创发布在华为开发者社区

介绍

数据防泄漏服务(Data Loss Prevention,简称为DLP),是系统提供的系统级的数据防泄漏解决方案,提供文件权限管理、加密存储、授权访问等能力,数据所有者可以基于账号认证对机密文件进行权限配置,允许拥有只读、编辑、拥有者权限,随后机密文件会通过密文存储,在支持DLP机制的设备上可以通过端云协调进行认证授权,获取对数据的访问和修改的能力。

本示例演示了如何创建、编辑一个dlp文件。

实现数据DLP加密源码地址

效果预览

实现数据DLP加密鸿蒙示例代码-鸿蒙开发者社区

使用说明

  1. 进入应用会看到两个按钮,根据文字提示点击第一个按钮可创建一个test.txt文件保存到本地,保存位置可选择。
  2. 点击第二个按钮,选择刚创建的test.txt文件,会显示已选择文件的信息,点击“生成dlp文件”按钮,可生成dlp文件。
  3. 再次点击第二个按钮,选择生成的txt.dlp文件,会直接打开该文件。

实现思路

创建test.txt文件并保存

使用@ohos.file.picker接口实现test.txt文件的创建和保存,使用@ohos.file.fs接口以同步方法打开创建的test.txt文件并写入数据。核心代码如下,源码参考

Index.ets

async saveFile () {
    let uri = ''
    try {
      let DocumentSaveOptions = new picker.DocumentSaveOptions();
      DocumentSaveOptions.newFileNames = ['test.txt'];
      let documentPicker = new picker.DocumentViewPicker();
      await documentPicker.save(DocumentSaveOptions).then((DocumentSaveResult: Array<string>) => {
        console.info(TAG, 'DocumentViewPicker.save successfully, DocumentSaveResult uri: ' + JSON.stringify(DocumentSaveResult));
        uri = DocumentSaveResult[0]
        return uri
      }).catch((err: BusinessError) => {
        console.error(TAG, 'DocumentViewPicker.save failed with err: ' + JSON.stringify(err));
        return uri
      });
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.error(TAG, 'DocumentViewPicker failed with err: ' + JSON.stringify(err));
      return uri
    }
    return uri
  }

  writeText (filePath: string) {
    if (!filePath) {
      return
    }
    let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
    let str: string = "hello, world";
    let writeLen = fs.writeSync(file.fd, str);
    console.info(TAG, "write data to file succeed and size is:" + writeLen);
    fs.closeSync(file);
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

生成dlp文件

先利用@ohos.file.picker接口选择一个文件,再使用@ohos.dlpPermission接口根据文件的fd,查询该文件是否是dlp文件。如果该文件不是dlp文件,则通过@ohos.app.ability.Want接口传入txt文件的数据,生成对应的dlp文件;如果该文件是dlp文件,则直接打开文件。
核心代码如下,源码参考

Index.ets,dlpPage.ets

  1. 选择一个文件
// 选择一个文件
  async callFilePickerSelectFile () {
    let DocumentSelectOptions = new picker.DocumentSelectOptions();
    DocumentSelectOptions.maxSelectNumber = 20;
    let documentPicker = new picker.DocumentViewPicker();
    documentPicker.select( DocumentSelectOptions ).then( ( DocumentSelectResult ) => {
      console.info( TAG , 'DocumentViewPicker.select successfully, DocumentSelectOptions : ' + JSON.stringify( DocumentSelectOptions ) );
      console.info( TAG , 'DocumentViewPicker.select successfully, DocumentSelectResult uri: ' + JSON.stringify( DocumentSelectResult ) );
      let editFlag = false;
      if ( DocumentSelectResult !== null && DocumentSelectResult !== undefined ) {
        DocumentSelectResult.forEach( ( value ) => {
          this.uri = value;
          editFlag = true;
          console.info( TAG , `select file uri: ${ this.uri }` );
        } )
      }
      if ( editFlag ) {
        this.getFilenameByUri( this.uri );
      }
    } ).catch( ( err: BusinessError ) => {
      console.error( TAG , 'DocumentViewPicker.select failed with err: ' + JSON.stringify( err ) );
    } );
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  1. 查询是否为dlp文件
/**
   * 是否为dlp文件
   */
  async isDlpFileFunc( fd: number ): Promise<boolean> {
    try {
      this.isDLPFile = await dlpPermission.isDLPFile( fd ); // 是否加密DLP文件
      console.info( TAG , 'isDlpFile:' + this.isDLPFile );
      this.inSandbox = await dlpPermission.isInSandbox();
      let msg = 'isDlpFile:' + true + ';inSandbox:' + this.inSandbox;
      dlpPermission.getDLPPermissionInfo().then( ( data: dlpPermission.DLPPermissionInfo ) => {
        console.info( TAG , 'getDLPPermissionInfo, result: ' + JSON.stringify( data ) );
        promptAction.showToast( { message : msg + ' permission:' + JSON.stringify( data ) , duration : 2000 } );
      } ).catch( ( err: BusinessError ) => {
        console.info( TAG , 'getDLPPermissionInfo: ' + JSON.stringify( err ) );
      } );
    } catch( err ) {
      console.error( TAG , 'isDLPFile error:' + (err as BusinessError).code + (err as BusinessError).message );
    }
    return this.isDLPFile;
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  1. 生成一个dlp文件
/**
   * 生成dlp文件
   */
  async onEncrypt() {
    console.info( TAG + 'new file and encrypt' );
    let context = getContext() as common.UIAbilityContext; // 获取当前UIAbilityContext
    let want: Want = {
      "action" : "ohos.want.action.editData" ,
      "bundleName" : "com.ohos.dlpmanager" ,
      "type" : "text/plain" ,
      "abilityName" : "MainAbility" ,
      "uri" : this.dlpUri ,
      "parameters" : {
        "displayName" : this.dlpName ,
        "fileName" : { "name" : this.dlpName } ,
        "linkFileName" : { "name" : this.dlpLinkName } ,
      }
    };
    try {
      console.info( TAG , "openDLPFile:" + JSON.stringify( want ) );
      context.startAbility( want , ( error: BusinessError ) => {
        console.error( TAG , 'want error.code =' + error.code + ',error.message =' + error.message );
      } );
    } catch( err ) {
      console.error( 'openDLPFile startAbility failed' + (err as BusinessError).code + (err as BusinessError).message );
      return;
    }
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
已于2025-2-28 14:50:03修改
收藏
回复
举报


回复
    相关推荐