HarmonyOS rsa加密返回401错误

使用rsa非对称加密对网络接口参数进行加密,返回401错误

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

参考以下代码:

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


@Entry
@Component
struct DisplayTest {
  async encryptMessagePromise(publicKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) {
    console.log('this is encryptMessagePromise start')
    let cipher = cryptoFramework.createCipher('RSA1024|PKCS1');
    await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, publicKey, null);
    let encryptData = await cipher.doFinal(plainText);
    console.log('this is encryptMessagePromise end')
    return encryptData;
  }

  async genKeyPairByData(pubKeyData: Uint8Array) {
    console.log('this is genKeyPairByData start')
    // let pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]);
    // let pubKeyBlob1: cryptoFramework.DataBlob = { data: pubKeyArray };
    let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData };
    console.log('this is genKeyPairByData createAsyKeyGenerator start')
    let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');
    console.log('this is genKeyPairByData createAsyKeyGenerator end')
    let keyPair = await rsaGenerator.convertKey(pubKeyBlob, null);
    console.info('convertKey success');
    return keyPair;
  }

  async getRsa(message: string) {
    let pkData = new Uint8Array(buffer.from('abcdJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==', 'base64').buffer)
    let keyPair = await this.genKeyPairByData(pkData);
    let pubKey = keyPair.pubKey;
    // 把字符串按utf-8解码为Uint8Array
    let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
    let encryptText = await this.encryptMessagePromise(pubKey, plainText);
    return encryptText
  }


  build() {
    Column({space:20}){

      Text('SO₂')
        .width('100%')


      Button('Test')
        .type(ButtonType.Capsule)
        .onClick(() => {
          this.getRsa('1719467841962')

        })
        .width('50%')
      // Image($rawfile('test/background.png'))
      // .size({width:50,height:50})
    }
    .width("100%")
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}

可以参考下面代码转换Uint8Array

let pkData = new Uint8Array(buffer.from('abcdJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==', 'base64').buffer)
let tes = pkData.buffer
let sss = buffer.from(tes,0,tes.byteLength)
console.log(sss.toString('base64'))
分享
微博
QQ
微信
回复
2天前
相关问题
请求广告返回错误401
2358浏览 • 1回复 待解决
request上传返回错误401
455浏览 • 2回复 待解决
HarmonyOS RSA加密方式
685浏览 • 1回复 待解决
HarmonyOS 实现RSA加密功能
627浏览 • 1回复 待解决
HarmonyOS RSA分段加密乱码
645浏览 • 1回复 待解决
HarmonyOS rsa加密报错
62浏览 • 1回复 待解决
HarmonyOS RSA加密失败问题
62浏览 • 1回复 待解决
HarmonyOS RSA加密算法实现
70浏览 • 1回复 待解决
RSA NoPadding模式加密失败
1278浏览 • 1回复 待解决
HarmonyOS RSA私钥加密公钥解密
71浏览 • 1回复 待解决
HarmonyOS RSA加密中,公私钥问题
51浏览 • 1回复 待解决
HarmonyOS 请问ArkTS如何实现RSA加密
534浏览 • 1回复 待解决
RSA加密方法在HarmonyOS 的实现
49浏览 • 1回复 待解决
RSA 加密的具体使用方法
255浏览 • 1回复 待解决
HarmonyOS RSA加密的base64秘钥转换
51浏览 • 1回复 待解决
如何使用RSA公钥加密明文数据
498浏览 • 1回复 待解决
RSA加密,使用自有私钥解密报错
276浏览 • 1回复 待解决