1# 加解密(C/C++) 2 3以AES 256密钥为例,完成加解密。具体的场景介绍及支持的算法规格,请参考[密钥生成支持的算法](huks-key-generation-overview.md#支持的算法)。 4 5## 在CMake脚本中链接相关动态库 6```txt 7target_link_libraries(entry PUBLIC libhuks_ndk.z.so) 8``` 9## 开发步骤 10 11**生成密钥** 12 131. 指定密钥别名。 14 152. 初始化密钥属性集。 16 173. 调用[OH_Huks_GenerateKeyItem](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_generatekeyitem)生成密钥,具体请参考[密钥生成](huks-key-generation-overview.md)。 18 19除此之外,开发者也可以参考[密钥导入](huks-key-import-overview.md),导入已有的密钥。 20 21**加密** 22 231. 获取密钥别名。 24 252. 获取待加密的数据。 26 273. 调用[OH_Huks_InitParamSet](../../reference/apis-universal-keystore-kit/_huks_param_set_api.md#oh_huks_initparamset)指定算法参数配置。在下方示例中,使用算法AES进行加密时,必须要选择其对应分组模式以及填充模式,用例中选取的分组模式为CBC、填充模式为PKCS7,此时必须要填参数IV。 28 294. 调用[OH_Huks_InitSession](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_initsession)初始化密钥会话,并获取会话的句柄handle。 30 315. 调用[OH_Huks_FinishSession](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_finishsession)结束密钥会话,获取加密后的密文。 32 33**解密** 34 351. 获取密钥别名。 36 372. 获取待解密的密文。 38 393. 调用[OH_Huks_InitParamSet](../../reference/apis-universal-keystore-kit/_huks_param_set_api.md#oh_huks_initparamset)指定算法参数配置。在下方示例中,使用算法AES进行解密时,必须要选择其对应分组模式以及填充模式,用例中选取的分组模式为CBC、填充模式为PKCS7,此时必须要填参数IV。 40 414. 调用[OH_Huks_InitSession](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_initsession)初始化密钥会话,并获取会话的句柄handle。 42 435. 调用[OH_Huks_FinishSession](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_finishsession)结束密钥会话,获取解密后的数据。 44 45**删除密钥** 46 47当密钥废弃不用时,需要调用OH_Huks_DeleteKeyItem删除密钥,具体请参考[密钥删除](huks-delete-key-ndk.md)。 48 49```c++ 50#include "huks/native_huks_api.h" 51#include "huks/native_huks_param.h" 52#include "napi/native_api.h" 53#include <string.h> 54OH_Huks_Result InitParamSet( 55 struct OH_Huks_ParamSet **paramSet, 56 const struct OH_Huks_Param *params, 57 uint32_t paramCount) 58{ 59 OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet); 60 if (ret.errorCode != OH_HUKS_SUCCESS) { 61 return ret; 62 } 63 ret = OH_Huks_AddParams(*paramSet, params, paramCount); 64 if (ret.errorCode != OH_HUKS_SUCCESS) { 65 OH_Huks_FreeParamSet(paramSet); 66 return ret; 67 } 68 ret = OH_Huks_BuildParamSet(paramSet); 69 if (ret.errorCode != OH_HUKS_SUCCESS) { 70 OH_Huks_FreeParamSet(paramSet); 71 return ret; 72 } 73 return ret; 74} 75static const uint32_t IV_SIZE = 16; 76static uint8_t IV[IV_SIZE] = { 0 }; // this is a test value, for real use the iv should be different every time. 77static struct OH_Huks_Param g_genEncDecParams[] = { 78 { 79 .tag = OH_HUKS_TAG_ALGORITHM, 80 .uint32Param = OH_HUKS_ALG_AES 81 }, { 82 .tag = OH_HUKS_TAG_PURPOSE, 83 .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT | OH_HUKS_KEY_PURPOSE_DECRYPT 84 }, { 85 .tag = OH_HUKS_TAG_KEY_SIZE, 86 .uint32Param = OH_HUKS_AES_KEY_SIZE_256 87 }, { 88 .tag = OH_HUKS_TAG_PADDING, 89 .uint32Param = OH_HUKS_PADDING_NONE 90 }, { 91 .tag = OH_HUKS_TAG_BLOCK_MODE, 92 .uint32Param = OH_HUKS_MODE_CBC 93 } 94}; 95static struct OH_Huks_Param g_encryptParams[] = { 96 { 97 .tag = OH_HUKS_TAG_ALGORITHM, 98 .uint32Param = OH_HUKS_ALG_AES 99 }, { 100 .tag = OH_HUKS_TAG_PURPOSE, 101 .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT 102 }, { 103 .tag = OH_HUKS_TAG_KEY_SIZE, 104 .uint32Param = OH_HUKS_AES_KEY_SIZE_256 105 }, { 106 .tag = OH_HUKS_TAG_PADDING, 107 .uint32Param = OH_HUKS_PADDING_NONE 108 }, { 109 .tag = OH_HUKS_TAG_BLOCK_MODE, 110 .uint32Param = OH_HUKS_MODE_CBC 111 }, { 112 .tag = OH_HUKS_TAG_IV, 113 .blob = { 114 .size = IV_SIZE, 115 .data = (uint8_t *)IV // this is a test value, for real use the iv should be different every time. 116 } 117 } 118}; 119static struct OH_Huks_Param g_decryptParams[] = { 120 { 121 .tag = OH_HUKS_TAG_ALGORITHM, 122 .uint32Param = OH_HUKS_ALG_AES 123 }, { 124 .tag = OH_HUKS_TAG_PURPOSE, 125 .uint32Param = OH_HUKS_KEY_PURPOSE_DECRYPT 126 }, { 127 .tag = OH_HUKS_TAG_KEY_SIZE, 128 .uint32Param = OH_HUKS_AES_KEY_SIZE_256 129 }, { 130 .tag = OH_HUKS_TAG_PADDING, 131 .uint32Param = OH_HUKS_PADDING_NONE 132 }, { 133 .tag = OH_HUKS_TAG_BLOCK_MODE, 134 .uint32Param = OH_HUKS_MODE_CBC 135 }, { 136 .tag = OH_HUKS_TAG_IV, 137 .blob = { 138 .size = IV_SIZE, 139 .data = (uint8_t *)IV // this is a test value, for real use the iv should be different every time. 140 } 141 } 142}; 143static const uint32_t AES_COMMON_SIZE = 1024; 144OH_Huks_Result HksAesCipherTestEncrypt( 145 const struct OH_Huks_Blob *keyAlias, 146 const struct OH_Huks_ParamSet *encryptParamSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *cipherText) 147{ 148 uint8_t handleE[sizeof(uint64_t)] = {0}; 149 struct OH_Huks_Blob handleEncrypt = {sizeof(uint64_t), handleE}; 150 OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr); 151 if (ret.errorCode != OH_HUKS_SUCCESS) { 152 return ret; 153 } 154 ret = OH_Huks_FinishSession(&handleEncrypt, encryptParamSet, inData, cipherText); 155 return ret; 156} 157OH_Huks_Result HksAesCipherTestDecrypt( 158 const struct OH_Huks_Blob *keyAlias, 159 const struct OH_Huks_ParamSet *decryptParamSet, const struct OH_Huks_Blob *cipherText, struct OH_Huks_Blob *plainText, 160 const struct OH_Huks_Blob *inData) 161{ 162 uint8_t handleD[sizeof(uint64_t)] = {0}; 163 struct OH_Huks_Blob handleDecrypt = {sizeof(uint64_t), handleD}; 164 OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr); 165 if (ret.errorCode != OH_HUKS_SUCCESS) { 166 return ret; 167 } 168 ret = OH_Huks_FinishSession(&handleDecrypt, decryptParamSet, cipherText, plainText); 169 return ret; 170} 171static napi_value EncDecKey(napi_env env, napi_callback_info info) 172{ 173 char tmpKeyAlias[] = "test_enc_dec"; 174 struct OH_Huks_Blob keyAlias = { (uint32_t)strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias }; 175 struct OH_Huks_ParamSet *genParamSet = nullptr; 176 struct OH_Huks_ParamSet *encryptParamSet = nullptr; 177 struct OH_Huks_ParamSet *decryptParamSet = nullptr; 178 OH_Huks_Result ohResult; 179 do { 180 /* 1. Generate Key */ 181 /* 182 * 模拟生成密钥场景 183 * 1.1. 确定密钥别名 184 */ 185 /* 186 * 1.2. 获取生成密钥算法参数配置 187 */ 188 ohResult = InitParamSet(&genParamSet, g_genEncDecParams, sizeof(g_genEncDecParams) / sizeof(OH_Huks_Param)); 189 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 190 break; 191 } 192 /* 193 * 1.3. 调用generateKeyItem 194 */ 195 ohResult = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr); 196 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 197 break; 198 } 199 /* 2. Encrypt */ 200 /* 201 * 模拟加密场景 202 * 2.1. 获取密钥别名 203 */ 204 /* 205 * 2.2. 获取待加密的数据 206 */ 207 /* 208 * 2.3. 获取加密算法参数配置 209 */ 210 ohResult = InitParamSet(&encryptParamSet, g_encryptParams, sizeof(g_encryptParams) / sizeof(OH_Huks_Param)); 211 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 212 break; 213 } 214 char tmpInData[] = "AES_ECB_INDATA_1"; 215 struct OH_Huks_Blob inData = { (uint32_t)strlen(tmpInData), (uint8_t *)tmpInData }; 216 uint8_t cipher[AES_COMMON_SIZE] = {0}; 217 struct OH_Huks_Blob cipherText = {AES_COMMON_SIZE, cipher}; 218 /* 219 * 2.4. 调用initSession获取handle 220 */ 221 /* 222 * 2.5. 调用finishSession获取加密后的密文 223 */ 224 ohResult = HksAesCipherTestEncrypt(&keyAlias, encryptParamSet, &inData, &cipherText); 225 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 226 break; 227 } 228 /* 3. Decrypt */ 229 /* 230 * 模拟解密场景 231 * 3.1. 获取密钥别名 232 */ 233 /* 234 * 3.2. 获取待解密的密文 235 */ 236 /* 237 * 3.3. 获取解密算法参数配置 238 */ 239 ohResult = InitParamSet(&decryptParamSet, g_decryptParams, sizeof(g_decryptParams) / sizeof(OH_Huks_Param)); 240 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 241 break; 242 } 243 uint8_t plain[AES_COMMON_SIZE] = {0}; 244 struct OH_Huks_Blob plainText = {AES_COMMON_SIZE, plain}; 245 /* 246 * 3.4. 调用initSession获取handle 247 */ 248 /* 249 * 3.5. 调用finishSession获取解密后的数据 250 */ 251 ohResult = HksAesCipherTestDecrypt(&keyAlias, decryptParamSet, &cipherText, &plainText, &inData); 252 } while (0); 253 /* 4. Delete Key */ 254 /* 255 * 模拟删除密钥场景 256 * 4.1. 获取密钥别名 257 */ 258 /* 259 * 4.2. 调用deleteKeyItem删除密钥 260 */ 261 (void)OH_Huks_DeleteKeyItem(&keyAlias, genParamSet); 262 263 OH_Huks_FreeParamSet(&genParamSet); 264 OH_Huks_FreeParamSet(&encryptParamSet); 265 OH_Huks_FreeParamSet(&decryptParamSet); 266 267 napi_value ret; 268 napi_create_int32(env, ohResult.errorCode, &ret); 269 return ret; 270} 271``` 272