HarmonyOS 鸿蒙Next 数据加密问题

发布于 1周前 作者 ionicwang 来自 鸿蒙OS

HarmonyOS 鸿蒙Next 数据加密问题

java中的加密方式RSA/ECB/PKCS1Padding 对应鸿蒙的是什么,文档中找不到对应的方法。尝试写了之后,发现一直报错Error: create C cipher fail! 数据加密

2 回复

您可以学习鸿蒙官方提供的加解密算法框架然后进行相应的数据加密,参考链接如下:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-encrypt-decrypt-dev-V5

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-cryptoframework-V5

您可以在官网文档的示例代码中下载加解密相关的代码demo进行参考和使用,对应链接如下:

https://developer.huawei.com/consumer/cn/doc/best-practices-V5/bpta-cross-platform-compatibility-V5

您可以页面全局搜索“加解密”来快速定位至相应的示例代码处。

您可以参考如下demo进行加解密功能实现:

import cryptoFramework from '@ohos.security.cryptoFramework';

import buffer from '@ohos.buffer';

// 加密消息

async function encryptMessagePromise(publicKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) {

  let cipher = cryptoFramework.createCipher('RSA1024|PKCS1');

  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, publicKey, null);

  let encryptData = await cipher.doFinal(plainText);

  return encryptData;

}

// 解密消息

async function decryptMessagePromise(privateKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) {

  let decoder = cryptoFramework.createCipher('RSA1024|PKCS1');

  await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, privateKey, null);

  let decryptData = await decoder.doFinal(cipherText);

  return decryptData;

}

// 生成RSA密钥对

async function genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) {

  let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData };

  let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData };

  let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');

  let keyPair = await rsaGenerator.convertKey(pubKeyBlob, priKeyBlob);

  console.info('convertKey success');

  return keyPair;

}

async function main() {

  let pkData = new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 197, 64, 10, 198, 14, 110, 65, 92, 206, 35, 28, 123, 153, 24, 134, 255, 145, 74, 42, 173, 40, 215, 146, 58, 143, 46, 10, 195, 154, 160, 69, 196, 220, 152, 179, 44, 111, 200, 84, 78, 215, 73, 210, 181, 12, 29, 70, 68, 36, 135, 153, 89, 230, 202, 130, 212, 111, 243, 234, 92, 131, 62, 145, 50, 73, 48, 104, 245, 46, 70, 45, 157, 147, 143, 140, 162, 156, 216, 220, 49, 121, 142, 194, 33, 223, 201, 0, 16, 163, 210, 240, 118, 92, 147, 121, 220, 17, 114, 24, 52, 125, 135, 176, 88, 21, 83, 86, 17, 156, 88, 250, 48, 79, 86, 128, 248, 105, 208, 133, 140, 13, 153, 164, 191, 136, 164, 44, 53, 2, 3, 1, 0, 1]);

  let skData = new Uint8Array([48, 130, 2, 119, 2, 1, 0, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 4, 130, 2, 97, 48, 130, 2, 93, 2, 1, 0, 2, 129, 129, 0, 197, 64, 10, 198, 14, 110, 65, 92, 206, 35, 28, 123, 153, 24, 134, 255, 145, 74, 42, 173, 40, 215, 146, 58, 143, 46, 10, 195, 154, 160, 69, 196, 220, 152, 179, 44, 111, 200, 84, 78, 215, 73, 210, 181, 12, 29, 70, 68, 36, 135, 153, 89, 230, 202, 130, 212, 111, 243, 234, 92, 131, 62, 145, 50, 73, 48, 104, 245, 46, 70, 45, 157, 147, 143, 140, 162, 156, 216, 220, 49, 121, 142, 194, 33, 223, 201, 0, 16, 163, 210, 240, 118, 92, 147, 121, 220, 17, 114, 24, 52, 125, 135, 176, 88, 21, 83, 86, 17, 156, 88, 250, 48, 79, 86, 128, 248, 105, 208, 133, 140, 13, 153, 164, 191, 136, 164, 44, 53, 2, 3, 1, 0, 1, 2, 129, 128, 70, 75, 184, 139, 53, 1, 94, 17, 240, 244, 218, 101, 193, 253, 215, 190, 164, 204, 197, 192, 200, 89, 107, 39, 171, 119, 65, 38, 204, 168, 105, 180, 234, 217, 16, 161, 185, 132, 175, 103, 25, 154, 153, 153, 36, 36, 26, 178, 150, 66, 45, 8, 185, 19, 90, 228, 210, 177, 30, 200, 177, 141, 78, 184, 248, 59, 113, 154, 145, 73, 160, 24, 73, 157, 86, 207, 186, 32, 95, 200, 106, 252, 107, 69, 170, 193, 216, 196, 181, 142, 74, 203, 15, 18, 89, 228, 152, 19, 239, 21, 233, 98, 121, 214, 57, 187, 111, 239, 223, 248, 199, 70, 223, 108, 108, 113, 234, 144, 155, 95, 246, 144, 244, 122, 39, 55, 127, 81, 2, 65, 0, 246, 96, 188, 0, 0, 104, 221, 105, 139, 144, 63, 175, 209, 87, 179, 162, 88, 192, 99, 82, 125, 53, 54, 48, 70, 245, 239, 37, 15, 242, 247, 84, 115, 187, 196, 95, 156, 40, 165, 60, 64, 102, 13, 229, 243, 2, 149, 0, 232, 226, 221, 192, 95, 11, 12, 208, 5, 181, 98, 62, 210, 190, 141, 235, 2, 65, 0, 204, 244, 34, 10, 105, 80, 76, 116, 163, 35, 231, 168, 187, 206, 189, 101, 215, 103, 80, 115, 86, 11, 34, 127, 203, 114, 84, 188, 121, 174, 169, 31, 142, 2, 182, 27, 140, 225, 157, 227, 71, 98, 15, 203, 187, 213, 5, 190, 20, 121, 8, 30, 193, 100, 232, 101, 141, 8, 124, 20, 29, 78, 6, 95, 2, 65, 0, 204, 43, 225, 224, 6, 118, 224, 117, 100, 200, 199, 94, 70, 23, 109, 175, 173, 232, 208, 230, 61, 8, 105, 189, 156, 48, 150, 91, 154, 89, 248, 136, 173, 215, 254, 166, 84, 220, 130, 1, 234, 68, 40, 100, 84, 251, 224, 202, 254, 51, 115, 28, 198, 38, 124, 25, 175, 129, 94, 199, 61, 17, 216, 189, 2, 64, 72, 230, 129, 129, 48, 138, 134, 87, 106, 123, 231, 247, 165, 173, 216, 194, 115, 198, 228, 223, 209, 120, 46, 114, 68, 92, 75, 117, 170, 214, 140, 131, 147, 208, 181, 19, 193, 157, 178, 186, 87, 246, 178, 101, 166, 79, 20, 54, 211, 51, 101, 199, 2, 197, 48, 192, 134, 84, 193, 69, 170, 82, 201, 131, 2, 65, 0, 213, 165, 55, 166, 131, 210, 195, 56, 250, 147, 195, 61, 205, 208, 189, 185, 40, 52, 50, 119, 137, 23, 246, 46, 220, 108, 52, 23, 152, 154, 94, 32, 144, 195, 184, 249, 21, 168, 12, 57, 222, 18, 60, 117, 81, 157, 72, 30, 155, 190, 165, 242, 228, 139, 240, 184, 145, 170, 103, 210, 160, 161, 135, 13]);

  let keyPair = await genKeyPairByData(pkData, skData);

  let pubKey = keyPair.pubKey;

  let priKey = keyPair.priKey;

  let message = 'This is a test';

  // 把字符串按utf-8解码为Uint8Array

  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };

  let encryptText = await encryptMessagePromise(pubKey, plainText);

  console.info('encrypted result string:' + JSON.stringify(encryptText));

  let decryptText = await decryptMessagePromise(priKey, encryptText);

  if (plainText.data.toString() === decryptText.data.toString()) {

    console.info('decrypt ok');

    // 把Uint8Array按utf-8编码为字符串

    let messageDecrypted = buffer.from(decryptText.data).toString('utf-8');

    console.info('decrypted result string:' + messageDecrypted);

  } else {

    console.error('decrypt failed');

  }

}

@Entry

@Component

struct Index {

  @State message: string = 'Hello World';

  build() {

    Row() {

      Column() {

        Button(this.message)

          .fontSize(50)

          .fontWeight(FontWeight.Bold)

          .onClick(()=>{

            main()

          })

      }

      .width('100%')

    }

    .height('100%')

  }

}

更多关于HarmonyOS 鸿蒙Next 数据加密问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对HarmonyOS 鸿蒙Next的数据加密问题,以下提供直接的技术说明:

HarmonyOS 鸿蒙Next在数据加密方面采用了先进的加密机制,确保用户数据的安全性和隐私保护。其加密框架涵盖了多种加密算法和技术,包括但不限于对称加密、非对称加密以及哈希算法。

  1. 对称加密:通过相同的密钥进行加密和解密,适用于大量数据的快速加密处理。

  2. 非对称加密:使用公钥和私钥对进行加密和解密,确保数据传输的机密性和完整性,常用于身份验证和数字签名。

  3. 哈希算法:用于数据完整性校验,通过哈希函数将任意长度的数据转换为固定长度的哈希值,确保数据在传输过程中未被篡改。

HarmonyOS 鸿蒙Next还提供了数据保护机制,如设备级加密和文件系统加密,进一步增强了数据的安全性。此外,它还支持应用级加密,允许开发者在开发过程中为应用数据添加额外的加密层。

请注意,HarmonyOS 鸿蒙Next的加密机制是复杂的,具体实现可能因设备型号、系统版本和应用场景而异。如需深入了解或解决特定问题,建议直接查阅HarmonyOS的官方文档或资源。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部