HarmonyOS SM2/SM4结合加解密

接口进行数据交互时,需要使用sm2与sm4结合的加解密组合;其中sm2对sm4密钥进行加密;sm4对实际请求的数据进行加密

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

sm4随机生成密钥并对明文加密的demo如下:

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

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

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

function SM4CryptoMain() {
  // 创建SymKeyGenerator实例
  let symKeyGenerator = cryptoFramework.createSymKeyGenerator('SM4_128');
  // 使用密钥生成器随机生成对称密钥
  let promiseSymKey = symKeyGenerator.generateSymKeySync();
  // 获取对称密钥的二进制数据,输出128位字节流。长度为16字节
  let encodedKey = promiseSymKey.getEncoded();
  console.info('key hex:' + encodedKey.data);

  //sm4密文hex***************************************************************
  let buf = buffer.from(encodedKey.data);
  console.log('hex u8aP result string: ' + buf.toString('hex'));
  //sm4密文hex***************************************************************
  // const sKey: string = "97c6a4a7f77d7c82716deca617ae933d";
  // const symKeyData = buffer.from(sKey, 'hex')//  hex    utf-8
  let symKeyDataUint8Array = new Uint8Array(encodedKey.data/*symKeyData.buffer*/)
  // let symKeyBlob: cryptoFramework.DataBlob = { data: new Uint8Array(symKeyData.buffer) };
  let symKey = genSymKeyByData(symKeyDataUint8Array);
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from('This is a test', 'utf-8').buffer) };
  let encryptText = encryptMessageSM4(symKey, plainText);

  let decryptText = decryptMessageSM4(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');
  }
}

function testSyncGenerateAesKey() {
  // 创建SymKeyGenerator实例
  let symKeyGenerator = cryptoFramework.createSymKeyGenerator('SM4_128');
  // 使用密钥生成器随机生成对称密钥
  let promiseSymKey = symKeyGenerator.generateSymKeySync();
  // 获取对称密钥的二进制数据,输出128位字节流。长度为16字节
  let encodedKey = promiseSymKey.getEncoded();
  console.info('key hex:' + encodedKey.data);
}
// 加密消息
function encryptMessageSM4(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
  let cipher = cryptoFramework.createCipher('SM4_128|ECB|PKCS7');
  cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null);
  let encryptData = cipher.doFinalSync(plainText);
  console.log(`加密成功`)
  return encryptData;
}

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

function genSymKeyByData(symKeyData: Uint8Array) {
  let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
  let symGenerator = cryptoFramework.createSymKeyGenerator('SM4_128');
  let symKey = symGenerator.convertKeySync(symKeyBlob);
  console.log(`sm4 转密钥成功`)
  return symKey;
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS SM2SM4国密加解密使用demo
310浏览 • 1回复 待解决
SM4采用OFB模式进行加解密
977浏览 • 1回复 待解决
如何使用SM4的CBC模式加解密
998浏览 • 1回复 待解决
HarmonyOS Sm2和DES加解密问题
512浏览 • 1回复 待解决
SM4 CBC模式加解密,有好的方案吗?
1059浏览 • 1回复 待解决
如何使用国密SM2算法进行加解密
4473浏览 • 1回复 待解决
HarmonyOS 没找到 sm4 cbc 加密
30浏览 • 1回复 待解决
HarmonyOS SM2数据签名
20浏览 • 1回复 待解决
HarmonyOS SM2密钥问题
27浏览 • 1回复 待解决
HarmonyOS SM2加密算法
23浏览 • 1回复 待解决
HarmonyOS sm2验签失败
76浏览 • 1回复 待解决
HarmonyOS 生成SM2密钥对报错
87浏览 • 1回复 待解决