HarmonyOS 鸿蒙Next SM2/SM4结合加解密
HarmonyOS 鸿蒙Next SM2/SM4结合加解密
SM2、SM4结合加解密 接口进行数据交互时,需要使用sm2与sm4结合的加解密组合;其中sm2对sm4密钥进行加密;sm4对实际请求的数据进行加密
2 回复
sm4随机生成密钥 并对明文加密
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer } from '@kit.ArkTS';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct SM2and4Crypto {
[@State](/user/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;
}
更多关于HarmonyOS 鸿蒙Next SM2/SM4结合加解密的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html