加解密(SM2,SM4),提供些案例demo作为开发自学参考

提供些案例demo作为开发自学参考。

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

SM2的相关demo如下:

import cryptoFramework from '@ohos.security.cryptoFramework'; 
import buffer from '@ohos.buffer'; 
 
// 加密消息 
async function encryptMessagePromise(publicKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) { 
  //密钥类型为SM2_256、摘要算法为SM3的Cipher 
  let cipher = cryptoFramework.createCipher('SM2_256|SM3'); 
  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, publicKey, null); 
  let encryptData = await cipher.doFinal(plainText); 
  return encryptData; 
} 
// 解密消息 
async function decryptMessagePromise(privateKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) { 
  let decoder = cryptoFramework.createCipher('SM2_256|SM3'); 
  await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, privateKey, null); 
  let decryptData = await decoder.doFinal(cipherText); 
  return decryptData; 
} 
// 生成SM2密钥对 
async function genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) { 
  let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData }; 
  let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData }; 
  let sm2Generator = cryptoFramework.createAsyKeyGenerator('SM2_256'); 
  let keyPair = await sm2Generator.convertKey(pubKeyBlob, priKeyBlob); 
  console.info('convertKey success'); 
  return keyPair; 
} 
// SM2  main代码 
async function main() { 
  //服务器传入的公钥密钥 
  let pkData = “ MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMTW+419MhK4O1IjKxpG4JCouhIpgplQjYHPQ5VzA......Dxiz23k21Zs= ”; 
  //服务器传入的私钥密钥 
  let skData = “MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMTW+419MhK4O1IjKxpG4JCouhIpgplQjYHPQ5VzA......Dxiz23k21Zs=”; 
  // 先对密钥进行base64解码,获取密钥Uint8Array对象 
  let base64 = new util.Base64Helper(); 
  let pkDataDatabase64= base64.decodeSync(pkData ); 
  let base64 = new util.Base64Helper(); 
  let skDataDatabase64= base64.decodeSync(skData ); 
  let keyPair = await genKeyPairByData(pkDataDatabase64, skDataDatabase64); 
  let pubKey = keyPair.pubKey; 
  let priKey = keyPair.priKey; 
  // 此处为明文 
  let message = 'This is a test'; 
  // 把字符串按utf-8解码为Uint8Array 
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 
  let encryptText = await encryptMessagePromise(pubKey, plainText); 
  let decryptText = await decryptMessagePromise(priKey, encryptText); 
  if (plainText.data.toString() === decryptText.data.toString()) { 
    console.info('decrypt ok'); 
    // 把Uint8Array按utf-8编码为字符串 
    let messageDecrypted = buffer.from(decryptText.data).toString('utf-8'); 
    console.info('decrypted result string:' + messageDecrypted); 
  } else { 
    console.error('decrypt failed'); 
  }

SM4的相关demo如下:

import cryptoFramework from '@ohos.security.cryptoFramework'; 
import buffer from '@ohos.buffer'; 
 
// 加密消息 
async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 
  let cipher = cryptoFramework.createCipher('SM4_128|ECB|PKCS7'); 
  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null); 
  let encryptData = await cipher.doFinal(plainText); 
  return encryptData; 
} 
// 解密消息 
async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { 
  let decoder = cryptoFramework.createCipher('SM4_128|ECB|PKCS7'); 
  await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, null); 
  let decryptData = await decoder.doFinal(cipherText); 
  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.info('convertKey success'); 
  return symKey; 
} 
async function main() {//服务器传入的密钥 
  let keyData = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMTW+419MhK4O1IjKxpG4JCouhIpgplQjYHPQ5VzA......Dxiz23k21Zs="; 
  // 先对密钥进行base64解码,获取密钥Uint8Array对象 
  let base64 = new util.Base64Helper(); 
  let keyDatabase64= base64.decodeSync(keyData ); 
  // 密钥Uint8Array 生成密钥 
  let symKey = await genSymKeyByData(keyDatabase64); 
  // 此处为明文 
  let message = "This is a test"; 
  // 把明文字符串按utf-8解码为Uint8Array 
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').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'); 
  } 
}
分享
微博
QQ
微信
回复
5h前
相关问题
SM4采用OFB模式进行加解密
741浏览 • 1回复 待解决
如何使用SM4的CBC模式加解密
722浏览 • 1回复 待解决
HarmonyOS Sm2和DES加解密问题
264浏览 • 1回复 待解决
SM4 CBC模式加解密,有好的方案吗?
902浏览 • 1回复 待解决
如何使用国密SM2算法进行加解密
3045浏览 • 1回复 待解决
想了解一下SM4相关案例
549浏览 • 1回复 待解决
HarmonyOS SM2密钥对转换失败
387浏览 • 1回复 待解决
huks sm2签名验签失败
2浏览 • 0回复 待解决
HarmonyOS SM2加签验签SM3摘要算法使用
221浏览 • 1回复 待解决
能否提供一个SM3加密案例
477浏览 • 1回复 待解决
加解密HmacSha1 、HmacSha256、aes参考Demo
1921浏览 • 1回复 待解决
HarmonyOS 加解密 demo
338浏览 • 1回复 待解决