中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
SM4相关案例。
微信扫码分享
async function SM4ED(plainText: string) { let sm4Generator = cryptoFramework.createSymKeyGenerator('SM4_128'); let cipher = cryptoFramework.createCipher('SM4_128|CBC|PKCS7'); let decoder = cryptoFramework.createCipher('SM4_128|CBC|PKCS7'); let input: cryptoFramework.DataBlob = { data: stringToUint8Array(plainText) }; let keydata: cryptoFramework.DataBlob = { data: stringToUint8Array('yc1436w86vhys51r') }; //密钥 let ivdata: cryptoFramework.DataBlob = { data: stringToUint8Array('43pr5253zf5mm7jd') }; //偏移 let iv: cryptoFramework.IvParamsSpec = { iv: ivdata, algName: 'IvParamsSpec'} //cbc模式的参数 let key: cryptoFramework.SymKey try { key = await sm4Generator.convertKey(keydata); // 加密 // 最后一个参数是偏移量的设置,如果没有则为null,下面解密同理 await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, key, iv); let cipherData = await cipher.doFinal(input) //解密 await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, key, iv); let result = await decoder.doFinal(cipherData); //解密后的明文 let str = uint8ArrayToString(result.data) AlertDialog.show({ message: 'decrypt success ==>' + str }) } catch (err) { console.error(err) } }