色哟哟视频在线观看-色哟哟视频在线-色哟哟欧美15最新在线-色哟哟免费在线观看-国产l精品国产亚洲区在线观看-国产l精品国产亚洲区久久

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線(xiàn)課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

鴻蒙OS開(kāi)發(fā)問(wèn)題:(ArkTS)【 RSA加解密,解決中文亂碼等現(xiàn)象】

jf_46214456 ? 來(lái)源:jf_46214456 ? 作者:jf_46214456 ? 2024-03-27 21:23 ? 次閱讀

RSA加解密開(kāi)始構(gòu)建工具類(lèi)就是舉步維艱,官方文檔雖然很全,但是還是有很多小瑕疵,在自己經(jīng)過(guò)幾天的時(shí)間,徹底解決了中文亂碼的問(wèn)題、分段加密的問(wèn)題。

首先看官方示例代碼(以RSA非對(duì)稱(chēng)加解密(多次調(diào)用doFinal實(shí)現(xiàn)分段)為例:):

import cryptoFramework from "@ohos.security.cryptoFramework"

function stringToUint8Array(str) {
  var arr = [];
  for (var i = 0, j = str.length; i < j; ++i) {
    arr.push(str.charCodeAt(i));
  }
  var tmpArray = new Uint8Array(arr);
  return tmpArray;
}

// 字節(jié)流轉(zhuǎn)成可理解的字符串
function uint8ArrayToString(array) {
  let arrayString = '';
  for (let i = 0; i < array.length; i++) {
    arrayString += String.fromCharCode(array[i]);
  }
  return arrayString;
}

function encryptLongMessagePromise() {
  let globalPlainText = "This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
  "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
  "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
  "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
  "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
  "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
  "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
  "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!";
  let globalCipherOutput;
  let globalDecodeOutput;
  var globalKeyPair;
  let plainTextSplitLen = 64; // RSA每次加解密允許的原文長(zhǎng)度大小與密鑰位數(shù)和填充模式等有關(guān),詳細(xì)規(guī)格內(nèi)容見(jiàn)overview文檔
  let cipherTextSplitLen = 128; // RSA密鑰每次加密生成的密文數(shù)據(jù)長(zhǎng)度計(jì)算方式:密鑰位數(shù)/8
  let keyGenName = "RSA1024";
  let cipherAlgName = "RSA1024|PKCS1";
  let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator(keyGenName); // 創(chuàng)建非對(duì)稱(chēng)密鑰生成器對(duì)象
  let cipher = cryptoFramework.createCipher(cipherAlgName); // 創(chuàng)建加密Cipher對(duì)象
  let decoder = cryptoFramework.createCipher(cipherAlgName); // 創(chuàng)建解密Decoder對(duì)象
  return new Promise((resolve, reject) = > {
    setTimeout(() = > {
      resolve("testRsaMultiDoFinal");
    }, 10);
  }).then(() = > {
    return asyKeyGenerator.generateKeyPair(); // 生成rsa密鑰
  }).then(keyPair = > {
    globalKeyPair = keyPair; // 保存到密鑰對(duì)全局變量
    return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, globalKeyPair.pubKey, null);
  }).then(async () = > {
    globalCipherOutput = [];
    // 將原文按64字符進(jìn)行拆分,循環(huán)調(diào)用doFinal進(jìn)行加密,使用1024bit密鑰時(shí),每次加密生成128B長(zhǎng)度的密文
    for (let i = 0; i < (globalPlainText.length / plainTextSplitLen); i++) {
      let tempStr = globalPlainText.substr(i * plainTextSplitLen, plainTextSplitLen);
      let tempBlob = { data : stringToUint8Array(tempStr) };
      let tempCipherOutput = await cipher.doFinal(tempBlob);
      globalCipherOutput = globalCipherOutput.concat(Array.from(tempCipherOutput.data));
    }
    console.info(`globalCipherOutput len is ${globalCipherOutput.length}, data is: ${globalCipherOutput.toString()}`);
    return;
  }).then(() = >{
    return decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, globalKeyPair.priKey, null);
  }).then(async() = > {
    globalDecodeOutput = [];
    // 將密文按128B進(jìn)行拆分解密,得到原文后進(jìn)行拼接
    for (let i = 0; i < (globalCipherOutput.length / cipherTextSplitLen); i++) {
      let tempBlobData = globalCipherOutput.slice(i * cipherTextSplitLen, (i + 1) * cipherTextSplitLen);
      let message = new Uint8Array(tempBlobData);
      let tempBlob = { data : message };
      let tempDecodeOutput = await decoder.doFinal(tempBlob);
      globalDecodeOutput += uint8ArrayToString(tempDecodeOutput.data);
    }
    if (globalDecodeOutput === globalPlainText) {
      console.info(`encode and decode success`);
    } else {
      console.info(`encode and decode error`);
    }
    return;
  }).catch(error = > {
    console.error(`catch error, ${error.code}, ${error.message}`);
  })
}
let plainTextSplitLen = 64; // RSA每次加解密允許的原文長(zhǎng)度大小與密鑰位數(shù)和填充模式等有關(guān),詳細(xì)規(guī)格內(nèi)容見(jiàn)overview文檔

注意點(diǎn):在解密中,這句代碼就是產(chǎn)生中文亂碼的關(guān)鍵。

鴻蒙OS開(kāi)發(fā)更多內(nèi)容↓點(diǎn)擊HarmonyOSOpenHarmony技術(shù)
鴻蒙技術(shù)文檔開(kāi)發(fā)知識(shí)更新庫(kù)gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md在這。或+mau123789學(xué)習(xí),是v喔

搜狗高速瀏覽器截圖20240326151450.png

globalDecodeOutput += uint8ArrayToString(tempDecodeOutput.data);

好,加上我的代碼

加密:

/**
 * 測(cè)試RSA加密
 */
export function textRsaEncryption(value: string) {
  let keyGenName = "RSA1024";
  let cipherAlgName = "RSA1024|PKCS1";
  //64 RSA每次加解密允許的原文長(zhǎng)度大小與密鑰位數(shù)和填充模式等有關(guān),詳細(xì)規(guī)格內(nèi)容見(jiàn)overview文檔
  let plainTextSplitLen = 117;
  let globalKeyPair; //密鑰對(duì)
  let globalEncryptionOutput; //加密輸出
  let arrTest = StringUtils.string2Uint8Array1(value);
  //創(chuàng)建非對(duì)稱(chēng)密鑰生成器對(duì)象
  let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator(keyGenName);
  // 創(chuàng)建加密Cipher對(duì)象
  let cipherEncryption = cryptoFramework.createCipher(cipherAlgName);

  return new Promise((resolve, reject) = > {
    setTimeout(() = > {
      resolve("textRsaEncryption");
    }, 10);
  })
    .then(() = > {
      let base64 = Base64.getInstance()
      let pubKeyBlob = { data: new Uint8Array(base64.decode(publicKey)) }
      let priKeyBlob = { data: new Uint8Array(base64.decode(privateKey)) }
      return asyKeyGenerator.convertKey(pubKeyBlob, priKeyBlob);
    })
    .then(keyPair = > {
      globalKeyPair = keyPair; // 保存到密鑰對(duì)全局變量
      return cipherEncryption.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, globalKeyPair.pubKey, null);
    }).then(async () = > {
      globalEncryptionOutput = [];

      // 將原文按64字符進(jìn)行拆分,循環(huán)調(diào)用doFinal進(jìn)行加密,使用1024bit密鑰時(shí),每次加密生成128B長(zhǎng)度的密文
      for (let i = 0; i < (arrTest.length / plainTextSplitLen); i++) {
        let tempArr = arrTest.slice(i * plainTextSplitLen, (i + 1) * plainTextSplitLen);
        let tempBlob = { data: tempArr };
        let tempCipherOutput = await cipherEncryption.doFinal(tempBlob);
        globalEncryptionOutput = globalEncryptionOutput.concat(Array.from(tempCipherOutput.data));
      }
      let base64 = Base64.getInstance()
      let enStr = base64.encode(globalEncryptionOutput)
      LogUtils.i("加密總長(zhǎng)度:" + globalEncryptionOutput.length + "n生成加密串:n" + enStr)
      return enStr
    })
    .catch(error = > {
      LogUtils.i(`加密異常, ${error.code}, ${error.message}`);
    })
}復(fù)制

解密:

/**
 * 測(cè)試RSA解密
 */
export function textRsaDecryption(value: string) {
  let keyGenName = "RSA1024";
  let cipherAlgName = "RSA1024|PKCS1";
  // RSA密鑰每次加密生成的密文數(shù)據(jù)長(zhǎng)度計(jì)算方式:密鑰位數(shù)/8
  let cipherTextSplitLen = 128;
  let globalKeyPair; //密鑰對(duì)
  //創(chuàng)建非對(duì)稱(chēng)密鑰生成器對(duì)象
  let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator(keyGenName);
  // 創(chuàng)建解密Decoder對(duì)象
  let cipherDecryption = cryptoFramework.createCipher(cipherAlgName);

  return new Promise((resolve, reject) = > {
    setTimeout(() = > {
      resolve("textRsaEncryption");
    }, 10);
  })
    .then(() = > {
      let base64 = Base64.getInstance()
      let pubKeyBlob = { data: new Uint8Array(base64.decode(publicKey)) }
      let priKeyBlob = { data: new Uint8Array(base64.decode(privateKey)) }
      return asyKeyGenerator.convertKey(pubKeyBlob, priKeyBlob);
    })
    .then(keyPair = > {
      globalKeyPair = keyPair; // 保存到密鑰對(duì)全局變量
      return cipherDecryption.init(cryptoFramework.CryptoMode.DECRYPT_MODE, globalKeyPair.priKey, null);
    }).then(async () = > {
      let base64 = Base64.getInstance()
      let globalCipherOutput1 = new Uint8Array(base64.decode(value))
      let len = globalCipherOutput1.length
      //解密輸出
      let globalDecryptionOutput = new Uint8Array(len);
      let globalOffset = 0
      // 將密文按128B進(jìn)行拆分解密,得到原文后進(jìn)行拼接
      for (let i = 0; i < (len / cipherTextSplitLen); i++) {
        let tempBlobData = globalCipherOutput1.subarray(i * cipherTextSplitLen, (i + 1) * cipherTextSplitLen);
        let message = new Uint8Array(tempBlobData);
        let tempBlob = { data: message };
        let tempDecodeOutput = await cipherDecryption.doFinal(tempBlob);
        //存入數(shù)組 解決邊累加邊轉(zhuǎn)中文時(shí) 字節(jié)錯(cuò)亂出現(xiàn)亂碼
        globalDecryptionOutput.set(tempDecodeOutput.data, globalOffset)
        //偏移量
        globalOffset += tempDecodeOutput.data.byteLength
      }
      let result = StringUtils.uint8Array2String(globalDecryptionOutput)
      LogUtils.i("解密串:cipherAlgName[" + cipherAlgName + "]n" + result);
    })
    .catch(error = > {
      LogUtils.i(`解密異常,cipherAlgName[${cipherAlgName}] ${error.code}, ${error.message}`);
    })
}復(fù)制

運(yùn)行代碼:

Text("RSA加解密聯(lián)測(cè)")
          .TextNormalStyle()
          .fontSize(16)
          .fontWeight(FontWeight.Normal)
          .fontColor(Color.White)
          .textAlign(TextAlign.Center)
          .margin({ left: 5 })
          .layoutWeight(1)
          .onClick(() = > {
            let globalPlainText = ""
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "一二三四五六七八九十"
            globalPlainText += "123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/123456789/"
            globalPlainText += "SDK向DevEco Studio提供全量API,DevEco Studio識(shí)別開(kāi)發(fā)者項(xiàng)目中選擇的設(shè)備形態(tài),找到該設(shè)備的支持能力集,篩選支持能力集包含的API并提供API聯(lián)想"
            //
            textRsaEncryption(globalPlainText)
              .then(enStr = > {
                if (enStr) textRsaDecryption(enStr)
              })
          })
      }
      .width('100%')
      .height(50)
      .margin({ top: 10 })
      .padding(5)復(fù)制

運(yùn)行結(jié)果:

cke_155247.png

終于大功告成!!

審核編輯 黃宇

聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • API
    API
    +關(guān)注

    關(guān)注

    2

    文章

    1505

    瀏覽量

    62170
  • RSA
    RSA
    +關(guān)注

    關(guān)注

    0

    文章

    59

    瀏覽量

    18908
  • OpenHarmony
    +關(guān)注

    關(guān)注

    25

    文章

    3728

    瀏覽量

    16401
  • 鴻蒙OS
    +關(guān)注

    關(guān)注

    0

    文章

    189

    瀏覽量

    4459
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    鴻蒙OS開(kāi)發(fā)實(shí)戰(zhàn):【ArkTS 實(shí)現(xiàn)MQTT協(xié)議(2)】

    1. 協(xié)議傳輸通道僅為T(mén)CPSocket 2. 基于HarmonyOS SDK API 9開(kāi)發(fā) 3. 開(kāi)發(fā)語(yǔ)言:ArkTS,TypeScript
    的頭像 發(fā)表于 04-01 14:48 ?1580次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開(kāi)發(fā)</b>實(shí)戰(zhàn):【<b class='flag-5'>ArkTS</b> 實(shí)現(xiàn)MQTT協(xié)議(2)】

    labviewAES加解密小程序

    本帖最后由 eehome 于 2013-1-5 10:10 編輯 使用AES算法對(duì)128bit加密和解密版本:LabVIEW 8.6 8.6.1作者:zerld功能:使用AES算法對(duì)128bit加密和解密為便于閱讀,算法還留有優(yōu)化空間。本
    發(fā)表于 02-22 14:12

    加解密

    stm32F4中如何看加解密的代碼
    發(fā)表于 04-08 11:23

    硬件加解密的分類(lèi)

    文章目錄1、硬件加解密的分類(lèi)2、ARM-CE / ARM-NEON3、Soc crypto engion4、cryptoisland5、cryptocell1、硬件加解密的分類(lèi)在armv8的芯片
    發(fā)表于 07-22 07:55

    如何對(duì)AES加解密效率進(jìn)行測(cè)試呢

    如何對(duì)AES加解密效率進(jìn)行測(cè)試呢?怎樣去測(cè)試AES的加解密效率呢?
    發(fā)表于 11-11 06:22

    硬件加解密主要優(yōu)點(diǎn)及引擎種類(lèi)

    運(yùn)算引擎,將一串明碼資料經(jīng)過(guò)加密或編碼(Cryptography)后變成一堆不具意義的亂碼,大幅降低數(shù)據(jù)傳輸?shù)娘L(fēng)險(xiǎn),但資料加密或編碼化是復(fù)雜的運(yùn)算過(guò)程,極耗CPU資源,大量加解密資料需求會(huì)造成CPU效能
    發(fā)表于 08-28 07:29

    STM32加解密技術(shù)

    沒(méi)有加解密技術(shù)是萬(wàn)萬(wàn)不能的? 通訊安全? 平臺(tái)安全? *例外:STM32 RDP保護(hù)知識(shí)產(chǎn)權(quán)? 加解密技術(shù)不是萬(wàn)能的? 只是工具? 無(wú)法代替其它STM32安全技術(shù)
    發(fā)表于 09-08 08:18

    鴻蒙 OS 應(yīng)用開(kāi)發(fā)初體驗(yàn)

    的 IDE、鴻蒙生態(tài)的開(kāi)發(fā)語(yǔ)言 ArkTS,通過(guò)模擬器運(yùn)行起來(lái)了鴻蒙 OS 版 HelloWorld。對(duì)于已經(jīng)有移動(dòng)
    發(fā)表于 11-02 19:38

    RSA加解密系統(tǒng)及其單芯片實(shí)現(xiàn)

    RSA加解密系統(tǒng)及其單芯片實(shí)現(xiàn)隨著計(jì)算機(jī)科技的進(jìn)步,帶給人類(lèi)極大的便利性,但伴隨而來(lái)的卻是安全性之問(wèn)題。最簡(jiǎn)單便利的安全措施是利用使用者賬號(hào)及密碼加以控管,但密
    發(fā)表于 11-20 16:25 ?27次下載

    源碼-加解密文本

    易語(yǔ)言是一門(mén)以中文作為程序代碼編程語(yǔ)言學(xué)習(xí)例程:易語(yǔ)言-源碼-加解密文本
    發(fā)表于 06-06 17:43 ?6次下載

    DES與RSA兩種加解密算法的詳細(xì)資料說(shuō)明

    對(duì)稱(chēng)加解密算法: 通信雙方(通信主體)同時(shí)掌握一個(gè)鑰匙,加解密都由這一個(gè)鑰匙完成。
    發(fā)表于 01-14 17:01 ?12次下載
    DES與<b class='flag-5'>RSA</b>兩種<b class='flag-5'>加解密</b>算法的詳細(xì)資料說(shuō)明

    RSA加密與解密

    RSA加密與解密
    發(fā)表于 05-26 15:44 ?4次下載

    STM32的加解密硬件模塊

    電子發(fā)燒友網(wǎng)站提供《STM32的加解密硬件模塊.pdf》資料免費(fèi)下載
    發(fā)表于 08-02 09:14 ?1次下載
    STM32的<b class='flag-5'>加解密</b>硬件模塊

    鴻蒙開(kāi)發(fā)ArkTS基礎(chǔ)知識(shí)

    一、ArkTS簡(jiǎn)介 ArkTS是HarmonyOS優(yōu)選的主力應(yīng)用開(kāi)發(fā)語(yǔ)言。它在TypeScript(簡(jiǎn)稱(chēng)TS)的基礎(chǔ)上,匹配了鴻蒙的ArkUI框架,擴(kuò)展了聲明式UI、狀態(tài)管理
    的頭像 發(fā)表于 01-24 16:44 ?2309次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>開(kāi)發(fā)</b>之<b class='flag-5'>ArkTS</b>基礎(chǔ)知識(shí)

    鴻蒙OS開(kāi)發(fā)問(wèn)題:(ArkTS) 【解決中文亂碼 string2Uint8Array、uint8Array2String】

    在進(jìn)行base64編碼中,遇到中文如果不進(jìn)行處理一定會(huì)出現(xiàn)亂碼
    的頭像 發(fā)表于 03-27 21:38 ?2318次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開(kāi)發(fā)問(wèn)</b>題:(<b class='flag-5'>ArkTS</b>) 【解決<b class='flag-5'>中文</b><b class='flag-5'>亂碼</b> string2Uint8Array、uint8Array2String】
    主站蜘蛛池模板: 亚洲精品视频区| 少妇无套内谢久久久久| 色偷偷亚洲男人天堂| 中文字幕精品在线视频| 国产在线午夜| 视频成人永久免费看| SM高H黄暴NP辣H调教性奴| 绝色娇嫩美人妻老师| 美女裸露胸部100%无遮挡| 亚洲精品嫩草AV在线观看| 囯产精品一品二区三区| 欧美亚洲高清国产| 97精品视频在线观看| 久久亚洲伊人中字综合精品| 亚洲欧美日韩高清中文在线| 国产欧美在线亚洲一区刘亦菲| 日韩亚洲不卡在线视频| gv手机在线观看| 免费视频精品38| 20岁αsrian男同志免费| 久久re视频这里精品免费1| 亚洲国产精品日本无码网站| 国产精品久久久久激情影院| 熟女理发厅| 国产69精品久久久久APP下载| 人人干人人看| 被黑人掹躁10次高潮| 欧洲精品一区二区不卡观看 | 国精产品一区一区三区有| 窝窝午夜色视频国产精品东北| 动漫AV纯肉无码AV电影网| 日韩精品免费在线观看| 成人免费无毒在线观看网站| 亲爱的妈妈6韩国电影免费观看| chinese帅哥gv在线看| 琪琪色在线播放| gogogo高清在线观看| 麒麟色欧美影院在线播放| 成人18视频在线| 涩涩在线视频| 国产高清在线观看|