HarmonyOS SM4如何进行SM4/ECB/NoPadding加密

// 加密消息
async encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
  let cipher = cryptoFramework.createCipher('SM4_128/ECB/NoPadding');
  cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null);
  let encryptData = cipher.doFinalSync(plainText);
  return encryptData;
}

请问如何进行SM4/ECB/NoPadding进行加密?/初始化cryptoFramework

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

sm4 ECB NoPadding 加解密的代码demo实例如下:

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 = "D6591A9DB9F8792A1A96CBC7A2BB3A49";
  const symKeyData = buffer.from(sKey, 'hex')//  hex    utf-8
  let symKeyDataUint8Array = new Uint8Array(symKeyData.buffer)

  let symKey = await genSymKeyByData(symKeyDataUint8Array);
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from('this is a test', 'hex').buffer) };
  let encryptText = await encryptMessagePromise(symKey, plainText);

  let decryptText = await decryptMessagePromise(symKey, encryptText);
  if (plainText.data.toString() === decryptText.data.toString()) {
    console.info('decrypt ok');
    console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8'));
  } else {
    console.error('decrypt failed');
  }
}

// 加密消息
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
微信
回复
1天前
相关问题
HarmonyOS SM4-ECB-NoPadding加密
69浏览 • 1回复 待解决
HarmonyOS 没找到 sm4 cbc 加密
30浏览 • 1回复 待解决
SM4采用OFB模式进行加解密
977浏览 • 1回复 待解决
HarmonyOS SM2/SM4结合加解密
29浏览 • 1回复 待解决
如何使用SM4的CBC模式加解密
998浏览 • 1回复 待解决
HarmonyOS SM2,SM4国密加解密使用demo
310浏览 • 1回复 待解决
想了解一下SM4相关案例
746浏览 • 1回复 待解决
SM4 CBC模式加解密,有好的方案吗?
1059浏览 • 1回复 待解决
HarmonyOS 如何进行SM2key的初始化
30浏览 • 1回复 待解决
HarmonyOS 如何使用SM3加密
426浏览 • 1回复 待解决
HarmonyOS SM2加密算法
23浏览 • 1回复 待解决
HarmonyOS SM3加密怎么写的呢?
67浏览 • 1回复 待解决
能否提供一个SM3加密案例
623浏览 • 1回复 待解决
如何使用国密SM2算法进行加解密
4473浏览 • 1回复 待解决
RSA NoPadding模式加密失败
1274浏览 • 1回复 待解决
如何使用AES ECB 加密方式?
222浏览 • 1回复 待解决