HarmonyOS 3DES加解密示例

开发中需要用到3DES加解密,有相关使用示例吗?同时因为涉及到密钥,相关密钥应该如何进行保护呢,也需要相关使用示例代码

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

3des加解密 可以参考以下代码demo实例

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

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

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

// 加密消息
function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
  let cipher = cryptoFramework.createCipher('3DES192|ECB|PKCS7');
  cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null);
  let encryptData = cipher.doFinalSync(plainText);
  return encryptData;
}
// 解密消息
function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
  let decoder = cryptoFramework.createCipher('3DES192|ECB|PKCS7');
  decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, null);
  let decryptData = decoder.doFinalSync(cipherText);
  return decryptData;
}
async function genSymKeyByData(symKeyData: Uint8Array) {
  let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
  let symGenerator = cryptoFramework.createSymKeyGenerator('3DES192');
  let symKey = await symGenerator.convertKey(symKeyBlob);
  console.info('convertKey success');
  return symKey;
}
async function DES3Crypto() {
  let keyData = new Uint8Array([238, 249, 61, 55, 128, 220, 183, 224, 139, 253, 248, 239, 239, 41, 71, 25, 235, 206, 230, 162, 249, 27, 234, 114]);
  let symKey = await genSymKeyByData(keyData);
  let message = "This is a test";
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
  let encryptText = encryptMessage(symKey, plainText);
  let decryptText = decryptMessage(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');
  }
}
分享
微博
QQ
微信
回复
6天前
相关问题
HarmonyOS 3des加解密失败
103浏览 • 1回复 待解决
关于3des加解密问题,有人知道吗?
308浏览 • 1回复 待解决
HarmonyOS 关于DES加解密的疑问
113浏览 • 1回复 待解决
HarmonyOS 如何进行DES加解密
651浏览 • 1回复 待解决
HarmonyOS Sm2和DES加解密问题
602浏览 • 1回复 待解决
HarmonyOS 文档中没有查找到DES加解密
485浏览 • 1回复 待解决
求大佬告知如何进行des加解密
2049浏览 • 1回复 待解决
HarmonyOS 如何做md5加密和DES加解密
171浏览 • 1回复 待解决
HarmonyOS CryptoJS Des解密问题
114浏览 • 1回复 待解决
des解密同步方法解密失败
288浏览 • 1回复 待解决
HarmonyOS 加解密咨询
105浏览 • 1回复 待解决
HarmonyOS 加解密 demo
635浏览 • 1回复 待解决