HarmonyOS 使用3DES对称密钥如何进行 CBC模式加解密

其他系统 app 接口数据加解密算法,迁移到HarmonyOS 后结果不正确

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

3DES 的CBC模式下的加解密代码demo实例如下

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

@Entry
@Component
struct TDes3Crypto {
  @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|CBC|PKCS7');
  let iv = genIvParamsSpec();
  cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, iv);
  let encryptData = cipher.doFinalSync(plainText);
  return encryptData;
}
// 解密消息
function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
  let decoder = cryptoFramework.createCipher('3DES192|CBC|PKCS7');
  let iv = genIvParamsSpec();
  decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, iv);
  let decryptData = decoder.doFinalSync(cipherText);
  return decryptData;
}
function genSymKeyByData(symKeyData: Uint8Array) {
  let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
  let symGenerator = cryptoFramework.createSymKeyGenerator('3DES192');
  let symKey = symGenerator.convertKeySync(symKeyBlob);
  console.info('convertKey success');
  return symKey;
}

function genIvParamsSpec() {
  let iv = new Uint8Array(buffer.from('1234567890123456', 'utf-8').buffer);
  // let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes
  // let arr = [-45, -21, 108, 107, 31, 7, 62, 12, 51, -39, 87, 89, -18, 48, 3, 23]; // 16 bytes
  // let arr =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]; ; // 16 bytes
  let dataIv = new Uint8Array(iv);
  let ivBlob: cryptoFramework.DataBlob = { data: dataIv };
  let ivParamsSpec: cryptoFramework.IvParamsSpec = {
    algName: "IvParamsSpec",
    iv: ivBlob
  };
  return ivParamsSpec;
}
function DES3Crypto() {
  let base64 = new util.Base64Helper()

  let key = "7vk9N4Dct+CL/fjv7ylHGevO5qL5G+py";
  let deBaseKey = base64.decodeSync(key)
  // 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 = genSymKeyByData(deBaseKey);

  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
微信
回复
1天前
相关问题
HarmonyOS 3des加解密失败
21浏览 • 1回复 待解决
HarmonyOS 如何进行DES加解密
598浏览 • 1回复 待解决
关于3des加解密问题,有人知道吗?
229浏览 • 1回复 待解决
求大佬告知如何进行des加解密
1958浏览 • 1回复 待解决
如何使用SM4的CBC模式加解密
998浏览 • 1回复 待解决
如何进行不同规格的AES加解密
858浏览 • 1回复 待解决
SM4 CBC模式加解密,有好的方案吗?
1059浏览 • 1回复 待解决
HarmonyOS 关于DES加解密的疑问
34浏览 • 1回复 待解决
实现一次非对称RSA非对称加解密
1102浏览 • 1回复 待解决
SM4采用OFB模式进行加解密
977浏览 • 1回复 待解决
RSA导入外部密钥实现加解密
816浏览 • 1回复 待解决