HarmonyOS 模拟器是否支持crypto-js?

模拟器是否支持crypto-js?目前发现模拟器在加密这块会有闪退,sm4Encode of undefined 。

static sm4Encode(d121, e121, f121) {
  if (f121 === undefined) {
    f121 = YTCryptoUtil.SM4_PADDING_TYPE_SPACE;
  }
  if (f121 === YTCryptoUtil.SM4_PADDING_TYPE_PKCS5) {
    let i121 = 16 - d121.length % 16;
    if (i121 !== 0) {
      d121 = d121 + String.fromCharCode(i121).repeat(i121);
    }
  } else if (f121 === YTCryptoUtil.SM4_PADDING_TYPE_SPACE) {
    let g121 = d121.length % 16;
    let h121 = g121 === 0 ? 0 : 16 - d121.length % 16;
    if (h121 !== 0) {
      d121 = d121 + " ".repeat(h121);
    }
  }
  return YTCrypto.sm4Encode(d121, e121);
}
HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

crypto-js是可以在模拟器上运行的,但是如果传参不符的话,会直接闪退。以下sm4| ECB | nopadding为例:

import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer } from '@kit.ArkTS';

@Entry
@Component
struct SM4Crypto {
  @State message: string = '点击开始';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            SM4CryptoMain()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

async function SM4CryptoMain() {
  const sKey: string = "397A3043326377365369586D4C625958";
  const symKeyData = buffer.from(sKey, 'hex')//  hex    utf-8
  let symKeyDataUint8Array = new Uint8Array(symKeyData.buffer)
  let symKey = await genSymKeyByData(symKeyDataUint8Array);
  let text = "BFABDF84112C3A2737CAAD36E1C2E041";
  let newBuf = buffer.from(buffer.from(text, 'utf-8').buffer);
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(newBuf.buffer) };
  let encryptText = await encryptMessagePromise(symKey, plainText);
  console.info('encryptText plainText: ' + encryptText);
  console.error('encryptText plainText: ' + buffer.from(encryptText.data).toString());
  let newBuf1 = buffer.from(buffer.from("BFABDF84112C3A2737CAAD36E1C2E041", 'hex').buffer);
  let encryptoText: cryptoFramework.DataBlob = { data: new Uint8Array(newBuf1.buffer) };
  let decryptText = await decryptMessagePromise(symKey, encryptoText);
  console.error('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8'));
  console.error('decrypt plainText: ' + buffer.from(decryptText.data).toString('hex'));
}

// 加密消息
async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
  let cipher = cryptoFramework.createCipher('SM4_128|ECB|NoPadding');
  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null);
  let encryptData = await cipher.doFinal(plainText);
  console.log(`加密成功`)
  return encryptData;
}

// 解密消息
async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
  let decoder = cryptoFramework.createCipher('SM4_128|ECB|NoPadding');
  await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, null);
  let decryptData = await decoder.doFinal(cipherText);
  console.log(`解密成功 ${buffer.from(decryptData.data).toString()}`)
  return decryptData;
}

async function genSymKeyByData(symKeyData: Uint8Array) {
  let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
  let symGenerator = cryptoFramework.createSymKeyGenerator('SM4_128');
  let symKey = await symGenerator.convertKey(symKeyBlob);
  console.log(`sm4 转密钥成功`)
  return symKey;
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS crypto-js 性能存在问题
506浏览 • 1回复 待解决
HarmonyOS crypto-js 解密中乱码问题
841浏览 • 1回复 待解决
鸿蒙ets项目如何引用crypto-js?
4396浏览 • 1回复 待解决
HarmonyOS crypto-js加密没有返回值
225浏览 • 1回复 待解决
加密算法(crypto-js)Arkts转化代码
1301浏览 • 1回复 待解决
HarmonyOS 模拟器黑屏
331浏览 • 1回复 待解决
本地模拟器支持断点调试????
2854浏览 • 1回复 待解决
HDC是否可以针对模拟器上使用?
904浏览 • 1回复 待解决
HarmonyOS如何下载模拟器
1064浏览 • 1回复 待解决
HarmonyOS 模拟器相册问题
349浏览 • 1回复 待解决
HarmonyOS 模拟器抓包问题
271浏览 • 1回复 待解决
HarmonyOS 模拟器怎么打开定位?
507浏览 • 1回复 待解决
HarmonyOS 模拟器无法安装应用
285浏览 • 1回复 待解决
模拟器能否跳过联网?
310浏览 • 1回复 待解决
DevEco模拟器无法启动
4786浏览 • 0回复 待解决
HarmonyOS 模拟器热更新报错cppcrash
301浏览 • 1回复 待解决