diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/AppScope/app.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d62c80428d5dd830d69ca50f5a965ba31d01f674 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.encryptiondecryptionguidance3des", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/AppScope/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..c9880ec1f7c47f15803905dfb649999fa94a426f --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "EncryptionDecryptionGuidance3DES" + } + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/AppScope/resources/base/media/app_icon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/AppScope/resources/base/media/app_icon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/README.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/README.md new file mode 100644 index 0000000000000000000000000000000000000000..4a626b76f03d8aed7ba07c453bcb05d8cf867a79 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/README.md @@ -0,0 +1,83 @@ +# 使用3DES对称密钥(ECB模式)加解密 + +### 介绍 + +本示例主要展示了使用3DES对称密钥(ECB模式)加解密(ArkTS)的同步异步方法和C/C++场景 。该工程中展示的代码详细描述可查如下链接。 + +- [使用3DES对称密钥(ECB模式)加解密(ArkTS)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-3des-sym-encrypt-decrypt-ecb.md) +- [使用3DES对称密钥(ECB模式)加解密(C/C++)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-3des-sym-encrypt-decrypt-ecb-ndk.md) + +### 效果预览 + +| 首页效果图 | 执行结果图 | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| | | + +### 使用说明 + +1. 运行Index主界面。 +2. 页面呈现上述执行结果图效果,点击不同按钮可以跳转到不同功能页面,点击跳转页面中按钮可以执行对应操作,并更新文本内容。 +3. 运行测试用例EncryptionDecryptionGuidance3DES.test.ets文件对页面代码进行测试可以全部通过。 + +### 工程目录 + +``` +entry/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ | ├── libentry + │ │ | ├── index.d.ts + │ │ | ├── oh-package.json5 + │ │ | ├── project + │ │ | ├── 3des_ecb_encryption_decryption.cpp + │ │ | ├── file.h + │ │ ├── CMakeList.txt + │ │ ├── napi_init.cpp + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // 使用3DES对称密钥(ECB模式)加解密示例代码 + │ │ ├── 3des_ecb_encryption_decryption.ets + │ │ ├── 3des_ecb_encryption_decryption_asynchronous.ets + │ │ ├── 3des_ecb_encryption_decryption_synchronous.ets + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ └── test + │ │ ├── Ability.test.ets + │ │ ├── EncryptionDecryptionGuidance3DES.test.ets // 自动化测试代码 + │ │ └── List.test.ets +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS 5.0.2 Release及以上。 + +3. DevEco Studio版本:DevEco Studio 5.0.2 Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS 5.0.2 Release及以上。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +```` +git init +git config core.sparsecheckout true +echo CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES > .git/info/sparse-checkout +git remote add origin https://gitee.com/harmonyos_samples/guide-snippets.git +git pull origin master +```` \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fb5a1ab654ae6715a0ab69a31900ad26dcf2487f --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/build-profile.json5 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.2(14)", + "targetSdkVersion": "5.0.2(14)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/code-linter.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2d8b081b615348a99aa197f90186f3d8891b7dc1 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/build-profile.json5 @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + "abiFilters": [ + "arm64-v8a", + "x86_64" + ] + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..118bdd4fe7699368a010e04c24f5bfc887cf1298 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libentry.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/CMakeLists.txt b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..05757dad0a38771d9bbab2b2d546ed22222c0bb0 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/CMakeLists.txt @@ -0,0 +1,15 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(EncryptionDecryptionGuidance3DESArkTs) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(entry SHARED napi_init.cpp types/project/3des_ecb_encryption_decryption.cpp) +target_link_libraries(entry PUBLIC libace_napi.z.so libohcrypto.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/napi_init.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..075d3ed0a5322f5fa644a22c88dbc56e51d1c2c4 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/napi_init.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "types/project/file.h" + +static napi_value NAPI_Global_doTest3DesEcb(napi_env env, napi_callback_info info) +{ + napi_value ret; + napi_create_int32(env, doTest3DesEcb(), &ret); + return ret; +} + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + {"doTest3DesEcb", nullptr, NAPI_Global_doTest3DesEcb, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "entry", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/types/libentry/index.d.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/types/libentry/index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..feacf528f99f276a68b23dae222d1e02e4dd0920 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/types/libentry/index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const doTest3DesEcb: () => number; \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/types/libentry/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2826cc7d6bd199c1008bb51d898dffa922201e6c --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libentry.so", + "types": "./index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/types/project/3des_ecb_encryption_decryption.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/types/project/3des_ecb_encryption_decryption.cpp new file mode 100644 index 0000000000000000000000000000000000000000..197ef6b1fc67a44b833f516189b1e66a4fcad1d4 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/types/project/3des_ecb_encryption_decryption.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start crypt_decrypt_flow] +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_sym_cipher.h" +#include +#include "file.h" + +OH_Crypto_ErrCode doTest3DesEcb() +{ + OH_CryptoSymKeyGenerator *genCtx = nullptr; + OH_CryptoSymCipher *encCtx = nullptr; + OH_CryptoSymCipher *decCtx = nullptr; + OH_CryptoSymKey *keyCtx = nullptr; + OH_CryptoSymCipherParams *params = nullptr; + char *plainText = const_cast("this is test!"); + Crypto_DataBlob input = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; + Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; + Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("3DES192", &genCtx); // 随机生成对称密钥 + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_Create(¶ms); // 创建参数 + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Create("3DES192|ECB|PKCS7", &encCtx); // 加密操作 + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(encCtx, &input, &outUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Create("3DES192|ECB|PKCS7", &decCtx); // 解密操作 + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } +end: + OH_CryptoSymCipherParams_Destroy(params); + OH_CryptoSymCipher_Destroy(encCtx); + OH_CryptoSymCipher_Destroy(decCtx); + OH_CryptoSymKeyGenerator_Destroy(genCtx); + OH_CryptoSymKey_Destroy(keyCtx); + OH_Crypto_FreeDataBlob(&outUpdate); + OH_Crypto_FreeDataBlob(&decUpdate); + return ret; +} +// [End crypt_decrypt_flow] \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/types/project/file.h b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/types/project/file.h new file mode 100644 index 0000000000000000000000000000000000000000..7e388d978e363aa72fa5ad00ae4ac452b562cd51 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/cpp/types/project/file.h @@ -0,0 +1,23 @@ +#ifndef FILE_H +#define FILE_H +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_sym_cipher.h" + +OH_Crypto_ErrCode doTest3DesEcb(); + +#endif // FILE_H \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/entryability/EntryAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..edc2839f203ba057c186e19b0cbbbf80c8faa8b3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/pages/3des_ecb_encryption_decryption.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/pages/3des_ecb_encryption_decryption.ets new file mode 100644 index 0000000000000000000000000000000000000000..9b26168d82175c649c701a588792bd4283041962 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/pages/3des_ecb_encryption_decryption.ets @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import testNapi from 'libentry.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance 3DES'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_3des_ecb')) + .width('70%') + .onClick(() => { + let ret: number = testNapi.doTest3DesEcb(); + this.message = 'Call Success Result:' + ret.toString(); + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/pages/3des_ecb_encryption_decryption_asynchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/pages/3des_ecb_encryption_decryption_asynchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..574aaf49581dbe0af16f7036552bb74d2ba3e9a4 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/pages/3des_ecb_encryption_decryption_asynchronous.ets @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start async_symmetry_encrypt_decrypt] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 加密消息 +async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('3DES192|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('3DES192|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('3DES192'); + let symKey = await symGenerator.convertKey(symKeyBlob); + console.info('convertKey success'); + return symKey; +} + +async function main() { + 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 = await genSymKeyByData(keyData); + let message = 'This is a test'; + 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'); + } +} +// [End async_symmetry_encrypt_decrypt] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance 3DES ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_3des_ecb_asynchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = '3DES_ECBSuccess'; + } catch { + this.message = '3DES_ECBFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/pages/3des_ecb_encryption_decryption_synchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/pages/3des_ecb_encryption_decryption_synchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..9e6078b4b84e823172ed4e748a27895038adda81 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/pages/3des_ecb_encryption_decryption_synchronous.ets @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start sync_symmetry_encrypt_decrypt] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 加密消息 +function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('3DES192|ECB|PKCS7'); + cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null); + let encryptData = cipher.doFinalSync(plainText); + return encryptData; +} + +// 解密消息 +function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('3DES192|ECB|PKCS7'); + decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, null); + 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('convertKeySync success'); + return symKey; +} + +function main() { + 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(keyData); + 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'); + } +} +// [End sync_symmetry_encrypt_decrypt] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance 3DES ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_3des_ecb_synchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = '3DES_ECBSuccess'; + } catch { + this.message = '3DES_ECBFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/pages/Index.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..27e9b60c294a9d53e31ee81b284a4a1a67a96682 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { router } from '@kit.ArkUI'; + +@Entry +@Component +struct Index { + goToSample(url: string): void { + router.pushUrl({ + url: url, + }, router.RouterMode.Single, (err) => { + if (err) { + console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); + return; + } + console.info('pushUrl success'); + }) + } + + build() { + Column({ space: 5 }) { + Text('EncryptionDecryptionGuidance3DES').fontSize(20).fontWeight(FontWeight.Bolder) + Button($r('app.string.3des_ecb_encryption_decryption_asynchronous')) + .width('80%') + .onClick(() => { + this.goToSample('pages/3des_ecb_encryption_decryption_asynchronous') + }) + Button($r('app.string.3des_ecb_encryption_decryption_synchronous')) + .width('80%') + .onClick(() => { + this.goToSample('pages/3des_ecb_encryption_decryption_synchronous') + }) + Button($r('app.string.3des_ecb_encryption_decryption')) + .width('80%') + .onClick(() => { + this.goToSample('pages/3des_ecb_encryption_decryption') + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..29398d5c3cf7485cee23a2719927c6527769efa9 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/element/string.json @@ -0,0 +1,40 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "EncryptionDecryptionGuidance3DES" + }, + { + "name": "3des_ecb_encryption_decryption_asynchronous", + "value": "3des_ecb_encryption_decryption_asynchronous" + }, + { + "name": "3des_ecb_encryption_decryption_synchronous", + "value": "3des_ecb_encryption_decryption_synchronous" + }, + { + "name": "3des_ecb_encryption_decryption", + "value": "3des_ecb_encryption_decryption" + }, + { + "name": "call_3des_ecb", + "value": "Call 3DES_ECB" + }, + { + "name": "call_3des_ecb_asynchronous", + "value": "Call 3DES_ECB Asynchronous" + }, + { + "name": "call_3des_ecb_synchronous", + "value": "Call 3DES_ECB Synchronous" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/media/background.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/media/background.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/media/foreground.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/media/foreground.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/media/layered_image.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/media/startIcon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/media/startIcon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/profile/backup_config.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/profile/main_pages.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..f8979dc615a2494bd90253528a71a12510886e35 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,8 @@ +{ + "src": [ + "pages/Index", + "pages/3des_ecb_encryption_decryption_asynchronous", + "pages/3des_ecb_encryption_decryption_synchronous", + "pages/3des_ecb_encryption_decryption" + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/dark/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/mock/mock-config.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/mock/mock-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b9a78e201535765168a92d3543c690273ecdc019 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/mock/mock-config.json5 @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/ohosTest/ets/test/Ability.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidance3DES.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidance3DES.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..df2a2c7b595a53e80d8fbfdf609ee9b7b539cd44 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidance3DES.test.ets @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, it, expect, beforeAll } from '@ohos/hypium'; +// 导入测试依赖kit +import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; + +const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); +const bundleName = abilityDelegatorRegistry.getArguments().bundleName; +let want: Want; + +export default function encryptionDecryptionGuidance3DESTest() { + describe('encryptionDecryptionGuidance3DESTest', () => { + /** + * 打开应用 + */ + beforeAll(async () => { + want = { + bundleName: bundleName, + abilityName: 'EntryAbility' + }; + await delegator.startAbility(want); + const driver = Driver.create(); + await driver.delayMs(1000); + const ability: UIAbility = await delegator.getCurrentTopAbility(); + console.info('get top ability'); + await driver.delayMs(1000); + expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + }) + + /** + * 点击按钮,使用3DES对称密钥(ECB模式)加解密(ArkTS),异步方法 + */ + it('testEncryptionDecryptionGuidance3DES001', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidance3DES001 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('3des_ecb_encryption_decryption_asynchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call 3DES_ECB Asynchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('3DES_ECBSuccess')); + console.info('uitest: testEncryptionDecryptionGuidance3DES001 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用3DES对称密钥(ECB模式)加解密(ArkTS),同步方法 + */ + it('testEncryptionDecryptionGuidance3DES002', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidance3DES002 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('3des_ecb_encryption_decryption_synchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call 3DES_ECB Synchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('3DES_ECBSuccess')); + console.info('uitest: testEncryptionDecryptionGuidance3DES002 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用3DES对称密钥(ECB模式)加解密(C/C++) + */ + it('testEncryptionDecryptionGuidance3DES003', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidance3DES003 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('3des_ecb_encryption_decryption')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call 3DES_ECB')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Call Success Result:0')); + console.info('uitest: testEncryptionDecryptionGuidance3DES003 end'); + await driver.pressBack(); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/ohosTest/ets/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..8cd00ecf4aa258e6866c5708d21757bcd0aa2f09 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; +import encryptionDecryptionGuidance3DESTest from './EncryptionDecryptionGuidance3DES.test'; + +export default function testsuite() { + abilityTest(); + encryptionDecryptionGuidance3DESTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/ohosTest/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/test/LocalUnit.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/hvigor/hvigor-config.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/ohosTest.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..c603d0ffe7f41d3cafe796f209fa330cbceb7da3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/ohosTest.md @@ -0,0 +1,10 @@ +# 使用3DES对称密钥(ECB模式)加解密测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ---------------------------------------------------------- | ------------ | ---- | -------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 点击按钮,使用3DES对称密钥(ECB模式)加解密(ArkTS),异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用3DES对称密钥(ECB模式)加解密(ArkTS),同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用3DES对称密钥(ECB模式)加解密(C/C++) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/screenshots/EncryptionDecryptionGuidance3DES1.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/screenshots/EncryptionDecryptionGuidance3DES1.png new file mode 100644 index 0000000000000000000000000000000000000000..96bd7702c444c92c12bfb8061b31b50a00df20b0 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/screenshots/EncryptionDecryptionGuidance3DES1.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/screenshots/EncryptionDecryptionGuidance3DES2.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/screenshots/EncryptionDecryptionGuidance3DES2.png new file mode 100644 index 0000000000000000000000000000000000000000..99fd9fa4279a22c36f6276c0f4167e04fc137b0f Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidance3DES/screenshots/EncryptionDecryptionGuidance3DES2.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/AppScope/app.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..153d7314e8a8ce2c07280135ee43e3fcad879b26 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.encryptiondecryptionguidanceaes", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/AppScope/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..41dc2dbd559230d97f136fc54001bdedd375946b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "EncryptionDecryptionGuidanceAes" + } + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/AppScope/resources/base/media/app_icon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/AppScope/resources/base/media/app_icon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/README.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/README.md new file mode 100644 index 0000000000000000000000000000000000000000..12f3d0e21307d2c53eacf6c557f713945e754454 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/README.md @@ -0,0 +1,87 @@ +# 使用AES对称密钥加解密(C/C++) + +### 介绍 + +本示例主要展示了使用AES对称密钥(C/C++)GCM模式、CCM模式、CBC模式、ECB模式以及GCM模式分段加解密场景 。该工程中展示的代码详细描述可查如下链接。 + +- [使用AES对称密钥(GCM模式)加解密(C/C++)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-gcm-ndk.md) +- [使用AES对称密钥(CCM模式)加解密(C/C++)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-ccm-ndk.md) +- [使用AES对称密钥(CBC模式)加解密(C/C++)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-cbc-ndk.md) +- [使用AES对称密钥(ECB模式)加解密(C/C++)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-ecb-ndk.md) +- [使用AES对称密钥(GCM模式)分段加解密(C/C++)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-gcm-by-segment-ndk.md) + +### 效果预览 + +| 首页效果图 | 执行结果图 | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| | | + +### 使用说明 + +1. 运行Index主界面。 +2. 页面呈现上述执行结果图效果,点击不同按钮可以跳转到不同功能页面,点击跳转页面中按钮可以执行对应操作,并更新文本内容。 +3. 运行测试用例EncryptionDecryptionGuidanceAes.test.ets文件对页面代码进行测试可以全部通过。 + +### 工程目录 + +``` +entry/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ | ├── libentry + │ │ | ├── index.d.ts + │ │ | ├── oh-package.json5 + │ │ | ├── project + │ │ | ├── aes_cbc_encryption_decryption.cpp + │ │ | ├── aes_ccm_encryption_decryption.cpp + │ │ | ├── aes_ecb_encryption_decryption.cpp + │ │ | ├── aes_gcm_encryption_decryption.cpp + │ │ | ├── aes_gcm_segment_encryption_decryption.cpp + │ │ | ├── file.h + │ │ ├── CMakeList.txt + │ │ ├── napi_init.cpp + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // 使用AES对称密钥加解密(C/C++)示例代码 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ └── test + │ │ ├── Ability.test.ets + │ │ ├── EncryptionDecryptionGuidanceAes.test.ets // 自动化测试代码 + │ │ └── List.test.ets +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS 5.0.2 Release及以上。 + +3. DevEco Studio版本:DevEco Studio 5.0.2 Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS 5.0.2 Release及以上。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +```` +git init +git config core.sparsecheckout true +echo CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes > .git/info/sparse-checkout +git remote add origin https://gitee.com/harmonyos_samples/guide-snippets.git +git pull origin master +```` \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fb5a1ab654ae6715a0ab69a31900ad26dcf2487f --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/build-profile.json5 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.2(14)", + "targetSdkVersion": "5.0.2(14)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/code-linter.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2d8b081b615348a99aa197f90186f3d8891b7dc1 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/build-profile.json5 @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + "abiFilters": [ + "arm64-v8a", + "x86_64" + ] + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..118bdd4fe7699368a010e04c24f5bfc887cf1298 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libentry.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/CMakeLists.txt b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6123b8267aa92ec425cfca36fe25ca0a83667c4b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/CMakeLists.txt @@ -0,0 +1,15 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(EncryptionDecryptionGuidanceAes) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(entry SHARED napi_init.cpp types/project/aes_gcm_encryption_decryption.cpp types/project/aes_ccm_encryption_decryption.cpp types/project/aes_cbc_encryption_decryption.cpp types/project/aes_ecb_encryption_decryption.cpp types/project/aes_gcm_segment_encryption_decryption.cpp) +target_link_libraries(entry PUBLIC libace_napi.z.so libohcrypto.so) \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/napi_init.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..835ac96773ff547d9ff1e3e40b4a8a64145b2c1e --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/napi_init.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "types/project/file.h" + +static napi_value NAPI_Global_doTestAesGcm(napi_env env, napi_callback_info info) +{ + napi_value ret; + napi_create_int32(env, doTestAesGcm(), &ret); + return ret; +} + +static napi_value NAPI_Global_doTestAesCcm(napi_env env, napi_callback_info info) +{ + napi_value ret; + napi_create_int32(env, doTestAesCcm(), &ret); + return ret; +} + +static napi_value NAPI_Global_doTestAesCbc(napi_env env, napi_callback_info info) +{ + napi_value ret; + napi_create_int32(env, doTestAesCbc(), &ret); + return ret; +} + +static napi_value NAPI_Global_doTestAesEcb(napi_env env, napi_callback_info info) +{ + napi_value ret; + napi_create_int32(env, doTestAesEcb(), &ret); + return ret; +} + +static napi_value NAPI_Global_testAesGcmSeg(napi_env env, napi_callback_info info) +{ + napi_value ret; + napi_create_int32(env, doTestAesGcmSeg(), &ret); + return ret; +} + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + {"testAesGcm", nullptr, NAPI_Global_doTestAesGcm, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testAesCcm", nullptr, NAPI_Global_doTestAesCcm, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testAesCbc", nullptr, NAPI_Global_doTestAesCbc, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testAesEcb", nullptr, NAPI_Global_doTestAesEcb, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testAesGcmSeg", nullptr, NAPI_Global_testAesGcmSeg, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "entry", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/libentry/Index.d.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..12eaa9255666c3c51a65cdc9c597f886900da869 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const testAesGcm: () => number; + +export const testAesCcm: () => number; + +export const testAesCbc: () => number; + +export const testAesEcb: () => number; + +export const testAesGcmSeg: () => number; diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/libentry/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6abf3b7c20f22c62aaac6a995a25cae672f73f35 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libentry.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_cbc_encryption_decryption.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_cbc_encryption_decryption.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5546576babbf6426d169e8ea01691444f5af3199 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_cbc_encryption_decryption.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start cbc_encrypt_decrypt_aes_symkey] +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_sym_cipher.h" +#include +#include "file.h" + +OH_Crypto_ErrCode doTestAesCbc() +{ + OH_CryptoSymKeyGenerator *genCtx = nullptr; + OH_CryptoSymCipher *encCtx = nullptr; + OH_CryptoSymCipher *decCtx = nullptr; + OH_CryptoSymKey *keyCtx = nullptr; + OH_CryptoSymCipherParams *params = nullptr; + Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; + Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; + char *plainText = const_cast("this is test!"); + Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; + uint8_t iv[16] = {1, 2, 4, 12, 3, 4, 2, 3, 3, 2, 0, 4, 3, 1, 0, 10}; // iv使用安全随机数生成 + Crypto_DataBlob ivBlob = {.data = iv, .len = sizeof(iv)}; + // 生成对称密钥 + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 设置参数 + ret = OH_CryptoSymCipherParams_Create(¶ms); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivBlob); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 加密 + ret = OH_CryptoSymCipher_Create("AES128|CBC|PKCS7", &encCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(encCtx, &msgBlob, &outUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 解密 + ret = OH_CryptoSymCipher_Create("AES128|CBC|PKCS7", &decCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + +end: + OH_CryptoSymCipherParams_Destroy(params); + OH_CryptoSymCipher_Destroy(encCtx); + OH_CryptoSymCipher_Destroy(decCtx); + OH_CryptoSymKeyGenerator_Destroy(genCtx); + OH_CryptoSymKey_Destroy(keyCtx); + OH_Crypto_FreeDataBlob(&outUpdate); + OH_Crypto_FreeDataBlob(&decUpdate); + return ret; +} +// [End cbc_encrypt_decrypt_aes_symkey] \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_ccm_encryption_decryption.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_ccm_encryption_decryption.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ccbd56687a9915c14dacadf0af83cce6ab2e47b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_ccm_encryption_decryption.cpp @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start ccm_encrypt_decrypt_aes_symkey] +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_sym_cipher.h" +#include +#include "file.h" + +OH_Crypto_ErrCode doTestAesCcm() +{ + OH_CryptoSymKeyGenerator *genCtx = nullptr; + OH_CryptoSymCipher *encCtx = nullptr; + OH_CryptoSymCipher *decCtx = nullptr; + OH_CryptoSymKey *keyCtx = nullptr; + OH_CryptoSymCipherParams *params = nullptr; + + Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; + Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; + + uint8_t aad[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + uint8_t tag[12] = {0}; + uint8_t iv[7] = {1, 2, 4, 12, 3, 4, 2}; // iv使用安全随机数生成 + Crypto_DataBlob ivData = {.data = iv, .len = sizeof(iv)}; + Crypto_DataBlob aadData = {.data = aad, .len = sizeof(aad)}; + Crypto_DataBlob tagData = {.data = tag, .len = sizeof(tag)}; + Crypto_DataBlob tagOutPut = {.data = nullptr, .len = 0}; + char *plainText = const_cast("this is test!"); + Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; + // 生成对称密钥 + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 设置参数 + ret = OH_CryptoSymCipherParams_Create(¶ms); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivData); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_AAD_DATABLOB, &aadData); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tagData); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 加密 + ret = OH_CryptoSymCipher_Create("AES128|CCM", &encCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Update(encCtx, &msgBlob, &outUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(encCtx, nullptr, &tagOutPut); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 解密 + ret = OH_CryptoSymCipher_Create("AES128|CCM", &decCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tagOutPut); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + +end: + OH_CryptoSymCipherParams_Destroy(params); + OH_CryptoSymCipher_Destroy(encCtx); + OH_CryptoSymCipher_Destroy(decCtx); + OH_CryptoSymKeyGenerator_Destroy(genCtx); + OH_CryptoSymKey_Destroy(keyCtx); + OH_Crypto_FreeDataBlob(&outUpdate); + OH_Crypto_FreeDataBlob(&decUpdate); + OH_Crypto_FreeDataBlob(&tagOutPut); + return ret; +} +// [End ccm_encrypt_decrypt_aes_symkey] \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_ecb_encryption_decryption.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_ecb_encryption_decryption.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fcce356f72d4fa8df4d3b0665afc2a669dbf0de6 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_ecb_encryption_decryption.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start ecb_encrypt_decrypt_aes_symkey] +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_sym_cipher.h" +#include +#include "file.h" + +OH_Crypto_ErrCode doTestAesEcb() +{ + OH_CryptoSymKeyGenerator *genCtx = nullptr; + OH_CryptoSymCipher *encCtx = nullptr; + OH_CryptoSymCipher *decCtx = nullptr; + OH_CryptoSymKey *keyCtx = nullptr; + OH_CryptoSymCipherParams *params = nullptr; + char *plainText = const_cast("this is test"); + Crypto_DataBlob input = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; + Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; + Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; + + // 随机生成对称密钥 + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + // 创建参数 + ret = OH_CryptoSymCipherParams_Create(¶ms); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 加密操作 + ret = OH_CryptoSymCipher_Create("AES128|ECB|PKCS7", &encCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(encCtx, &input, &outUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 解密操作 + ret = OH_CryptoSymCipher_Create("AES128|ECB|PKCS7", &decCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + +end: + OH_CryptoSymCipherParams_Destroy(params); + OH_CryptoSymCipher_Destroy(encCtx); + OH_CryptoSymCipher_Destroy(decCtx); + OH_CryptoSymKeyGenerator_Destroy(genCtx); + OH_CryptoSymKey_Destroy(keyCtx); + OH_Crypto_FreeDataBlob(&outUpdate); + OH_Crypto_FreeDataBlob(&decUpdate); + return ret; +} +// [End ecb_encrypt_decrypt_aes_symkey] \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_gcm_encryption_decryption.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_gcm_encryption_decryption.cpp new file mode 100644 index 0000000000000000000000000000000000000000..118068d9c21565456c7855059fbec9dae920ee7e --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_gcm_encryption_decryption.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start gcm_encrypt_decrypt_aes_symkey] +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_sym_cipher.h" +#include +#include "file.h" + +OH_Crypto_ErrCode doTestAesGcm() +{ + OH_CryptoSymKeyGenerator *genCtx = nullptr; + OH_CryptoSymCipher *encCtx = nullptr; + OH_CryptoSymCipher *decCtx = nullptr; + OH_CryptoSymKey *keyCtx = nullptr; + OH_CryptoSymCipherParams *params = nullptr; + + Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; + Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; + + uint8_t aad[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + uint8_t tag[16] = {0}; + uint8_t iv[12] = {1, 2, 4, 12, 3, 4, 2, 3, 3, 2, 0, 4}; // iv使用安全随机数生成 + Crypto_DataBlob ivData = {.data = iv, .len = sizeof(iv)}; + Crypto_DataBlob aadData = {.data = aad, .len = sizeof(aad)}; + Crypto_DataBlob tagData = {.data = tag, .len = sizeof(tag)}; + Crypto_DataBlob tagOutPut = {.data = nullptr, .len = 0}; + char *plainText = const_cast("this is test!"); + Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; + + // 生成对称密钥 + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 设置参数 + ret = OH_CryptoSymCipherParams_Create(¶ms); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivData); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_AAD_DATABLOB, &aadData); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tagData); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 加密 + ret = OH_CryptoSymCipher_Create("AES128|GCM|PKCS7", &encCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Update(encCtx, &msgBlob, &outUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(encCtx, nullptr, &tagOutPut); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 解密 + ret = OH_CryptoSymCipher_Create("AES128|GCM|PKCS7", &decCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tagOutPut); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + +end: + OH_CryptoSymCipherParams_Destroy(params); + OH_CryptoSymCipher_Destroy(encCtx); + OH_CryptoSymCipher_Destroy(decCtx); + OH_CryptoSymKeyGenerator_Destroy(genCtx); + OH_CryptoSymKey_Destroy(keyCtx); + OH_Crypto_FreeDataBlob(&outUpdate); + OH_Crypto_FreeDataBlob(&decUpdate); + OH_Crypto_FreeDataBlob(&tagOutPut); + return ret; +} +// [End gcm_encrypt_decrypt_aes_symkey] \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_gcm_segment_encryption_decryption.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_gcm_segment_encryption_decryption.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b43fd23d9f0adc1af6d569a44a11b02cfd89c90 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/aes_gcm_segment_encryption_decryption.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start gcm_seg_encrypt_decrypt_aes_symkey] +#include +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_sym_cipher.h" +#include "file.h" + +#define OH_CRYPTO_GCM_TAG_LEN 16 +#define OH_CRYPTO_MAX_TEST_DATA_LEN 128 +OH_Crypto_ErrCode doTestAesGcmSeg() +{ + OH_CryptoSymKeyGenerator *genCtx = nullptr; + OH_CryptoSymCipher *encCtx = nullptr; + OH_CryptoSymCipher *decCtx = nullptr; + OH_CryptoSymKey *keyCtx = nullptr; + OH_CryptoSymCipherParams *params = nullptr; + + char *plainText = const_cast("aaaaa.....bbbbb.....ccccc.....ddddd.....eee"); + Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; + + uint8_t aad[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + uint8_t tagArr[16] = {0}; + uint8_t iv[12] = {1, 2, 4, 12, 3, 4, 2, 3, 3, 2, 0, 4}; // iv使用安全随机数生成 + Crypto_DataBlob tag = {.data = nullptr, .len = 0}; + Crypto_DataBlob ivBlob = {.data = iv, .len = sizeof(iv)}; + Crypto_DataBlob aadBlob = {.data = aad, .len = sizeof(aad)}; + Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; + Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; + Crypto_DataBlob tagInit = {.data = tagArr, .len = sizeof(tagArr)}; + int32_t cipherLen = 0; + int blockSize = 20; + int32_t randomLen = strlen(plainText); + int cnt = randomLen / blockSize; + int rem = randomLen % blockSize; + uint8_t cipherText[OH_CRYPTO_MAX_TEST_DATA_LEN] = {0}; + Crypto_DataBlob cipherBlob; + + // 生成密钥 + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("AES128", &genCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 设置参数 + ret = OH_CryptoSymCipherParams_Create(¶ms); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivBlob); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_AAD_DATABLOB, &aadBlob); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tagInit); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 加密 + ret = OH_CryptoSymCipher_Create("AES128|GCM|PKCS7", &encCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + for (int i = 0; i < cnt; i++) { + msgBlob.len = blockSize; + ret = OH_CryptoSymCipher_Update(encCtx, &msgBlob, &outUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + msgBlob.data += blockSize; + memcpy(&cipherText[cipherLen], outUpdate.data, outUpdate.len); + cipherLen += outUpdate.len; + } + if (rem > 0) { + msgBlob.len = rem; + ret = OH_CryptoSymCipher_Update(encCtx, (Crypto_DataBlob *)&msgBlob, &outUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + memcpy(&cipherText[cipherLen], outUpdate.data, outUpdate.len); + cipherLen += outUpdate.len; + } + ret = OH_CryptoSymCipher_Final(encCtx, nullptr, &tag); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 解密 + cipherBlob = {.data = reinterpret_cast(cipherText), .len = (size_t)cipherLen}; + msgBlob.data -= strlen(plainText) - rem; + msgBlob.len = strlen(plainText); + ret = OH_CryptoSymCipher_Create("AES128|GCM|PKCS7", &decCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tag); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(decCtx, &cipherBlob, &decUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + +end: + OH_CryptoSymCipherParams_Destroy(params); + OH_CryptoSymCipher_Destroy(encCtx); + OH_CryptoSymCipher_Destroy(decCtx); + OH_CryptoSymKeyGenerator_Destroy(genCtx); + OH_CryptoSymKey_Destroy(keyCtx); + OH_Crypto_FreeDataBlob(&outUpdate); + OH_Crypto_FreeDataBlob(&tag); + OH_Crypto_FreeDataBlob(&decUpdate); + return ret; +} +// [End gcm_seg_encrypt_decrypt_aes_symkey] \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/file.h b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/file.h new file mode 100644 index 0000000000000000000000000000000000000000..0b8939ca3dc36757e475b979f6f4a41a4e2b5c93 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/cpp/types/project/file.h @@ -0,0 +1,26 @@ +#ifndef ENCRYPTIONDECRYPTIONGUIDANCEAES_FILE_H +#define ENCRYPTIONDECRYPTIONGUIDANCEAES_FILE_H +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "CryptoArchitectureKit/crypto_common.h" + +OH_Crypto_ErrCode doTestAesGcm(); +OH_Crypto_ErrCode doTestAesCcm(); +OH_Crypto_ErrCode doTestAesCbc(); +OH_Crypto_ErrCode doTestAesEcb(); +OH_Crypto_ErrCode doTestAesGcmSeg(); + +#endif //ENCRYPTIONDECRYPTIONGUIDANCEAES_FILE_H \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/ets/entryability/EntryAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/ets/pages/Index.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..97b84c02d1377c86115b2852a4974f9b5392ca8e --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import testNapi from 'libentry.so'; + +@Entry +@Component +struct Index { + @State message: string = 'EncryptionDecryptionGuidanceAes'; + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(24) + .fontWeight(FontWeight.Bold) + Button($r('app.string.call_aes_gcm')) + .width('70%') + .onClick(() => { + let ret: number = testNapi.testAesGcm(); + this.message = 'AES_GCMSuccess Result:' + ret.toString(); + }) + Button($r('app.string.call_aes_ccm')) + .width('70%') + .onClick(() => { + let ret: number = testNapi.testAesCcm(); + this.message = 'AES_CCMSuccess Result:' + ret.toString(); + }) + Button($r('app.string.call_aes_cbc')) + .width('70%') + .onClick(() => { + let ret: number = testNapi.testAesCbc(); + this.message = 'AES_CBCSuccess Result:' + ret.toString(); + }) + Button($r('app.string.call_aes_ecb')) + .width('70%') + .onClick(() => { + let ret: number = testNapi.testAesEcb(); + this.message = 'AES_ECBSuccess Result:' + ret.toString(); + }) + Button($r('app.string.call_aes_gcm_segment')) + .width('70%') + .onClick(() => { + let ret: number = testNapi.testAesEcb(); + this.message = 'AES_GCM_SegmentSuccess Result:' + ret.toString(); + }) + } + .width('100%') + .height('100%') + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..c7153b9d5383c3d6a6a4aec5181fe3a51b06070c --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/element/string.json @@ -0,0 +1,36 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "EncryptionDecryptionGuidanceAes" + }, + { + "name": "call_aes_gcm", + "value": "Call AES_GCM" + }, + { + "name": "call_aes_ccm", + "value": "Call AES_CCM" + }, + { + "name": "call_aes_cbc", + "value": "Call AES_CBC" + }, + { + "name": "call_aes_ecb", + "value": "Call AES_ECB" + }, + { + "name": "call_aes_gcm_segment", + "value": "Call AES_GCM_Segment" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/media/background.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/media/background.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/media/foreground.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/media/foreground.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/media/layered_image.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/media/startIcon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/media/startIcon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/profile/backup_config.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/profile/main_pages.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/dark/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/ohosTest/ets/test/Ability.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceAes.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceAes.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..5d9bff15da9e807bb4e3aec246dd8e3dbe15b49f --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceAes.test.ets @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, it, expect, beforeAll } from '@ohos/hypium'; +// 导入测试依赖kit +import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; + +const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); +const bundleName = abilityDelegatorRegistry.getArguments().bundleName; +let want: Want; + +export default function encryptionDecryptionGuidanceAesTest() { + describe('EncryptionDecryptionGuidanceAesTest', () => { + /** + * 打开应用 + */ + beforeAll(async () => { + want = { + bundleName: bundleName, + abilityName: 'EntryAbility' + }; + await delegator.startAbility(want); + const driver = Driver.create(); + await driver.delayMs(1000); + const ability: UIAbility = await delegator.getCurrentTopAbility(); + console.info('get top ability'); + await driver.delayMs(1000); + expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + }) + + /** + * 点击按钮,使用AES对称密钥(GCM模式)加解密(C/C++) + */ + it('testEncryptionDecryptionGuidanceAes001', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAes001 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Call AES_GCM')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_GCMSuccess Result:0')); + console.info('uitest: testEncryptionDecryptionGuidanceAes001 end'); + }) + + /** + * 点击按钮,使用AES对称密钥(CCM模式)加解密(C/C++) + */ + it('testEncryptionDecryptionGuidanceAes002', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAes002 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Call AES_CCM')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_CCMSuccess Result:0')); + console.info('uitest: testEncryptionDecryptionGuidanceAes002 end'); + }) + + /** + * 点击按钮,使用AES对称密钥(CBC模式)加解密(C/C++) + */ + it('testEncryptionDecryptionGuidanceAes003', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAes003 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Call AES_CBC')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_CBCSuccess Result:0')); + console.info('uitest: testEncryptionDecryptionGuidanceAes003 end'); + }) + + /** + * 点击按钮,使用AES对称密钥(ECB模式)加解密(C/C++) + */ + it('testEncryptionDecryptionGuidanceAes004', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAes004 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Call AES_ECB')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_ECBSuccess Result:0')); + console.info('uitest: testEncryptionDecryptionGuidanceAes004 end'); + }) + + /** + * 点击按钮,使用AES对称密钥(GCM模式)分段加解密(C/C++) + */ + it('testEncryptionDecryptionGuidanceAes005', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAes005 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Call AES_GCM_Segment')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_GCM_SegmentSuccess Result:0')); + console.info('uitest: testEncryptionDecryptionGuidanceAes005 end'); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/ohosTest/ets/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..26cf884d58d50318163299cccf2fc2ee38f482be --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; +import encryptionDecryptionGuidanceAesTest from './EncryptionDecryptionGuidanceAes.test'; + +export default function testsuite() { + abilityTest(); + encryptionDecryptionGuidanceAesTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/ohosTest/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/test/LocalUnit.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/hvigor/hvigor-config.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/ohosTest.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..8415a3609c3dc791d0ef027b891cfe622f53ccd5 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/ohosTest.md @@ -0,0 +1,12 @@ +# 使用AES对称密钥加解密(C/C++)测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ----------------------------------------------------- | ------------ | ---- | -------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 点击按钮,使用AES对称密钥(GCM模式)加解密(C/C++) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(CCM模式)加解密(C/C++) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(CBC模式)加解密(C/C++) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(ECB模式)加解密(C/C++) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(GCM模式)分段加解密(C/C++) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/screenshots/EncryptionDecryptionGuidanceAes1.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/screenshots/EncryptionDecryptionGuidanceAes1.png new file mode 100644 index 0000000000000000000000000000000000000000..6cc9d7d8de20f13cd606f5d06ff035fdc2d3d336 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/screenshots/EncryptionDecryptionGuidanceAes1.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/screenshots/EncryptionDecryptionGuidanceAes2.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/screenshots/EncryptionDecryptionGuidanceAes2.png new file mode 100644 index 0000000000000000000000000000000000000000..90c853e56b099e675de29feb8828d8123c487cbb Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAes/screenshots/EncryptionDecryptionGuidanceAes2.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/AppScope/app.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f6ae8fe6054272263ccae98ee296eca6ca23341d --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.encryptiondecryptionguidanceaesarkts", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/AppScope/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..96fc25d01b6383cbebb7640bd61eadbac364ea90 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "EncryptionDecryptionGuidanceAesArkTs" + } + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/AppScope/resources/base/media/app_icon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/AppScope/resources/base/media/app_icon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/README.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f0f183aa87109eec0c8214fd3904ad20ea8a805b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/README.md @@ -0,0 +1,88 @@ +# 使用AES对称密钥加解密(ArkTS) + +### 介绍 + +本示例主要展示了使用AES对称密钥不同模式加解密(ArkTS)的同步异步方法和C/C++场景 。该工程中展示的代码详细描述可查如下链接。 + +- [使用AES对称密钥(GCM模式)加解密(ArkTS)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-gcm.md) +- [使用AES对称密钥(CCM模式)加解密(ArkTS)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-ccm.md) +- [使用AES对称密钥(CBC模式)加解密(ArkTS)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-cbc.md) +- [使用AES对称密钥(ECB模式)加解密(ArkTS)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-ecb.md) +- [使用AES对称密钥(GCM模式)分段加解密(ArkTS)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-aes-sym-encrypt-decrypt-gcm-by-segment.md) + +### 效果预览 + +| 首页效果图 | 执行结果图 | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| | | + +### 使用说明 + +1. 运行Index主界面。 +2. 页面呈现上述执行结果图效果,点击不同按钮可以跳转到不同功能页面,点击跳转页面中按钮可以执行对应操作,并更新文本内容。 +3. 运行测试用例EncryptionDecryptionGuidanceAesArkTs.test.ets文件对页面代码进行测试可以全部通过。 + +### 工程目录 + +``` +entry/src/ + ├── main + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── aes_cbc_encryption_decryption + │ │ | ├── aes_cbc_encryption_decryption_asynchronous.ets + │ │ | ├── aes_cbc_encryption_decryption_synchronous.ets + │ │ ├── aes_ccm_encryption_decryption + │ │ | ├── aes_ccm_encryption_decryption_asynchronous.ets + │ │ | ├── aes_ccm_encryption_decryption_synchronous.ets + │ │ ├── aes_ecb_encryption_decryption + │ │ | ├── aes_ecb_encryption_decryption_asynchronous.ets + │ │ | ├── aes_ecb_encryption_decryption_synchronous.ets + │ │ ├── aes_gcm_encryption_decryption + │ │ | ├── aes_gcm_encryption_decryption_asynchronous.ets + │ │ | ├── aes_gcm_encryption_decryption_synchronous.ets + │ │ ├── aes_gcm_seg_encryption_decryption + │ │ | ├── aes_gcm_seg_encryption_decryption_asynchronous.ets + │ │ | ├── aes_gcm_seg_encryption_decryption_synchronous.ets + │ │ ├── Index.ets // 使用AES对称密钥加解密(ArkTS)示例代码 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ └── test + │ │ ├── Ability.test.ets + │ │ ├── EncryptionDecryptionGuidanceAesArkTs.test.ets // 自动化测试代码 + │ │ └── List.test.ets +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS 5.0.2 Release及以上。 + +3. DevEco Studio版本:DevEco Studio 5.0.2 Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS 5.0.2 Release及以上。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +```` +git init +git config core.sparsecheckout true +echo CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs > .git/info/sparse-checkout +git remote add origin https://gitee.com/harmonyos_samples/guide-snippets.git +git pull origin master +```` \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fb5a1ab654ae6715a0ab69a31900ad26dcf2487f --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/build-profile.json5 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.2(14)", + "targetSdkVersion": "5.0.2(14)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/code-linter.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e7569e3056e27af38e9991b7ea73ec10f3ba8a05 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/build-profile.json5 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c9cb6c8174858277c9b0d465a51547dcab16d5ff --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/entryability/EntryAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..edc2839f203ba057c186e19b0cbbbf80c8faa8b3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/Index.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..cef25aaed0a600a6fa28e92f52e887f8b97a3eee --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { router } from '@kit.ArkUI'; + +@Entry +@Component +struct Index { + goToSample(url: string): void { + router.pushUrl({ + url: url, + }, router.RouterMode.Single, (err) => { + if (err) { + console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); + return; + } + console.info('pushUrl success'); + }) + } + + build() { + Column({ space: 5 }) { + Text('EncryptionDecryptionGuidanceAesArkTs').fontSize(23).fontWeight(FontWeight.Bolder) + Button($r('app.string.aes_gcm_encryption_decryption_asynchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/aes_gcm_encryption_decryption/aes_gcm_encryption_decryption_asynchronous') + }) + Button($r('app.string.aes_gcm_encryption_decryption_synchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/aes_gcm_encryption_decryption/aes_gcm_encryption_decryption_synchronous') + }) + Button($r('app.string.aes_ccm_encryption_decryption_asynchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/aes_ccm_encryption_decryption/aes_ccm_encryption_decryption_asynchronous') + }) + Button($r('app.string.aes_ccm_encryption_decryption_synchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/aes_ccm_encryption_decryption/aes_ccm_encryption_decryption_synchronous') + }) + Button($r('app.string.aes_cbc_encryption_decryption_asynchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/aes_cbc_encryption_decryption/aes_cbc_encryption_decryption_asynchronous') + }) + Button($r('app.string.aes_cbc_encryption_decryption_synchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/aes_cbc_encryption_decryption/aes_cbc_encryption_decryption_synchronous') + }) + Button($r('app.string.aes_ecb_encryption_decryption_asynchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/aes_ecb_encryption_decryption/aes_ecb_encryption_decryption_asynchronous') + }) + Button($r('app.string.aes_ecb_encryption_decryption_synchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/aes_ecb_encryption_decryption/aes_ecb_encryption_decryption_synchronous') + }) + Button($r('app.string.aes_gcm_seg_encryption_decryption_asynchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/aes_gcm_seg_encryption_decryption/aes_gcm_seg_encryption_decryption_asynchronous') + }) + Button($r('app.string.aes_gcm_seg_encryption_decryption_synchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/aes_gcm_seg_encryption_decryption/aes_gcm_seg_encryption_decryption_synchronous') + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_cbc_encryption_decryption/aes_cbc_encryption_decryption_asynchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_cbc_encryption_decryption/aes_cbc_encryption_decryption_asynchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..7db8cf2008c8207957ae2742fd8af80bffed5940 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_cbc_encryption_decryption/aes_cbc_encryption_decryption_asynchronous.ets @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start cbc_encrypt_decrypt_aes_symkey_async] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function generateRandom(len: number) { + let rand = cryptoFramework.createRandom(); + let generateRandSync = rand.generateRandomSync(len); + return generateRandSync; +} + +function genIvParamsSpec() { + let ivBlob = generateRandom(16); + let ivParamsSpec: cryptoFramework.IvParamsSpec = { + algName: 'IvParamsSpec', + iv: ivBlob + }; + return ivParamsSpec; +} + +let iv = genIvParamsSpec(); + +// 加密消息 +async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('AES128|CBC|PKCS7'); + await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, iv); + let cipherData = await cipher.doFinal(plainText); + return cipherData; +} + +// 解密消息 +async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('AES128|CBC|PKCS7'); + await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, iv); + let decryptData = await decoder.doFinal(cipherText); + return decryptData; +} + +async function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); + let symKey = await aesGenerator.convertKey(symKeyBlob); + console.info('convertKey success'); + return symKey; +} + +async function aesCBC() { + try { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = await genSymKeyByData(keyData); + let message = 'This is a test'; + 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'); + } + } catch (error) { + console.error(`AES CBC “${error}“, error code: ${error.code}`); + } +} +// [End cbc_encrypt_decrypt_aes_symkey_async] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance Aes ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_aes_cbc_asynchronous')) + .width('70%') + .onClick(() => { + try { + aesCBC(); + this.message = 'AES_CBCAsynchronousSuccess'; + } catch { + this.message = 'AES_CBCAsynchronousFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_cbc_encryption_decryption/aes_cbc_encryption_decryption_synchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_cbc_encryption_decryption/aes_cbc_encryption_decryption_synchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..9417d3be74a85b537382b3085e2cf5c628ec7b01 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_cbc_encryption_decryption/aes_cbc_encryption_decryption_synchronous.ets @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start cbc_encrypt_decrypt_aes_symkey_sync] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function generateRandom(len: number) { + let rand = cryptoFramework.createRandom(); + let generateRandSync = rand.generateRandomSync(len); + return generateRandSync; +} + +function genIvParamsSpec() { + let ivBlob = generateRandom(16); + let ivParamsSpec: cryptoFramework.IvParamsSpec = { + algName: 'IvParamsSpec', + iv: ivBlob + }; + return ivParamsSpec; +} + +let iv = genIvParamsSpec(); + +// 加密消息 +function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('AES128|CBC|PKCS7'); + cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, iv); + let cipherData = cipher.doFinalSync(plainText); + return cipherData; +} + +// 解密消息 +function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('AES128|CBC|PKCS7'); + 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 aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); + let symKey = aesGenerator.convertKeySync(symKeyBlob); + console.info('convertKeySync success'); + return symKey; +} + +function main() { + try { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = genSymKeyByData(keyData); + 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'); + } + } catch (error) { + console.error(`AES CBC “${error}“, error code: ${error.code}`); + } +} +// [End cbc_encrypt_decrypt_aes_symkey_sync] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance Aes ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_aes_cbc_synchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = 'AES_CBCSynchronousSuccess'; + } catch { + this.message = 'AES_CBCSynchronousFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_ccm_encryption_decryption/aes_ccm_encryption_decryption_asynchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_ccm_encryption_decryption/aes_ccm_encryption_decryption_asynchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..8bae8f3c819fecf1e66c8bce5c2ac49db74e585a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_ccm_encryption_decryption/aes_ccm_encryption_decryption_asynchronous.ets @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start ccm_encrypt_decrypt_aes_symkey_async] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function genCcmParamsSpec() { + let rand: cryptoFramework.Random = cryptoFramework.createRandom(); + let ivBlob: cryptoFramework.DataBlob = rand.generateRandomSync(7); + let aadBlob: cryptoFramework.DataBlob = rand.generateRandomSync(8); + let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 12 bytes + let dataTag = new Uint8Array(arr); + let tagBlob: cryptoFramework.DataBlob = { + data: dataTag + }; + // CCM的authTag在加密时从doFinal结果中获取,在解密时填入init函数的params参数中 + let ccmParamsSpec: cryptoFramework.CcmParamsSpec = { + iv: ivBlob, + aad: aadBlob, + authTag: tagBlob, + algName: 'CcmParamsSpec' + }; + return ccmParamsSpec; +} + +let ccmParams = genCcmParamsSpec(); + +// 加密消息 +async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('AES128|CCM'); + await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, ccmParams); + let encryptUpdate = await cipher.update(plainText); + // ccm模式加密doFinal时传入空,获得tag数据,并更新至ccmParams对象中。 + ccmParams.authTag = await cipher.doFinal(null); + return encryptUpdate; +} + +// 解密消息 +async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('AES128|CCM'); + await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, ccmParams); + let decryptUpdate = await decoder.doFinal(cipherText); + return decryptUpdate; +} + +async function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); + let symKey = await aesGenerator.convertKey(symKeyBlob); + console.info('convertKey success'); + return symKey; +} + +async function main() { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = await genSymKeyByData(keyData); + let message = 'This is a test'; + 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'); + } +} +// [End ccm_encrypt_decrypt_aes_symkey_async] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance Aes ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_aes_ccm_asynchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = 'AES_CCMAsynchronousSuccess'; + } catch { + this.message = 'AES_CCMAsynchronousFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_ccm_encryption_decryption/aes_ccm_encryption_decryption_synchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_ccm_encryption_decryption/aes_ccm_encryption_decryption_synchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..262ab0a434102d723ba5efbcc03562bbb90ee59b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_ccm_encryption_decryption/aes_ccm_encryption_decryption_synchronous.ets @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start ccm_encrypt_decrypt_aes_symkey_sync] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function genCcmParamsSpec() { + let rand: cryptoFramework.Random = cryptoFramework.createRandom(); + let ivBlob: cryptoFramework.DataBlob = rand.generateRandomSync(7); + let aadBlob: cryptoFramework.DataBlob = rand.generateRandomSync(8); + let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 12 bytes + let dataTag = new Uint8Array(arr); + let tagBlob: cryptoFramework.DataBlob = { + data: dataTag + }; + // CCM的authTag在加密时从doFinal结果中获取,在解密时填入init函数的params参数中 + let ccmParamsSpec: cryptoFramework.CcmParamsSpec = { + iv: ivBlob, + aad: aadBlob, + authTag: tagBlob, + algName: 'CcmParamsSpec' + }; + return ccmParamsSpec; +} + +let ccmParams = genCcmParamsSpec(); + +// 加密消息 +function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('AES128|CCM'); + cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, ccmParams); + let encryptUpdate = cipher.updateSync(plainText); + // ccm模式加密doFinal时传入空,获得tag数据,并更新至ccmParams对象中。 + ccmParams.authTag = cipher.doFinalSync(null); + return encryptUpdate; +} + +// 解密消息 +function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('AES128|CCM'); + decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, ccmParams); + let decryptUpdate = decoder.doFinalSync(cipherText); + return decryptUpdate; +} + +function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); + let symKey = aesGenerator.convertKeySync(symKeyBlob); + console.info('convertKeySync success'); + return symKey; +} + +function main() { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = genSymKeyByData(keyData); + 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'); + } +} +// [End ccm_encrypt_decrypt_aes_symkey_sync] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance Aes ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_aes_ccm_synchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = 'AES_CCMSynchronousSuccess'; + } catch { + this.message = 'AES_CCMSynchronousFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_ecb_encryption_decryption/aes_ecb_encryption_decryption_asynchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_ecb_encryption_decryption/aes_ecb_encryption_decryption_asynchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..457bd5d0aae7eca4cce920f1bc891e776179db9f --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_ecb_encryption_decryption/aes_ecb_encryption_decryption_asynchronous.ets @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start ecb_encrypt_decrypt_aes_symkey_async] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 加密消息 +async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('AES128|ECB|PKCS7'); + await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null); + let cipherData = await cipher.doFinal(plainText); + return cipherData; +} + +// 解密消息 +async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('AES128|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 aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); + let symKey = await aesGenerator.convertKey(symKeyBlob); + console.info('convertKey success'); + return symKey; +} + +async function aesECB() { + try { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = await genSymKeyByData(keyData); + let message = 'This is a test'; + 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'); + } + } catch (error) { + console.error(`AES ECB “${error}“, error code: ${error.code}`); + } +} +// [End ecb_encrypt_decrypt_aes_symkey_async] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance Aes ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_aes_ecb_asynchronous')) + .width('70%') + .onClick(() => { + try { + aesECB(); + this.message = 'AES_ECBAsynchronousSuccess'; + } catch { + this.message = 'AES_ECBAsynchronousFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_ecb_encryption_decryption/aes_ecb_encryption_decryption_synchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_ecb_encryption_decryption/aes_ecb_encryption_decryption_synchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..920e400cdd47c87e52029adfb13da3fb17ad93d9 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_ecb_encryption_decryption/aes_ecb_encryption_decryption_synchronous.ets @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start ecb_encrypt_decrypt_aes_symkey_sync] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 加密消息 +function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('AES128|ECB|PKCS7'); + cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null); + let cipherData = cipher.doFinalSync(plainText); + return cipherData; +} + +// 解密消息 +function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('AES128|ECB|PKCS7'); + decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, null); + let decryptData = decoder.doFinalSync(cipherText); + return decryptData; +} + +function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); + let symKey = aesGenerator.convertKeySync(symKeyBlob); + console.info('convertKeySync success'); + return symKey; +} + +function main() { + try { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = genSymKeyByData(keyData); + 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'); + } + } catch (error) { + console.error(`AES ECB “${error}“, error code: ${error.code}`); + } +} +// [End ecb_encrypt_decrypt_aes_symkey_sync] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance Aes ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_aes_ecb_synchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = 'AES_ECBSynchronousSuccess'; + } catch { + this.message = 'AES_ECBSynchronousFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_gcm_encryption_decryption/aes_gcm_encryption_decryption_asynchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_gcm_encryption_decryption/aes_gcm_encryption_decryption_asynchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..321c95d6153bcd021769fdfefc1b6080288f2bf3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_gcm_encryption_decryption/aes_gcm_encryption_decryption_asynchronous.ets @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start gcm_encrypt_decrypt_aes_symkey_async] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function generateRandom(len: number) { + let rand = cryptoFramework.createRandom(); + let generateRandSync = rand.generateRandomSync(len); + return generateRandSync; +} + +function genGcmParamsSpec() { + let ivBlob = generateRandom(12); + let arr = [1, 2, 3, 4, 5, 6, 7, 8]; // 8 bytes + let dataAad = new Uint8Array(arr); + let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; + arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes + let dataTag = new Uint8Array(arr); + let tagBlob: cryptoFramework.DataBlob = { + data: dataTag + }; + // GCM的authTag在加密时从doFinal结果中获取,在解密时填入init函数的params参数中 + let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { + iv: ivBlob, + aad: aadBlob, + authTag: tagBlob, + algName: 'GcmParamsSpec' + }; + return gcmParamsSpec; +} + +let gcmParams = genGcmParamsSpec(); + +// 加密消息 +async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('AES128|GCM|PKCS7'); + await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); + let encryptUpdate = await cipher.update(plainText); + // gcm模式加密doFinal时传入空,获得tag数据,并更新至gcmParams对象中。 + gcmParams.authTag = await cipher.doFinal(null); + return encryptUpdate; +} + +// 解密消息 +async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('AES128|GCM|PKCS7'); + await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); + let decryptUpdate = await decoder.update(cipherText); + // gcm模式解密doFinal时传入空,验证init时传入的tag数据,如果验证失败会抛出异常。 + let decryptData = await decoder.doFinal(null); + if (decryptData == null) { + console.info('GCM decrypt success, decryptData is null'); + } + return decryptUpdate; +} + +async function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); + let symKey = await aesGenerator.convertKey(symKeyBlob); + console.info('convertKey success'); + return symKey; +} + +async function main() { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = await genSymKeyByData(keyData); + let message = 'This is a test'; + 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'); + } +} +// [End gcm_encrypt_decrypt_aes_symkey_async] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance Aes ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_aes_gcm_asynchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = 'AES_GCMAsynchronousSuccess'; + } catch { + this.message = 'AES_GCMAsynchronousFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_gcm_encryption_decryption/aes_gcm_encryption_decryption_synchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_gcm_encryption_decryption/aes_gcm_encryption_decryption_synchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..d8f1bf0e107e1d6014edf58db7394015ce0a38dc --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_gcm_encryption_decryption/aes_gcm_encryption_decryption_synchronous.ets @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start gcm_encrypt_decrypt_aes_symkey_sync] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function generateRandom(len: number) { + let rand = cryptoFramework.createRandom(); + let generateRandSync = rand.generateRandomSync(len); + return generateRandSync; +} + +function genGcmParamsSpec() { + let ivBlob = generateRandom(12); + let arr = [1, 2, 3, 4, 5, 6, 7, 8]; // 8 bytes + let dataAad = new Uint8Array(arr); + let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; + arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes + let dataTag = new Uint8Array(arr); + let tagBlob: cryptoFramework.DataBlob = { + data: dataTag + }; + // GCM的authTag在加密时从doFinal结果中获取,在解密时填入init函数的params参数中 + let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { + iv: ivBlob, + aad: aadBlob, + authTag: tagBlob, + algName: 'GcmParamsSpec' + }; + return gcmParamsSpec; +} + +let gcmParams = genGcmParamsSpec(); + +// 加密消息 +function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('AES128|GCM|PKCS7'); + cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); + let encryptUpdate = cipher.updateSync(plainText); + // gcm模式加密doFinal时传入空,获得tag数据,并更新至gcmParams对象中。 + gcmParams.authTag = cipher.doFinalSync(null); + return encryptUpdate; +} + +// 解密消息 +function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('AES128|GCM|PKCS7'); + decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); + let decryptUpdate = decoder.updateSync(cipherText); + // gcm模式解密doFinal时传入空,验证init时传入的tag数据,如果验证失败会抛出异常。 + let decryptData = decoder.doFinalSync(null); + if (decryptData == null) { + console.info('GCM decrypt success, decryptData is null'); + } + return decryptUpdate; +} + +function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); + let symKey = aesGenerator.convertKeySync(symKeyBlob); + console.info('convertKeySync success'); + return symKey; +} + +function main() { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = genSymKeyByData(keyData); + 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'); + } +} +// [End gcm_encrypt_decrypt_aes_symkey_sync] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance Aes ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_aes_gcm_synchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = 'AES_GCMSynchronousSuccess'; + } catch { + this.message = 'AES_GCMSynchronousFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_gcm_seg_encryption_decryption/aes_gcm_seg_encryption_decryption_asynchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_gcm_seg_encryption_decryption/aes_gcm_seg_encryption_decryption_asynchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..5e19e08c8f996c95f184ccb7077a9117afcfa6a1 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_gcm_seg_encryption_decryption/aes_gcm_seg_encryption_decryption_asynchronous.ets @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start gcm_seg_encrypt_decrypt_aes_symkey_async] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function generateRandom(len: number) { + let rand = cryptoFramework.createRandom(); + let generateRandSync = rand.generateRandomSync(len); + return generateRandSync; +} + +function genGcmParamsSpec() { + let ivBlob = generateRandom(12); + let arr = [1, 2, 3, 4, 5, 6, 7, 8]; // 8 bytes + let dataAad = new Uint8Array(arr); + let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; + arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes + let dataTag = new Uint8Array(arr); + let tagBlob: cryptoFramework.DataBlob = { + data: dataTag + }; // The GCM authTag is obtained by doFinal() in encryption and passed in params of init() in decryption. + let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { + iv: ivBlob, + aad: aadBlob, + authTag: tagBlob, + algName: 'GcmParamsSpec' + }; + return gcmParamsSpec; +} + +let gcmParams = genGcmParamsSpec(); + +// 分段加密消息 +async function encryptMessageUpdateBySegment(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('AES128|GCM|PKCS7'); + await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); + let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 + let cipherText = new Uint8Array(); + for (let i = 0; i < plainText.data.length; i += updateLength) { + let updateMessage = plainText.data.subarray(i, i + updateLength); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + // 分段update + let updateOutput = await cipher.update(updateMessageBlob); + // 把update的结果拼接起来,得到密文(有些情况下还需拼接doFinal的结果,这取决于分组模式 + // 和填充模式,本例中GCM模式的doFinal结果只包含authTag而不含密文,所以不需要拼接) + let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length); + mergeText.set(cipherText); + mergeText.set(updateOutput.data, cipherText.length); + cipherText = mergeText; + } + gcmParams.authTag = await cipher.doFinal(null); + let cipherBlob: cryptoFramework.DataBlob = { data: cipherText }; + return cipherBlob; +} + +// 分段解密消息 +async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('AES128|GCM|PKCS7'); + await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); + let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 + let decryptText = new Uint8Array(); + for (let i = 0; i < cipherText.data.length; i += updateLength) { + let updateMessage = cipherText.data.subarray(i, i + updateLength); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + // 分段update + let updateOutput = await decoder.update(updateMessageBlob); + // 把update的结果拼接起来,得到明文 + let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length); + mergeText.set(decryptText); + mergeText.set(updateOutput.data, decryptText.length); + decryptText = mergeText; + } + let decryptData = await decoder.doFinal(null); + if (decryptData == null) { + console.info('GCM decrypt success, decryptData is null'); + } + let decryptBlob: cryptoFramework.DataBlob = { data: decryptText }; + return decryptBlob; +} + +async function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); + let symKey = await aesGenerator.convertKey(symKeyBlob); + console.info('convertKey success'); + return symKey; +} + +async function aes() { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = await genSymKeyByData(keyData); + let message = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee'; // 假设信息总共43字节,根据utf-8解码后,也是43字节 + let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; + let encryptText = await encryptMessageUpdateBySegment(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'); + } +} +// [End gcm_seg_encrypt_decrypt_aes_symkey_async] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance Aes ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_aes_gcm_segmentation_asynchronous')) + .width('70%') + .onClick(() => { + try { + aes(); + this.message = 'AES_GCMAsynchronousSuccess'; + } catch { + this.message = 'AES_GCMAsynchronousFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_gcm_seg_encryption_decryption/aes_gcm_seg_encryption_decryption_synchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_gcm_seg_encryption_decryption/aes_gcm_seg_encryption_decryption_synchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..d31b3456ff268696127087c3265ded8fa8d106fd --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/ets/pages/aes_gcm_seg_encryption_decryption/aes_gcm_seg_encryption_decryption_synchronous.ets @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start gcm_seg_encrypt_decrypt_aes_symkey_sync] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function generateRandom(len: number) { + let rand = cryptoFramework.createRandom(); + let generateRandSync = rand.generateRandomSync(len); + return generateRandSync; +} + +function genGcmParamsSpec() { + let ivBlob = generateRandom(12); + let arr = [1, 2, 3, 4, 5, 6, 7, 8]; // 8 bytes + let dataAad = new Uint8Array(arr); + let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; + arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes + let dataTag = new Uint8Array(arr); + let tagBlob: cryptoFramework.DataBlob = { + data: dataTag + }; // The GCM authTag is obtained by doFinal() in encryption and passed in params of init() in decryption. + let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { + iv: ivBlob, + aad: aadBlob, + authTag: tagBlob, + algName: 'GcmParamsSpec' + }; + return gcmParamsSpec; +} + +let gcmParams = genGcmParamsSpec(); + +// 分段加密消息 +function encryptMessageUpdateBySegment(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('AES128|GCM|PKCS7'); + cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); + let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 + let cipherText = new Uint8Array(); + for (let i = 0; i < plainText.data.length; i += updateLength) { + let updateMessage = plainText.data.subarray(i, i + updateLength); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + // 分段update + let updateOutput = cipher.updateSync(updateMessageBlob); + // 把update的结果拼接起来,得到密文(有些情况下还需拼接doFinal的结果,这取决于分组模式 + // 和填充模式,本例中GCM模式的doFinal结果只包含authTag而不含密文,所以不需要拼接) + let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length); + mergeText.set(cipherText); + mergeText.set(updateOutput.data, cipherText.length); + cipherText = mergeText; + } + gcmParams.authTag = cipher.doFinalSync(null); + let cipherBlob: cryptoFramework.DataBlob = { data: cipherText }; + return cipherBlob; +} + +// 分段解密消息 +function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('AES128|GCM|PKCS7'); + decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); + let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 + let decryptText = new Uint8Array(); + for (let i = 0; i < cipherText.data.length; i += updateLength) { + let updateMessage = cipherText.data.subarray(i, i + updateLength); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + // 分段update + let updateOutput = decoder.updateSync(updateMessageBlob); + // 把update的结果拼接起来,得到明文 + let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length); + mergeText.set(decryptText); + mergeText.set(updateOutput.data, decryptText.length); + decryptText = mergeText; + } + let decryptData = decoder.doFinalSync(null); + if (decryptData == null) { + console.info('GCM decrypt success, decryptData is null'); + } + let decryptBlob: cryptoFramework.DataBlob = { data: decryptText }; + return decryptBlob; +} + +function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); + let symKey = aesGenerator.convertKeySync(symKeyBlob); + console.info('convertKeySync success'); + return symKey; +} + +function main() { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = genSymKeyByData(keyData); + let message = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee'; // 假设信息总共43字节,根据utf-8解码后,也是43字节 + let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; + let encryptText = encryptMessageUpdateBySegment(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'); + } +} +// [End gcm_seg_encrypt_decrypt_aes_symkey_sync] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance Aes ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_aes_gcm_segmentation_synchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = 'AES_GCMSynchronousSuccess'; + } catch { + this.message = 'AES_GCMSynchronousFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..d6327c87f55a328b662665f8fe6bdd2240ccc863 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/element/string.json @@ -0,0 +1,96 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "EncryptionDecryptionGuidanceAesArkTs" + }, + { + "name": "aes_gcm_encryption_decryption_asynchronous", + "value": "aes_gcm_encryption_decryption_asynchronous" + }, + { + "name": "aes_gcm_encryption_decryption_synchronous", + "value": "aes_gcm_encryption_decryption_synchronous" + }, + { + "name": "aes_ccm_encryption_decryption_asynchronous", + "value": "aes_ccm_encryption_decryption_asynchronous" + }, + { + "name": "aes_ccm_encryption_decryption_synchronous", + "value": "aes_ccm_encryption_decryption_synchronous" + }, + { + "name": "aes_cbc_encryption_decryption_asynchronous", + "value": "aes_cbc_encryption_decryption_asynchronous" + }, + { + "name": "aes_cbc_encryption_decryption_synchronous", + "value": "aes_cbc_encryption_decryption_synchronous" + }, + { + "name": "aes_ecb_encryption_decryption_asynchronous", + "value": "aes_ecb_encryption_decryption_asynchronous" + }, + { + "name": "aes_ecb_encryption_decryption_synchronous", + "value": "aes_ecb_encryption_decryption_synchronous" + }, + { + "name": "aes_gcm_seg_encryption_decryption_asynchronous", + "value": "aes_gcm_seg_encryption_decryption_asynchronous" + }, + { + "name": "aes_gcm_seg_encryption_decryption_synchronous", + "value": "aes_gcm_seg_encryption_decryption_synchronous" + }, + { + "name": "call_aes_cbc_asynchronous", + "value": "Call AES_CBC Asynchronous" + }, + { + "name": "call_aes_cbc_synchronous", + "value": "Call AES_CBC Synchronous" + }, + { + "name": "call_aes_ccm_asynchronous", + "value": "Call AES_CCM Asynchronous" + }, + { + "name": "call_aes_ccm_synchronous", + "value": "Call AES_CCM Synchronous" + }, + { + "name": "call_aes_ecb_asynchronous", + "value": "Call AES_ECB Asynchronous" + }, + { + "name": "call_aes_ecb_synchronous", + "value": "Call AES_ECB Synchronous" + }, + { + "name": "call_aes_gcm_asynchronous", + "value": "Call AES_GCM Asynchronous" + }, + { + "name": "call_aes_gcm_synchronous", + "value": "Call AES_GCM Synchronous" + }, + { + "name": "call_aes_gcm_segmentation_asynchronous", + "value": "Call AES_GCM segmentation Asynchronous" + }, + { + "name": "call_aes_gcm_segmentation_synchronous", + "value": "Call AES_GCM segmentation Synchronous" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/media/background.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/media/background.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/media/foreground.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/media/foreground.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/media/layered_image.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/media/startIcon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/media/startIcon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/profile/backup_config.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/profile/main_pages.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..fad7ebe82b7bc5c06d26c5dd240242451e31abdb --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,15 @@ +{ + "src": [ + "pages/Index", + "pages/aes_cbc_encryption_decryption/aes_cbc_encryption_decryption_asynchronous", + "pages/aes_cbc_encryption_decryption/aes_cbc_encryption_decryption_synchronous", + "pages/aes_ccm_encryption_decryption/aes_ccm_encryption_decryption_asynchronous", + "pages/aes_ccm_encryption_decryption/aes_ccm_encryption_decryption_synchronous", + "pages/aes_ecb_encryption_decryption/aes_ecb_encryption_decryption_asynchronous", + "pages/aes_ecb_encryption_decryption/aes_ecb_encryption_decryption_synchronous", + "pages/aes_gcm_encryption_decryption/aes_gcm_encryption_decryption_asynchronous", + "pages/aes_gcm_encryption_decryption/aes_gcm_encryption_decryption_synchronous", + "pages/aes_gcm_seg_encryption_decryption/aes_gcm_seg_encryption_decryption_asynchronous", + "pages/aes_gcm_seg_encryption_decryption/aes_gcm_seg_encryption_decryption_synchronous" + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/dark/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/ohosTest/ets/test/Ability.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceAesArkTs.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceAesArkTs.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f5217bc5f5f55976cdb0afb55f3e187c2311e70a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceAesArkTs.test.ets @@ -0,0 +1,224 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, it, expect, beforeAll } from '@ohos/hypium'; +// 导入测试依赖kit +import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; + +const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); +const bundleName = abilityDelegatorRegistry.getArguments().bundleName; +let want: Want; + +export default function encryptionDecryptionGuidanceAesArkTsTest() { + describe('encryptionDecryptionGuidanceAesArkTsTest', () => { + /** + * 打开应用 + */ + beforeAll(async () => { + want = { + bundleName: bundleName, + abilityName: 'EntryAbility' + }; + await delegator.startAbility(want); + const driver = Driver.create(); + await driver.delayMs(1000); + const ability: UIAbility = await delegator.getCurrentTopAbility(); + console.info('get top ability'); + await driver.delayMs(1000); + expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + }) + + /** + * 点击按钮,使用AES对称密钥(GCM模式)加解密(ArkTS),异步方法 + */ + it('testEncryptionDecryptionGuidanceAesArkTs001', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs001 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('aes_gcm_encryption_decryption_asynchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call AES_GCM Asynchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_GCMAsynchronousSuccess')); + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs001 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用AES对称密钥(GCM模式)加解密(ArkTS),同步方法 + */ + it('testEncryptionDecryptionGuidanceAesArkTs002', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs002 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('aes_gcm_encryption_decryption_synchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call AES_GCM Synchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_GCMSynchronousSuccess')); + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs002 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用AES对称密钥(CCM模式)加解密(ArkTS),异步方法 + */ + it('testEncryptionDecryptionGuidanceAesArkTs003', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs003 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('aes_ccm_encryption_decryption_asynchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call AES_CCM Asynchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_CCMAsynchronousSuccess')); + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs003 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用AES对称密钥(CCM模式)加解密(ArkTS),同步方法 + */ + it('testEncryptionDecryptionGuidanceAesArkTs004', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs004 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('aes_ccm_encryption_decryption_synchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call AES_CCM Synchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_CCMSynchronousSuccess')); + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs004 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用AES对称密钥(CBC模式)加解密(ArkTS),异步方法 + */ + it('testEncryptionDecryptionGuidanceAesArkTs005', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs005 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('aes_cbc_encryption_decryption_asynchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call AES_CBC Asynchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_CBCAsynchronousSuccess')); + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs005 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用AES对称密钥(CBC模式)加解密(ArkTS),同步方法 + */ + it('testEncryptionDecryptionGuidanceAesArkTs006', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs006 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('aes_cbc_encryption_decryption_synchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call AES_CBC Synchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_CBCSynchronousSuccess')); + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs006 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用AES对称密钥(ECB模式)加解密(ArkTS),异步方法 + */ + it('testEncryptionDecryptionGuidanceAesArkTs007', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs007 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('aes_ecb_encryption_decryption_asynchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call AES_ECB Asynchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_ECBAsynchronousSuccess')); + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs007 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用AES对称密钥(ECB模式)加解密(ArkTS),同步方法 + */ + it('testEncryptionDecryptionGuidanceAesArkTs008', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs008 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('aes_ecb_encryption_decryption_synchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call AES_ECB Synchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_ECBSynchronousSuccess')); + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs008 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用AES对称密钥(GCM模式)分段加解密(ArkTS),异步方法 + */ + it('testEncryptionDecryptionGuidanceAesArkTs009', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs009 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('aes_gcm_seg_encryption_decryption_asynchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call AES_GCM segmentation Asynchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_GCMAsynchronousSuccess')); + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs009 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用AES对称密钥(GCM模式)分段加解密(ArkTS),同步方法 + */ + it('testEncryptionDecryptionGuidanceAesArkTs010', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs010 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('aes_gcm_seg_encryption_decryption_synchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call AES_GCM segmentation Synchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('AES_GCMSynchronousSuccess')); + console.info('uitest: testEncryptionDecryptionGuidanceAesArkTs010 end'); + await driver.pressBack(); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/ohosTest/ets/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..a7ceb187530644103f398c3cf6c6b55abc71ad03 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; +import encryptionDecryptionGuidanceAesArkTsTest from './EncryptionDecryptionGuidanceAesArkTs.test'; +export default function testsuite() { + abilityTest(); + encryptionDecryptionGuidanceAesArkTsTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/ohosTest/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/test/LocalUnit.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/hvigor/hvigor-config.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/ohosTest.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..ad51c77def0b7d3a42a25c291d5323cc43b0b12b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/ohosTest.md @@ -0,0 +1,17 @@ +# 使用AES对称密钥加解密(ArkTS)测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ------------------------------------------------------------ | ------------ | ---- | -------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 点击按钮,使用AES对称密钥(GCM模式)加解密(ArkTS),异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(GCM模式)加解密(ArkTS),同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(CCM模式)加解密(ArkTS),异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(CCM模式)加解密(ArkTS),同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(CBC模式)加解密(ArkTS),异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(CBC模式)加解密(ArkTS),同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(ECB模式)加解密(ArkTS),异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(ECB模式)加解密(ArkTS),同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(GCM模式)分段加解密(ArkTS),异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用AES对称密钥(GCM模式)分段加解密(ArkTS),同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/screenshots/EncryptionDecryptionGuidanceAesArkTs1.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/screenshots/EncryptionDecryptionGuidanceAesArkTs1.png new file mode 100644 index 0000000000000000000000000000000000000000..7afd55b3bd573b41dfc31a75115d0f3a55768ff0 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/screenshots/EncryptionDecryptionGuidanceAesArkTs1.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/screenshots/EncryptionDecryptionGuidanceAesArkTs2.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/screenshots/EncryptionDecryptionGuidanceAesArkTs2.png new file mode 100644 index 0000000000000000000000000000000000000000..379ca89cd1c3b39a04fb5cacb990842adc406499 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceAesArkTs/screenshots/EncryptionDecryptionGuidanceAesArkTs2.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/AppScope/app.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c7be98756b482d8b1b6bbef6e75546b3a59e4cbc --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.encryptiondecryptionguidancersa", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/AppScope/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..a097cdb19d896f946557ad86446ef4d55f7e0448 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "EncryptionDecryptionGuidanceRSA" + } + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/AppScope/resources/base/media/app_icon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/AppScope/resources/base/media/app_icon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/README.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/README.md new file mode 100644 index 0000000000000000000000000000000000000000..fc5a632b74344d4dedc7c89bad507e0a3d1253d7 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/README.md @@ -0,0 +1,80 @@ +# 使用RSA非对称密钥加解密 + +### 介绍 + +本示例主要展示了使用RSA非对称密钥(PKCS1模式)加解密、使用RSA非对称密钥分段加解密、使用RSA非对称密钥(PKCS1_OAEP模式)加解密场景 。该工程中展示的代码详细描述可查如下链接。 + +- [使用RSA非对称密钥(PKCS1模式)加解密](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-rsa-asym-encrypt-decrypt-pkcs1.md) +- [使用RSA非对称密钥分段加解密](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-rsa-asym-encrypt-decrypt-by-segment.md) +- [使用RSA非对称密钥(PKCS1_OAEP模式)加解密](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-rsa-asym-encrypt-decrypt-pkcs1_oaep.md) + +### 效果预览 + +| 首页效果图 | 执行结果图 | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| | | + +### 使用说明 + +1. 运行Index主界面。 +2. 页面呈现上述执行结果图效果,点击不同按钮可以跳转到不同功能页面,点击跳转页面中按钮可以执行对应操作,并更新文本内容。 +3. 运行测试用例EncryptionDecryptionGuidanceRSA.test.ets文件对页面代码进行测试可以全部通过。 + +### 工程目录 + +``` +entry/src/ + ├── main + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // 使用RSA非对称密钥加解密示例代码 + │ │ ├── rsa_pkcs1 + │ │ | ├── RSAPKCS1Async.ets + │ │ | ├── RSAPKCS1Sync.ets + │ │ ├── rsa_pkcs1_oaep + │ │ | ├── RSAPKCS1OAEPAsync.ets + │ │ | ├── RSAPKCS1OAEPSync.ets + │ │ ├── rsa_segmentation + │ │ | ├── RSASegmentationAsync.ets + │ │ | ├── RSASegmentationSync.ets + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ └── test + │ │ ├── Ability.test.ets + │ │ ├── EncryptionDecryptionGuidanceRSA.test.ets // 自动化测试代码 + │ │ └── List.test.ets +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS 5.0.2 Release及以上。 + +3. DevEco Studio版本:DevEco Studio 5.0.2 Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS 5.0.2 Release及以上。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +```` +git init +git config core.sparsecheckout true +echo CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA > .git/info/sparse-checkout +git remote add origin https://gitee.com/harmonyos_samples/guide-snippets.git +git pull origin master +```` \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fb5a1ab654ae6715a0ab69a31900ad26dcf2487f --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/build-profile.json5 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.2(14)", + "targetSdkVersion": "5.0.2(14)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/code-linter.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e7569e3056e27af38e9991b7ea73ec10f3ba8a05 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/build-profile.json5 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c9cb6c8174858277c9b0d465a51547dcab16d5ff --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/entryability/EntryAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..edc2839f203ba057c186e19b0cbbbf80c8faa8b3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/Index.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..47012d3acb9300f95bc4761f5dd0487cd33d4398 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { router } from '@kit.ArkUI'; + +@Entry +@Component +struct Index { + @State message: string = 'EncryptionDecryptionGuidanceRSA'; + + goToSample(url: string): void { + router.pushUrl({ + url: url, + }, router.RouterMode.Single, (err) => { + if (err) { + console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); + return; + } + console.info('pushUrl success'); + }) + } + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(25) + .fontWeight(FontWeight.Bold) + Button($r('app.string.rsa_pkcs1_async')) + .width('70%') + .onClick(() => { + this.goToSample('pages/rsa_pkcs1/RSAPKCS1Async') + }) + Button($r('app.string.rsa_pkcs1_sync')) + .width('70%') + .onClick(() => { + this.goToSample('pages/rsa_pkcs1/RSAPKCS1Sync') + }) + Button($r('app.string.rsa_segmentation_async')) + .width('70%') + .onClick(() => { + this.goToSample('pages/rsa_segmentation/RSASegmentationAsync') + }) + Button($r('app.string.rsa_segmentation_sync')) + .width('70%') + .onClick(() => { + this.goToSample('pages/rsa_segmentation/RSASegmentationSync') + }) + Button($r('app.string.rsa_pkcs1_oaep_async')) + .width('70%') + .onClick(() => { + this.goToSample('pages/rsa_pkcs1_oaep/RSAPKCS1OAEPAsync') + }) + Button($r('app.string.rsa_pkcs1_oaep_sync')) + .width('70%') + .onClick(() => { + this.goToSample('pages/rsa_pkcs1_oaep/RSAPKCS1OAEPSync') + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_pkcs1/RSAPKCS1Async.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_pkcs1/RSAPKCS1Async.ets new file mode 100644 index 0000000000000000000000000000000000000000..33683cc72ee34889e00a88d21609ad602b6e78a2 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_pkcs1/RSAPKCS1Async.ets @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 加密消息 +async function encryptMessagePromise(publicKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('RSA1024|PKCS1'); + 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('RSA1024|PKCS1'); + await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, privateKey, null); + let decryptData = await decoder.doFinal(cipherText); + return decryptData; +} + +// 生成RSA密钥对 +async function genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) { + let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData }; + let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData }; + let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); + let keyPair = await rsaGenerator.convertKey(pubKeyBlob, priKeyBlob); + console.info('convertKey success'); + return keyPair; +} + +async function main() { + let pkData = + new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, + 2, 129, 129, 0, 197, 64, 10, 198, 14, 110, 65, 92, 206, 35, 28, 123, 153, 24, 134, 255, 145, 74, 42, 173, 40, 215, + 146, 58, 143, 46, 10, 195, 154, 160, 69, 196, 220, 152, 179, 44, 111, 200, 84, 78, 215, 73, 210, 181, 12, 29, 70, + 68, 36, 135, 153, 89, 230, 202, 130, 212, 111, 243, 234, 92, 131, 62, 145, 50, 73, 48, 104, 245, 46, 70, 45, 157, + 147, 143, 140, 162, 156, 216, 220, 49, 121, 142, 194, 33, 223, 201, 0, 16, 163, 210, 240, 118, 92, 147, 121, 220, + 17, 114, 24, 52, 125, 135, 176, 88, 21, 83, 86, 17, 156, 88, 250, 48, 79, 86, 128, 248, 105, 208, 133, 140, 13, + 153, 164, 191, 136, 164, 44, 53, 2, 3, 1, 0, 1]); + let skData = + new Uint8Array([48, 130, 2, 119, 2, 1, 0, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 4, 130, 2, 97, 48, + 130, 2, 93, 2, 1, 0, 2, 129, 129, 0, 197, 64, 10, 198, 14, 110, 65, 92, 206, 35, 28, 123, 153, 24, 134, 255, 145, + 74, 42, 173, 40, 215, 146, 58, 143, 46, 10, 195, 154, 160, 69, 196, 220, 152, 179, 44, 111, 200, 84, 78, 215, 73, + 210, 181, 12, 29, 70, 68, 36, 135, 153, 89, 230, 202, 130, 212, 111, 243, 234, 92, 131, 62, 145, 50, 73, 48, 104, + 245, 46, 70, 45, 157, 147, 143, 140, 162, 156, 216, 220, 49, 121, 142, 194, 33, 223, 201, 0, 16, 163, 210, 240, + 118, 92, 147, 121, 220, 17, 114, 24, 52, 125, 135, 176, 88, 21, 83, 86, 17, 156, 88, 250, 48, 79, 86, 128, 248, + 105, 208, 133, 140, 13, 153, 164, 191, 136, 164, 44, 53, 2, 3, 1, 0, 1, 2, 129, 128, 70, 75, 184, 139, 53, 1, 94, + 17, 240, 244, 218, 101, 193, 253, 215, 190, 164, 204, 197, 192, 200, 89, 107, 39, 171, 119, 65, 38, 204, 168, 105, + 180, 234, 217, 16, 161, 185, 132, 175, 103, 25, 154, 153, 153, 36, 36, 26, 178, 150, 66, 45, 8, 185, 19, 90, 228, + 210, 177, 30, 200, 177, 141, 78, 184, 248, 59, 113, 154, 145, 73, 160, 24, 73, 157, 86, 207, 186, 32, 95, 200, + 106, 252, 107, 69, 170, 193, 216, 196, 181, 142, 74, 203, 15, 18, 89, 228, 152, 19, 239, 21, 233, 98, 121, 214, + 57, 187, 111, 239, 223, 248, 199, 70, 223, 108, 108, 113, 234, 144, 155, 95, 246, 144, 244, 122, 39, 55, 127, 81, + 2, 65, 0, 246, 96, 188, 0, 0, 104, 221, 105, 139, 144, 63, 175, 209, 87, 179, 162, 88, 192, 99, 82, 125, 53, 54, + 48, 70, 245, 239, 37, 15, 242, 247, 84, 115, 187, 196, 95, 156, 40, 165, 60, 64, 102, 13, 229, 243, 2, 149, 0, + 232, 226, 221, 192, 95, 11, 12, 208, 5, 181, 98, 62, 210, 190, 141, 235, 2, 65, 0, 204, 244, 34, 10, 105, 80, 76, + 116, 163, 35, 231, 168, 187, 206, 189, 101, 215, 103, 80, 115, 86, 11, 34, 127, 203, 114, 84, 188, 121, 174, 169, + 31, 142, 2, 182, 27, 140, 225, 157, 227, 71, 98, 15, 203, 187, 213, 5, 190, 20, 121, 8, 30, 193, 100, 232, 101, + 141, 8, 124, 20, 29, 78, 6, 95, 2, 65, 0, 204, 43, 225, 224, 6, 118, 224, 117, 100, 200, 199, 94, 70, 23, 109, + 175, 173, 232, 208, 230, 61, 8, 105, 189, 156, 48, 150, 91, 154, 89, 248, 136, 173, 215, 254, 166, 84, 220, 130, + 1, 234, 68, 40, 100, 84, 251, 224, 202, 254, 51, 115, 28, 198, 38, 124, 25, 175, 129, 94, 199, 61, 17, 216, 189, + 2, 64, 72, 230, 129, 129, 48, 138, 134, 87, 106, 123, 231, 247, 165, 173, 216, 194, 115, 198, 228, 223, 209, 120, + 46, 114, 68, 92, 75, 117, 170, 214, 140, 131, 147, 208, 181, 19, 193, 157, 178, 186, 87, 246, 178, 101, 166, 79, + 20, 54, 211, 51, 101, 199, 2, 197, 48, 192, 134, 84, 193, 69, 170, 82, 201, 131, 2, 65, 0, 213, 165, 55, 166, 131, + 210, 195, 56, 250, 147, 195, 61, 205, 208, 189, 185, 40, 52, 50, 119, 137, 23, 246, 46, 220, 108, 52, 23, 152, + 154, 94, 32, 144, 195, 184, 249, 21, 168, 12, 57, 222, 18, 60, 117, 81, 157, 72, 30, 155, 190, 165, 242, 228, 139, + 240, 184, 145, 170, 103, 210, 160, 161, 135, 13]); + let keyPair = await genKeyPairByData(pkData, skData); + 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'); + } +} + +@Entry +@Component +struct Index { + @State message: string = 'RSA PKCS1 Async'; + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(25) + .fontWeight(FontWeight.Bold) + Button($r('app.string.call_rsa_pkcs1_async')) + .onClick(async () => { + try { + await main(); + this.message = 'RSA PKCS1 Async Success'; + } catch (error) { + console.error(error); + this.message = 'RSA PKCS1 Async Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_pkcs1/RSAPKCS1Sync.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_pkcs1/RSAPKCS1Sync.ets new file mode 100644 index 0000000000000000000000000000000000000000..ed3d8769b0404fd77ee798b678f81b340c4934e1 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_pkcs1/RSAPKCS1Sync.ets @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 加密消息 +function encryptMessage(publicKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('RSA1024|PKCS1'); + cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, publicKey, null); + let encryptData = cipher.doFinalSync(plainText); + return encryptData; +} + +// 解密消息 +function decryptMessage(privateKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('RSA1024|PKCS1'); + decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, privateKey, null); + let decryptData = decoder.doFinalSync(cipherText); + return decryptData; +} + +// 生成RSA密钥对 +function genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) { + let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData }; + let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData }; + let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); + let keyPair = rsaGenerator.convertKeySync(pubKeyBlob, priKeyBlob); + console.info('convertKeySync success'); + return keyPair; +} + +function main() { + let pkData = + new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, + 2, 129, 129, 0, 197, 64, 10, 198, 14, 110, 65, 92, 206, 35, 28, 123, 153, 24, 134, 255, 145, 74, 42, 173, 40, 215, + 146, 58, 143, 46, 10, 195, 154, 160, 69, 196, 220, 152, 179, 44, 111, 200, 84, 78, 215, 73, 210, 181, 12, 29, 70, + 68, 36, 135, 153, 89, 230, 202, 130, 212, 111, 243, 234, 92, 131, 62, 145, 50, 73, 48, 104, 245, 46, 70, 45, 157, + 147, 143, 140, 162, 156, 216, 220, 49, 121, 142, 194, 33, 223, 201, 0, 16, 163, 210, 240, 118, 92, 147, 121, 220, + 17, 114, 24, 52, 125, 135, 176, 88, 21, 83, 86, 17, 156, 88, 250, 48, 79, 86, 128, 248, 105, 208, 133, 140, 13, + 153, 164, 191, 136, 164, 44, 53, 2, 3, 1, 0, 1]); + let skData = + new Uint8Array([48, 130, 2, 119, 2, 1, 0, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 4, 130, 2, 97, 48, + 130, 2, 93, 2, 1, 0, 2, 129, 129, 0, 197, 64, 10, 198, 14, 110, 65, 92, 206, 35, 28, 123, 153, 24, 134, 255, 145, + 74, 42, 173, 40, 215, 146, 58, 143, 46, 10, 195, 154, 160, 69, 196, 220, 152, 179, 44, 111, 200, 84, 78, 215, 73, + 210, 181, 12, 29, 70, 68, 36, 135, 153, 89, 230, 202, 130, 212, 111, 243, 234, 92, 131, 62, 145, 50, 73, 48, 104, + 245, 46, 70, 45, 157, 147, 143, 140, 162, 156, 216, 220, 49, 121, 142, 194, 33, 223, 201, 0, 16, 163, 210, 240, + 118, 92, 147, 121, 220, 17, 114, 24, 52, 125, 135, 176, 88, 21, 83, 86, 17, 156, 88, 250, 48, 79, 86, 128, 248, + 105, 208, 133, 140, 13, 153, 164, 191, 136, 164, 44, 53, 2, 3, 1, 0, 1, 2, 129, 128, 70, 75, 184, 139, 53, 1, 94, + 17, 240, 244, 218, 101, 193, 253, 215, 190, 164, 204, 197, 192, 200, 89, 107, 39, 171, 119, 65, 38, 204, 168, 105, + 180, 234, 217, 16, 161, 185, 132, 175, 103, 25, 154, 153, 153, 36, 36, 26, 178, 150, 66, 45, 8, 185, 19, 90, 228, + 210, 177, 30, 200, 177, 141, 78, 184, 248, 59, 113, 154, 145, 73, 160, 24, 73, 157, 86, 207, 186, 32, 95, 200, + 106, 252, 107, 69, 170, 193, 216, 196, 181, 142, 74, 203, 15, 18, 89, 228, 152, 19, 239, 21, 233, 98, 121, 214, + 57, 187, 111, 239, 223, 248, 199, 70, 223, 108, 108, 113, 234, 144, 155, 95, 246, 144, 244, 122, 39, 55, 127, 81, + 2, 65, 0, 246, 96, 188, 0, 0, 104, 221, 105, 139, 144, 63, 175, 209, 87, 179, 162, 88, 192, 99, 82, 125, 53, 54, + 48, 70, 245, 239, 37, 15, 242, 247, 84, 115, 187, 196, 95, 156, 40, 165, 60, 64, 102, 13, 229, 243, 2, 149, 0, + 232, 226, 221, 192, 95, 11, 12, 208, 5, 181, 98, 62, 210, 190, 141, 235, 2, 65, 0, 204, 244, 34, 10, 105, 80, 76, + 116, 163, 35, 231, 168, 187, 206, 189, 101, 215, 103, 80, 115, 86, 11, 34, 127, 203, 114, 84, 188, 121, 174, 169, + 31, 142, 2, 182, 27, 140, 225, 157, 227, 71, 98, 15, 203, 187, 213, 5, 190, 20, 121, 8, 30, 193, 100, 232, 101, + 141, 8, 124, 20, 29, 78, 6, 95, 2, 65, 0, 204, 43, 225, 224, 6, 118, 224, 117, 100, 200, 199, 94, 70, 23, 109, + 175, 173, 232, 208, 230, 61, 8, 105, 189, 156, 48, 150, 91, 154, 89, 248, 136, 173, 215, 254, 166, 84, 220, 130, + 1, 234, 68, 40, 100, 84, 251, 224, 202, 254, 51, 115, 28, 198, 38, 124, 25, 175, 129, 94, 199, 61, 17, 216, 189, + 2, 64, 72, 230, 129, 129, 48, 138, 134, 87, 106, 123, 231, 247, 165, 173, 216, 194, 115, 198, 228, 223, 209, 120, + 46, 114, 68, 92, 75, 117, 170, 214, 140, 131, 147, 208, 181, 19, 193, 157, 178, 186, 87, 246, 178, 101, 166, 79, + 20, 54, 211, 51, 101, 199, 2, 197, 48, 192, 134, 84, 193, 69, 170, 82, 201, 131, 2, 65, 0, 213, 165, 55, 166, 131, + 210, 195, 56, 250, 147, 195, 61, 205, 208, 189, 185, 40, 52, 50, 119, 137, 23, 246, 46, 220, 108, 52, 23, 152, + 154, 94, 32, 144, 195, 184, 249, 21, 168, 12, 57, 222, 18, 60, 117, 81, 157, 72, 30, 155, 190, 165, 242, 228, 139, + 240, 184, 145, 170, 103, 210, 160, 161, 135, 13]); + let keyPair = genKeyPairByData(pkData, skData); + 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 = encryptMessage(pubKey, plainText); + let decryptText = decryptMessage(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'); + } +} + +@Entry +@Component +struct Index { + @State message: string = 'RSA PKCS1 Sync'; + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(25) + .fontWeight(FontWeight.Bold) + Button($r('app.string.call_rsa_pkcs1_sync')) + .onClick(() => { + try { + main(); + this.message = 'RSA PKCS1 Sync Success'; + } catch (error) { + console.error(error); + this.message = 'RSA PKCS1 Sync Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_pkcs1_oaep/RSAPKCS1OAEPAsync.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_pkcs1_oaep/RSAPKCS1OAEPAsync.ets new file mode 100644 index 0000000000000000000000000000000000000000..2ba5cba1861c14e6afa769b207f5f8400875527a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_pkcs1_oaep/RSAPKCS1OAEPAsync.ets @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start pss_verify_rsa_keypair_sign_async] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 根据密钥参数属性构造RSA非对称密钥对密钥参数 +function genRsaKeyPairSpec(nIn: bigint, eIn: bigint, dIn: bigint) { + let rsaCommSpec: cryptoFramework.RSACommonParamsSpec = { + n: nIn, + algName: 'RSA', + specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC + }; + let rsaKeyPairSpec: cryptoFramework.RSAKeyPairSpec = { + params: rsaCommSpec, + sk: dIn, + pk: eIn, + algName: 'RSA', + specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC + }; + return rsaKeyPairSpec; +} + +// 生成RSA2048密钥对参数 +function genRsa2048KeyPairSpec(): cryptoFramework.RSAKeyPairSpec { + let nIn = + BigInt('0x9260d0750ae117eee55c3f3deaba74917521a262ee76007cdf8a56755ad73a1598a1408410a01434c3f5bc54a88b57fa19fc4328daea0750a4c44e88cff3b2382621b80f670464433e4336e6d003e8cd65bff211da144b88291c2259a00a72b711c116ef7686e8fee34e4d933c868187bdc26f7be071493c86f7a5941c3510806ad67b0f94d88f5cf5c02a092821d8626e8932b65c5bd8c92049c210932b7afa7ac59c0e886ae5c1edb00d8ce2c57633db26bd6639bff73cee82be9275c402b4cf2a4388da8cf8c64eefe1c5a0f5ab8057c39fa5c0589c3e253f0960332300f94bea44877b588e1edbde97cf2360727a09b775262d7ee552b3319b9266f05a25'); + let eIn = BigInt('0x010001'); + let dIn = + BigInt('0x6a7df2ca63ead4dda191d614b6b385e0d9056a3d6d5cfe07db1daabee022db08212d97613d3328e0267c9dd23d787abde2afcb306aeb7dfce69246cc73f5c87fdf06030179a2114b767db1f083ff841c025d7dc00cd82435b9a90f695369e94df23d2ce458bc3b3283ad8bba2b8fa1ba62e2dce9accff3799aae7c840016f3ba8e0048c0b6cc4339af7161003a5beb864a0164b2c1c9237b64bc87556994351b27506c33d4bcdfce0f9c491a7d6b0628c7c852be4f0a9c3132b2ed3a2c8881e9aab07e20e17deb074691be677776a78b5c502e05d9bdde72126b3738695e2dd1a0a98a14247c65d8a7ee79432a092cb0721a12df798e44f7cfce0c498147a9b1'); + return genRsaKeyPairSpec(nIn, eIn, dIn); +} + +async function rsaUseSpecDecryptOAEPPromise() { + let plan = 'This is a test'; + // 获得RSA密钥对密钥参数对象 + let rsaKeyPairSpec = genRsa2048KeyPairSpec(); + // 根据RSA密钥对参数生成RSA密钥对 + let rsaGeneratorSpec = cryptoFramework.createAsyKeyGeneratorBySpec(rsaKeyPairSpec); + let cipher = cryptoFramework.createCipher('RSA2048|PKCS1_OAEP|SHA256|MGF1_SHA1'); + let decoder = cryptoFramework.createCipher('RSA2048|PKCS1_OAEP|SHA256|MGF1_SHA1'); + // RSA加解密PKCS1-OAEP模式填充字节流P + let pSource = new Uint8Array([1, 2, 3, 4]); + let input: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(plan, 'utf-8').buffer) }; + // 生成密钥对 + let keyPair = await rsaGeneratorSpec.generateKeyPair(); + // 进行加密操作初始化 + await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null); + // get和set操作可以放在Cipher对象init之后,此处对cipher进行set和get操作 + cipher.setCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR, pSource); + let retP = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR); + // 比较get出来的P字节流与set进去的P字节流是否一致 + if (retP.toString() != pSource.toString()) { + console.error('error init pSource' + retP); + } else { + console.info('pSource changed ==' + retP); + } + // 进行OAEP其他参数的get操作 + let md = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MD_NAME_STR); + console.info('md == ' + md); + let mgf = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF_NAME_STR); + console.info('mgf == ' + mgf); + let mgf1Md = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_MD_STR); + console.info('mgf1Md == ' + mgf1Md); + let cipherDataBlob = await cipher.doFinal(input); + // get和set操作可以放在Cipher对象init之前,且与init之后等价,此处对decoder进行set和get操作 + decoder.setCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR, pSource); + retP = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR); + // 比较get出来的P字节流与set进去的P字节流是否一致 + if (retP.toString() != pSource.toString()) { + console.error('error init pSource' + retP); + } else { + console.info('pSource changed ==' + retP); + } + // 进行OAEP其他参数的get操作 + md = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MD_NAME_STR); + console.info('md == ' + md); + mgf = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF_NAME_STR); + console.info('mgf == ' + mgf); + mgf1Md = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_MD_STR); + console.info('mgf1Md == ' + mgf1Md); + // 初始化解密操作 + await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null); + let decodeData = await decoder.doFinal(cipherDataBlob); + // 解密成功 + if (decodeData.data.toString() === input.data.toString()) { + console.info('oaep decrypt success'); + } else { + console.error('oaep decrypt fail'); + } +} +// [End pss_verify_rsa_keypair_sign_async] + +@Entry +@Component +struct Index { + @State message: string = 'RSA PKCS1 OAEP Async'; + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(25) + .fontWeight(FontWeight.Bold) + Button($r('app.string.call_rsa_pkcs1_oaep_async')) + .onClick(async () => { + try { + await rsaUseSpecDecryptOAEPPromise(); + this.message = 'RSA PKCS1 OAEP Async Success'; + } catch (error) { + console.error(error); + this.message = 'RSA PKCS1 OAEP Async Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_pkcs1_oaep/RSAPKCS1OAEPSync.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_pkcs1_oaep/RSAPKCS1OAEPSync.ets new file mode 100644 index 0000000000000000000000000000000000000000..b03ec271bcde48c0068576c50515a9a41d5ecdc0 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_pkcs1_oaep/RSAPKCS1OAEPSync.ets @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start pss_verify_rsa_keypair_sign_sync] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; +// 根据密钥参数属性构造RSA非对称密钥对密钥参数 +function genRsaKeyPairSpec(nIn: bigint, eIn: bigint, dIn: bigint) { + let rsaCommSpec: cryptoFramework.RSACommonParamsSpec = { + n: nIn, + algName: 'RSA', + specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC + }; + let rsaKeyPairSpec: cryptoFramework.RSAKeyPairSpec = { + params: rsaCommSpec, + sk: dIn, + pk: eIn, + algName: 'RSA', + specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC + }; + return rsaKeyPairSpec; +} +// 生成RSA2048密钥对参数 +function genRsa2048KeyPairSpec(): cryptoFramework.RSAKeyPairSpec { + let nIn = BigInt('0x9260d0750ae117eee55c3f3deaba74917521a262ee76007cdf8a56755ad73a1598a1408410a01434c3f5bc54a88b57fa19fc4328daea0750a4c44e88cff3b2382621b80f670464433e4336e6d003e8cd65bff211da144b88291c2259a00a72b711c116ef7686e8fee34e4d933c868187bdc26f7be071493c86f7a5941c3510806ad67b0f94d88f5cf5c02a092821d8626e8932b65c5bd8c92049c210932b7afa7ac59c0e886ae5c1edb00d8ce2c57633db26bd6639bff73cee82be9275c402b4cf2a4388da8cf8c64eefe1c5a0f5ab8057c39fa5c0589c3e253f0960332300f94bea44877b588e1edbde97cf2360727a09b775262d7ee552b3319b9266f05a25'); + let eIn = BigInt('0x010001'); + let dIn = BigInt('0x6a7df2ca63ead4dda191d614b6b385e0d9056a3d6d5cfe07db1daabee022db08212d97613d3328e0267c9dd23d787abde2afcb306aeb7dfce69246cc73f5c87fdf06030179a2114b767db1f083ff841c025d7dc00cd82435b9a90f695369e94df23d2ce458bc3b3283ad8bba2b8fa1ba62e2dce9accff3799aae7c840016f3ba8e0048c0b6cc4339af7161003a5beb864a0164b2c1c9237b64bc87556994351b27506c33d4bcdfce0f9c491a7d6b0628c7c852be4f0a9c3132b2ed3a2c8881e9aab07e20e17deb074691be677776a78b5c502e05d9bdde72126b3738695e2dd1a0a98a14247c65d8a7ee79432a092cb0721a12df798e44f7cfce0c498147a9b1'); + return genRsaKeyPairSpec(nIn, eIn, dIn); +} +function main() { + let plan = 'This is a test'; + // 获得RSA密钥对密钥参数对象 + let rsaKeyPairSpec = genRsa2048KeyPairSpec(); + // 根据RSA密钥对参数生成RSA密钥对 + let rsaGeneratorSpec = cryptoFramework.createAsyKeyGeneratorBySpec(rsaKeyPairSpec); + let cipher = cryptoFramework.createCipher('RSA2048|PKCS1_OAEP|SHA256|MGF1_SHA1'); + let decoder = cryptoFramework.createCipher('RSA2048|PKCS1_OAEP|SHA256|MGF1_SHA1'); + // RSA加解密PKCS1-OAEP模式填充字节流P + let pSource = new Uint8Array([1, 2, 3, 4]); + let input: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(plan, 'utf-8').buffer) }; + // 生成密钥对 + let keyPair = rsaGeneratorSpec.generateKeyPairSync(); + // 进行加密操作初始化 + cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null); + // get和set操作可以放在Cipher对象init之后,此处对cipher进行set和get操作 + cipher.setCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR, pSource); + let retP = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR); + // 比较get出来的P字节流与set进去的P字节流是否一致 + if (retP.toString() != pSource.toString()) { + console.error('error init pSource' + retP); + } else { + console.info('pSource changed ==' + retP); + } + // 进行OAEP其他参数的get操作 + let md = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MD_NAME_STR); + console.info('md == ' + md); + let mgf = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF_NAME_STR); + console.info('mgf == ' + mgf); + let mgf1Md = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_MD_STR); + console.info('mgf1Md == ' + mgf1Md); + let cipherDataBlob = cipher.doFinalSync(input); + // get和set操作可以放在Cipher对象init之前,且与init之后等价,此处对decoder进行set和get操作 + decoder.setCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR, pSource); + retP = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR); + // 比较get出来的P字节流与set进去的P字节流是否一致 + if (retP.toString() != pSource.toString()) { + console.error('error init pSource' + retP); + } else { + console.info('pSource changed ==' + retP); + } + // 进行OAEP其他参数的get操作 + md = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MD_NAME_STR); + console.info('md == ' + md); + mgf = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF_NAME_STR); + console.info('mgf == ' + mgf); + mgf1Md = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_MD_STR); + console.info('mgf1Md == ' + mgf1Md); + // 初始化解密操作 + decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null); + let decodeData = decoder.doFinalSync(cipherDataBlob); + // 解密成功 + if (decodeData.data.toString() === input.data.toString()) { + console.info('oaep decrypt success'); + } else { + console.error('oaep decrypt fail'); + } +} +// [End pss_verify_rsa_keypair_sign_sync] + +@Entry +@Component +struct Index { + @State message: string = 'RSA PKCS1 OAEP Sync'; + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(25) + .fontWeight(FontWeight.Bold) + Button($r('app.string.call_rsa_pkcs1_oaep_sync')) + .onClick(() => { + try { + main(); + this.message = 'RSA PKCS1 OAEP Sync Success'; + } catch (error) { + console.error(error); + this.message = 'RSA PKCS1 OAEP Sync Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_segmentation/RSASegmentationAsync.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_segmentation/RSASegmentationAsync.ets new file mode 100644 index 0000000000000000000000000000000000000000..ddec066c4f399ba96e2dbf2f7f41c518fb5dc016 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_segmentation/RSASegmentationAsync.ets @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 分段加密消息 +async function rsaEncryptBySegment(pubKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('RSA1024|PKCS1'); + await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null); + let plainTextSplitLen = 64; + let cipherText = new Uint8Array(); + for (let i = 0; i < plainText.data.length; i += plainTextSplitLen) { + let updateMessage = plainText.data.subarray(i, i + plainTextSplitLen); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + // 将原文按64字符进行拆分,循环调用doFinal进行加密,使用1024bit密钥时,每次加密生成128字节长度的密文 + let updateOutput = await cipher.doFinal(updateMessageBlob); + let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length); + mergeText.set(cipherText); + mergeText.set(updateOutput.data, cipherText.length); + cipherText = mergeText; + } + let cipherBlob: cryptoFramework.DataBlob = { data: cipherText }; + return cipherBlob; +} + +// 分段解密消息 +async function rsaDecryptBySegment(priKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('RSA1024|PKCS1'); + await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, priKey, null); + let cipherTextSplitLen = 128; // RSA密钥每次加密生成的密文字节长度计算方式:密钥位数/8 + let decryptText = new Uint8Array(); + for (let i = 0; i < cipherText.data.length; i += cipherTextSplitLen) { + let updateMessage = cipherText.data.subarray(i, i + cipherTextSplitLen); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + // 将密文按128字节进行拆分解密,得到原文后进行拼接 + let updateOutput = await decoder.doFinal(updateMessageBlob); + let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length); + mergeText.set(decryptText); + mergeText.set(updateOutput.data, decryptText.length); + decryptText = mergeText; + } + let decryptBlob: cryptoFramework.DataBlob = { data: decryptText }; + return decryptBlob; +} + +async function rsaEncryptLongMessage() { + let message = 'This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!'; + let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); // 创建非对称密钥生成器对象 + let keyPair = await asyKeyGenerator.generateKeyPair(); // 随机生成RSA密钥 + let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; + let encryptText = await rsaEncryptBySegment(keyPair.pubKey, plainText); + let decryptText = await rsaDecryptBySegment(keyPair.priKey, 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'); + } +} + +@Entry +@Component +struct Index { + @State message: string = 'RSA Segmentation Async'; + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(25) + .fontWeight(FontWeight.Bold) + Button($r('app.string.call_rsa_segmentation_async')) + .onClick(async () => { + try { + await rsaEncryptLongMessage(); + this.message = 'RSA Segmentation Async Success'; + } catch (error) { + console.error(error); + this.message = 'RSA Segmentation Async Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_segmentation/RSASegmentationSync.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_segmentation/RSASegmentationSync.ets new file mode 100644 index 0000000000000000000000000000000000000000..e38c88681e459f8f726d89de79a24a780d8b60d3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/ets/pages/rsa_segmentation/RSASegmentationSync.ets @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 分段加密消息 +function rsaEncryptBySegment(pubKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('RSA1024|PKCS1'); + cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null); + let plainTextSplitLen = 64; + let cipherText = new Uint8Array(); + for (let i = 0; i < plainText.data.length; i += plainTextSplitLen) { + let updateMessage = plainText.data.subarray(i, i + plainTextSplitLen); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + // 将原文按64字符进行拆分,循环调用doFinal进行加密,使用1024bit密钥时,每次加密生成128字节长度的密文 + let updateOutput = cipher.doFinalSync(updateMessageBlob); + let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length); + mergeText.set(cipherText); + mergeText.set(updateOutput.data, cipherText.length); + cipherText = mergeText; + } + let cipherBlob: cryptoFramework.DataBlob = { data: cipherText }; + return cipherBlob; +} + +// 分段解密消息 +function rsaDecryptBySegment(priKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('RSA1024|PKCS1'); + decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, priKey, null); + let cipherTextSplitLen = 128; // RSA密钥每次加密生成的密文字节长度计算方式:密钥位数/8 + let decryptText = new Uint8Array(); + for (let i = 0; i < cipherText.data.length; i += cipherTextSplitLen) { + let updateMessage = cipherText.data.subarray(i, i + cipherTextSplitLen); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + // 将密文按128字节进行拆分解密,得到原文后进行拼接 + let updateOutput = decoder.doFinalSync(updateMessageBlob); + let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length); + mergeText.set(decryptText); + mergeText.set(updateOutput.data, decryptText.length); + decryptText = mergeText; + } + let decryptBlob: cryptoFramework.DataBlob = { data: decryptText }; + return decryptBlob; +} + +function main() { + let message = 'This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!' + + 'This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!'; + let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024'); // 创建非对称密钥生成器对象 + let keyPair = asyKeyGenerator.generateKeyPairSync(); // 随机生成RSA密钥 + let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; + let encryptText = rsaEncryptBySegment(keyPair.pubKey, plainText); + let decryptText = rsaDecryptBySegment(keyPair.priKey, 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'); + } +} + +@Entry +@Component +struct Index { + @State message: string = 'RSA Segmentation Sync'; + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(25) + .fontWeight(FontWeight.Bold) + Button($r('app.string.call_rsa_segmentation_sync')) + .onClick(() => { + try { + main(); + this.message = 'RSA Segmentation Sync Success'; + } catch (error) { + console.error(error); + this.message = 'RSA Segmentation Sync Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..817d73142eb7e8a51309dada5723633f238559b5 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/element/string.json @@ -0,0 +1,64 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "EncryptionDecryptionGuidanceRSA" + }, + { + "name": "rsa_pkcs1_async", + "value": "RSA PKCS1 Async" + }, + { + "name": "rsa_pkcs1_sync", + "value": "RSA PKCS1 Sync" + }, + { + "name": "rsa_segmentation_async", + "value": "RSA Segmentation Async" + }, + { + "name": "rsa_segmentation_sync", + "value": "RSA Segmentation Sync" + }, + { + "name": "rsa_pkcs1_oaep_async", + "value": "RSA PKCS1 OAEP Async" + }, + { + "name": "rsa_pkcs1_oaep_sync", + "value": "RSA PKCS1 OAEP Sync" + }, + { + "name": "call_rsa_pkcs1_async", + "value": "Call RSA PKCS1 Async" + }, + { + "name": "call_rsa_pkcs1_sync", + "value": "Call RSA PKCS1 Sync" + }, + { + "name": "call_rsa_pkcs1_oaep_async", + "value": "Call RSA PKCS1 OAEP Async" + }, + { + "name": "call_rsa_pkcs1_oaep_sync", + "value": "Call RSA PKCS1 OAEP Sync" + }, + { + "name": "call_rsa_segmentation_async", + "value": "Call RSA Segmentation Async" + }, + { + "name": "call_rsa_segmentation_sync", + "value": "Call RSA Segmentation Sync" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/media/background.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/media/background.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/media/foreground.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/media/foreground.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/media/layered_image.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/media/startIcon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/media/startIcon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/profile/backup_config.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/profile/main_pages.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..690f4947d8cf50e3b2e0420f442fd9fb64269869 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,11 @@ +{ + "src": [ + "pages/Index", + "pages/rsa_pkcs1_oaep/RSAPKCS1OAEPAsync", + "pages/rsa_pkcs1_oaep/RSAPKCS1OAEPSync", + "pages/rsa_pkcs1/RSAPKCS1Async", + "pages/rsa_pkcs1/RSAPKCS1Sync", + "pages/rsa_segmentation/RSASegmentationAsync", + "pages/rsa_segmentation/RSASegmentationSync" + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/dark/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/ohosTest/ets/test/Ability.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceRSA.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceRSA.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..01251e87086fa33d64a485597bbf945c62724a71 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceRSA.test.ets @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, it, expect, beforeAll } from '@ohos/hypium'; +// 导入测试依赖kit +import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; + +const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); +const bundleName = abilityDelegatorRegistry.getArguments().bundleName; +let want: Want; + +export default function encryptionDecryptionGuidanceRSATest() { + describe('encryptionDecryptionGuidanceRSATest', () => { + /** + * 打开应用 + */ + beforeAll(async () => { + want = { + bundleName: bundleName, + abilityName: 'EntryAbility' + }; + await delegator.startAbility(want); + const driver = Driver.create(); + await driver.delayMs(1000); + const ability: UIAbility = await delegator.getCurrentTopAbility(); + console.info('get top ability'); + await driver.delayMs(1000); + expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + }) + + /** + * 点击按钮,使用RSA非对称密钥(PKCS1模式)加解密,异步方法 + */ + it('testEncryptionDecryptionGuidanceRSA001', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceRSA001 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('RSA PKCS1 Async')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call RSA PKCS1 Async')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('RSA PKCS1 Async Success')); + console.info('uitest: testEncryptionDecryptionGuidanceRSA001 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用RSA非对称密钥(PKCS1模式)加解密,同步方法 + */ + it('testEncryptionDecryptionGuidanceRSA002', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceRSA002 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('RSA PKCS1 Sync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call RSA PKCS1 Sync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('RSA PKCS1 Sync Success')); + console.info('uitest: testEncryptionDecryptionGuidanceRSA002 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用RSA非对称密钥分段加解密,异步方法 + */ + it('testEncryptionDecryptionGuidanceRSA003', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceRSA003 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('RSA Segmentation Async')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call RSA Segmentation Async')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('RSA Segmentation Async Success')); + console.info('uitest: testEncryptionDecryptionGuidanceRSA003 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用RSA非对称密钥分段加解密,同步方法 + */ + it('testEncryptionDecryptionGuidanceRSA004', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceRSA004 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('RSA Segmentation Sync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call RSA Segmentation Sync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('RSA Segmentation Sync Success')); + console.info('uitest: testEncryptionDecryptionGuidanceRSA004 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用RSA非对称密钥(PKCS1_OAEP模式)加解密,异步方法 + */ + it('testEncryptionDecryptionGuidanceRSA005', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceRSA005 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('RSA PKCS1 OAEP Async')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call RSA PKCS1 OAEP Async')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('RSA PKCS1 OAEP Async Success')); + console.info('uitest: testEncryptionDecryptionGuidanceRSA005 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用RSA非对称密钥(PKCS1_OAEP模式)加解密,同步方法 + */ + it('testEncryptionDecryptionGuidanceRSA006', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceRSA006 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('RSA PKCS1 OAEP Sync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call RSA PKCS1 OAEP Sync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('RSA PKCS1 OAEP Sync Success')); + console.info('uitest: testEncryptionDecryptionGuidanceRSA006 end'); + await driver.pressBack(); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/ohosTest/ets/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..933baa6d8adce79fcdaab34a351fc7b2125a6a43 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; +import encryptionDecryptionGuidanceRSATest from './EncryptionDecryptionGuidanceRSA.test'; + +export default function testsuite() { + abilityTest(); + encryptionDecryptionGuidanceRSATest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/ohosTest/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/test/LocalUnit.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/hvigor/hvigor-config.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/ohosTest.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..3316dd685d3e854785928d322465f4204ca3d6dc --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/ohosTest.md @@ -0,0 +1,13 @@ +# 使用RSA非对称密钥加解密测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ------------------------------------------------------------ | ------------ | ---- | -------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 点击按钮,使用RSA非对称密钥(PKCS1模式)加解密,异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用RSA非对称密钥(PKCS1模式)加解密,同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用RSA非对称密钥分段加解密,异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用RSA非对称密钥分段加解密,同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用RSA非对称密钥(PKCS1_OAEP模式)加解密,异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用RSA非对称密钥(PKCS1_OAEP模式)加解密,同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/screenshots/EncryptionDecryptionGuidanceRSA1.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/screenshots/EncryptionDecryptionGuidanceRSA1.png new file mode 100644 index 0000000000000000000000000000000000000000..811bc9aca0468eae08b80cb05980ed4b22b875ae Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/screenshots/EncryptionDecryptionGuidanceRSA1.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/screenshots/EncryptionDecryptionGuidanceRSA2.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/screenshots/EncryptionDecryptionGuidanceRSA2.png new file mode 100644 index 0000000000000000000000000000000000000000..c5a8c1497e68120eaf3521f0816fd43f9df9f334 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceRSA/screenshots/EncryptionDecryptionGuidanceRSA2.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/AppScope/app.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d2e812a42e3891bec1dbc7fec55010a7da696c95 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.encryptiondecryptionguidancesm2", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/AppScope/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..d6c14f7e0d796be9b3b253366e2948661e841ac4 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "EncryptionDecryptionGuidanceSM2" + } + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/AppScope/resources/base/media/app_icon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/AppScope/resources/base/media/app_icon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/README.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f434398adec18b162e949939cdf55064190de9c3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/README.md @@ -0,0 +1,75 @@ +# 使用SM2非对称密钥加解密及密文格式转换 + +### 介绍 + +本示例主要展示了使用SM2非对称密钥加解密及密文格式转换场景 。该工程中展示的代码详细描述可查如下链接。 + +- [使用SM2非对称密钥加解密](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-sm2-asym-encrypt-decrypt.md) +- [使用SM2密文格式转换](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-sm2-ciphertext-conversion.md) + +### 效果预览 + +| 首页效果图 | 执行结果图 | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| | | + +### 使用说明 + +1. 运行Index主界面。 +2. 页面呈现上述执行结果图效果,点击不同按钮可以跳转到不同功能页面,点击跳转页面中按钮可以执行对应操作,并更新文本内容。 +3. 运行测试用例EncryptionDecryptionGuidanceSM2.test.ets文件对页面代码进行测试可以全部通过。 + +### 工程目录 + +``` +entry/src/ + ├── main + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // 使用SM2非对称密钥加解密及密文格式转换示例代码 + │ │ ├── sm2 + │ │ | ├── CreateASN.1Ciphertext.ets + │ │ | ├── ObtainCiphertext.ets + │ │ | ├── SM2EncryptionDecryptionAsync.ets + │ │ | ├── SM2EncryptionDecryptionSync.ets + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ └── test + │ │ ├── Ability.test.ets + │ │ ├── EncryptionDecryptionGuidanceSM2.test.ets // 自动化测试代码 + │ │ └── List.test.ets +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS 5.0.2 Release及以上。 + +3. DevEco Studio版本:DevEco Studio 5.0.2 Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS 5.0.2 Release及以上。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +```` +git init +git config core.sparsecheckout true +echo CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2 > .git/info/sparse-checkout +git remote add origin https://gitee.com/harmonyos_samples/guide-snippets.git +git pull origin master +```` \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fb5a1ab654ae6715a0ab69a31900ad26dcf2487f --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/build-profile.json5 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.2(14)", + "targetSdkVersion": "5.0.2(14)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/code-linter.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e7569e3056e27af38e9991b7ea73ec10f3ba8a05 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/build-profile.json5 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c9cb6c8174858277c9b0d465a51547dcab16d5ff --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/entryability/EntryAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..edc2839f203ba057c186e19b0cbbbf80c8faa8b3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/Index.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..e3d992fa1409ea335fd585e6858fea2279b98cfd --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { router } from '@kit.ArkUI'; + +@Entry +@Component +struct Index { + @State message: string = 'EncryptionDecryptionGuidanceSM2'; + + goToSample(url: string): void { + router.pushUrl({ + url: url, + }, router.RouterMode.Single, (err) => { + if (err) { + console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); + return; + } + console.info('pushUrl success'); + }) + } + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(25) + .fontWeight(FontWeight.Bold) + Button($r('app.string.sm2_async')) + .width('70%') + .onClick(() => { + this.goToSample('pages/sm2/SM2EncryptionDecryptionAsync') + }) + Button($r('app.string.sm2_sync')) + .width('70%') + .onClick(() => { + this.goToSample('pages/sm2/SM2EncryptionDecryptionSync') + }) + Button($r('app.string.create_asn1')) + .width('70%') + .onClick(() => { + this.goToSample('pages/sm2/CreateASN.1Ciphertext') + }) + Button($r('app.string.obtain_ciphertext')) + .width('70%') + .onClick(() => { + this.goToSample('pages/sm2/ObtainCiphertext') + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/sm2/CreateASN.1Ciphertext.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/sm2/CreateASN.1Ciphertext.ets new file mode 100644 index 0000000000000000000000000000000000000000..fee1793d98a3161c815909c50161a9db042ec1d5 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/sm2/CreateASN.1Ciphertext.ets @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; + +function testGenCipherTextBySpec() { + let spec: cryptoFramework.SM2CipherTextSpec = { + xCoordinate: BigInt('20625015362595980457695435345498579729138244358573902431560627260141789922999'), + yCoordinate: BigInt('48563164792857017065725892921053777369510340820930241057309844352421738767712'), + cipherTextData: new Uint8Array([100, 227, 78, 195, 249, 179, 43, 70, 242, 69, 169, 10, 65, 123]), + hashData: new Uint8Array([87, 167, 167, 247, 88, 146, 203, 234, 83, 126, 117, 129, 52, 142, 82, 54, 152, 226, 201, + 111, 143, 115, 169, 125, 128, 42, 157, 31, 114, 198, 109, 244]), + } + // 此处的data可直接使用cryptoFramework进行SM2解密 + let data = cryptoFramework.SM2CryptoUtil.genCipherTextBySpec(spec, 'C1C3C2'); + console.info('genCipherTextBySpec success'); +} + +@Entry +@Component +struct Index { + @State message: string = 'Create ASN.1'; + + build() { + Column({ space: 5 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_create_asn1')) + .width('70%') + .onClick(() => { + try { + testGenCipherTextBySpec(); + this.message = 'Create ASN.1 Success'; + } catch { + this.message = 'Create ASN.1 Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/sm2/ObtainCiphertext.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/sm2/ObtainCiphertext.ets new file mode 100644 index 0000000000000000000000000000000000000000..b8a3c20316412877ad8a5d002fee8ec4cb646195 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/sm2/ObtainCiphertext.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; + +function testGetCipherTextSpec() { + let cipherTextArray = + new Uint8Array([48, 118, 2, 32, 45, 153, 88, 82, 104, 221, 226, 43, 174, 21, 122, 248, 5, 232, 105, 41, 92, 95, 102, + 224, 216, 149, 85, 236, 110, 6, 64, 188, 149, 70, 70, 183, 2, 32, 107, 93, 198, 247, 119, 18, 40, 110, 90, 156, + 193, 158, 205, 113, 170, 128, 146, 109, 75, 17, 181, 109, 110, 91, 149, 5, 110, 233, 209, 78, 229, 96, 4, 32, 87, + 167, 167, 247, 88, 146, 203, 234, 83, 126, 117, 129, 52, 142, 82, 54, 152, 226, 201, 111, 143, 115, 169, 125, 128, + 42, 157, 31, 114, 198, 109, 244, 4, 14, 100, 227, 78, 195, 249, 179, 43, 70, 242, 69, 169, 10, 65, 123]); + let cipherText: cryptoFramework.DataBlob = { data: cipherTextArray }; + let spec: cryptoFramework.SM2CipherTextSpec = cryptoFramework.SM2CryptoUtil.getCipherTextSpec(cipherText, 'C1C3C2'); + console.info('getCipherTextSpec success'); +} + +@Entry +@Component +struct Index { + @State message: string = 'Obtain Ciphertext'; + + build() { + Column({ space: 5 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_obtain_ciphertext')) + .width('70%') + .onClick(() => { + try { + testGetCipherTextSpec(); + this.message = 'Obtain Ciphertext Success'; + } catch { + this.message = 'Obtain Ciphertext Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/sm2/SM2EncryptionDecryptionAsync.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/sm2/SM2EncryptionDecryptionAsync.ets new file mode 100644 index 0000000000000000000000000000000000000000..9f27af389240649027a9edbf4b32ad198d1a98a3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/sm2/SM2EncryptionDecryptionAsync.ets @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 加密消息 +async function encryptMessagePromise(publicKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) { + 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; +} + +async function main() { + let pkData = + new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 129, 28, 207, 85, 1, 130, 45, 3, 66, 0, + 4, 90, 3, 58, 157, 190, 248, 76, 7, 132, 200, 151, 208, 112, 230, 96, 140, 90, 238, 211, 155, 128, 109, 248, 40, + 83, 214, 78, 42, 104, 106, 55, 148, 249, 35, 61, 32, 221, 135, 143, 100, 45, 97, 194, 176, 52, 73, 136, 174, 40, + 70, 70, 34, 103, 103, 161, 99, 27, 187, 13, 187, 109, 244, 13, 7]); + let skData = + new Uint8Array([48, 49, 2, 1, 1, 4, 32, 54, 41, 239, 240, 63, 188, 134, 113, 31, 102, 149, 203, 245, 89, 15, 15, 47, + 202, 170, 60, 38, 154, 28, 169, 189, 100, 251, 76, 112, 223, 156, 159, 160, 10, 6, 8, 42, 129, 28, 207, 85, 1, + 130, 45]); + let keyPair = await genKeyPairByData(pkData, skData); + 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'); + } +} + +@Entry +@Component +struct Index { + @State message: string = 'SM2 Async'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_sm2_async')) + .width('70%') + .onClick(async () => { + try { + await main(); + this.message = 'SM2 Async Success'; + } catch { + this.message = 'SM2 Async Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/sm2/SM2EncryptionDecryptionSync.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/sm2/SM2EncryptionDecryptionSync.ets new file mode 100644 index 0000000000000000000000000000000000000000..48d1673144ab8bae6352bbf782ee530df3da364e --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/ets/pages/sm2/SM2EncryptionDecryptionSync.ets @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 加密消息 +function encryptMessage(publicKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('SM2_256|SM3'); + cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, publicKey, null); + let encryptData = cipher.doFinalSync(plainText); + return encryptData; +} + +// 解密消息 +function decryptMessage(privateKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('SM2_256|SM3'); + decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, privateKey, null); + let decryptData = decoder.doFinalSync(cipherText); + return decryptData; +} + +// 生成SM2密钥对 +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 = sm2Generator.convertKeySync(pubKeyBlob, priKeyBlob); + console.info('convertKeySync success'); + return keyPair; +} + +function main() { + let pkData = + new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 129, 28, 207, 85, 1, 130, 45, 3, 66, 0, + 4, 90, 3, 58, 157, 190, 248, 76, 7, 132, 200, 151, 208, 112, 230, 96, 140, 90, 238, 211, 155, 128, 109, 248, 40, + 83, 214, 78, 42, 104, 106, 55, 148, 249, 35, 61, 32, 221, 135, 143, 100, 45, 97, 194, 176, 52, 73, 136, 174, 40, + 70, 70, 34, 103, 103, 161, 99, 27, 187, 13, 187, 109, 244, 13, 7]); + let skData = + new Uint8Array([48, 49, 2, 1, 1, 4, 32, 54, 41, 239, 240, 63, 188, 134, 113, 31, 102, 149, 203, 245, 89, 15, 15, 47, + 202, 170, 60, 38, 154, 28, 169, 189, 100, 251, 76, 112, 223, 156, 159, 160, 10, 6, 8, 42, 129, 28, 207, 85, 1, + 130, 45]); + let keyPair = genKeyPairByData(pkData, skData); + 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 = encryptMessage(pubKey, plainText); + let decryptText = decryptMessage(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'); + } +} + +@Entry +@Component +struct Index { + @State message: string = 'SM2 Sync'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_sm2_sync')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = 'SM2 Sync Success'; + } catch { + this.message = 'SM2 Sync Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..5a6054af4b7c31a57d22023b701b379d4626c1c5 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/element/string.json @@ -0,0 +1,48 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "EncryptionDecryptionGuidanceSM2" + }, + { + "name": "sm2_async", + "value": "SM2 Async" + }, + { + "name": "sm2_sync", + "value": "SM2 Sync" + }, + { + "name": "create_asn1", + "value": "Create ASN.1" + }, + { + "name": "obtain_ciphertext", + "value": "Obtain Ciphertext" + }, + { + "name": "call_create_asn1", + "value": "Call Create ASN.1" + }, + { + "name": "call_obtain_ciphertext", + "value": "Call Obtain Ciphertext" + }, + { + "name": "call_sm2_async", + "value": "Call SM2 Async" + }, + { + "name": "call_sm2_sync", + "value": "Call SM2 Sync" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/media/background.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/media/background.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/media/foreground.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/media/foreground.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/media/layered_image.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/media/startIcon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/media/startIcon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/profile/backup_config.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/profile/main_pages.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..dc7a892b7d8bb461503196986213e048cbc23b71 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,9 @@ +{ + "src": [ + "pages/Index", + "pages/sm2/CreateASN.1Ciphertext", + "pages/sm2/ObtainCiphertext", + "pages/sm2/SM2EncryptionDecryptionAsync", + "pages/sm2/SM2EncryptionDecryptionSync" + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/dark/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/ohosTest/ets/test/Ability.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceSM2.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceSM2.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..beab5f33d065030d2797dc57c92d1031c30a2eea --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceSM2.test.ets @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, it, expect, beforeAll } from '@ohos/hypium'; +// 导入测试依赖kit +import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; + +const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); +const bundleName = abilityDelegatorRegistry.getArguments().bundleName; +let want: Want; + +export default function encryptionDecryptionGuidanceSM2Test() { + describe('encryptionDecryptionGuidanceSM2Test', () => { + /** + * 打开应用 + */ + beforeAll(async () => { + want = { + bundleName: bundleName, + abilityName: 'EntryAbility' + }; + await delegator.startAbility(want); + const driver = Driver.create(); + await driver.delayMs(1000); + const ability: UIAbility = await delegator.getCurrentTopAbility(); + console.info('get top ability'); + await driver.delayMs(1000); + expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + }) + + /** + * 点击按钮,使用SM2非对称密钥加解密,异步方法 + */ + it('testEncryptionDecryptionGuidanceSM2001', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceSM2001 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('SM2 Async')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SM2 Async')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM2 Async Success')); + console.info('uitest: testEncryptionDecryptionGuidanceSM2001 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用SM2非对称密钥加解密,同步方法 + */ + it('testEncryptionDecryptionGuidanceSM2002', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceSM2002 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('SM2 Sync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SM2 Sync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM2 Sync Success')); + console.info('uitest: testEncryptionDecryptionGuidanceSM2002 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用SM2密文格式转换,指定密文参数,生成标准ASN.1密文 + */ + it('testEncryptionDecryptionGuidanceSM2003', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceSM2003 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Create ASN.1')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call Create ASN.1')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Create ASN.1 Success')); + console.info('uitest: testEncryptionDecryptionGuidanceSM2003 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用SM2密文格式转换,从标准ASN.1密文中,获取密文参数 + */ + it('testEncryptionDecryptionGuidanceSM2004', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceSM2004 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Obtain Ciphertext')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call Obtain Ciphertext')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Obtain Ciphertext Success')); + console.info('uitest: testEncryptionDecryptionGuidanceSM2004 end'); + await driver.pressBack(); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/ohosTest/ets/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..83c4d6686dc8f9d18309c88e766c9c4c7b4fdaa2 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; +import encryptionDecryptionGuidanceSM2Test from './EncryptionDecryptionGuidanceSM2.test'; + +export default function testsuite() { + abilityTest(); + encryptionDecryptionGuidanceSM2Test(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/ohosTest/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/test/LocalUnit.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/hvigor/hvigor-config.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/ohosTest.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..517166af842a101b0c94c32c7fe1dfae9ebd830b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/ohosTest.md @@ -0,0 +1,11 @@ +# 使用SM2非对称密钥加解密及密文格式转换测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ------------------------------------------------------------ | ------------ | ---- | -------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 点击按钮,使用SM2非对称密钥加解密,异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM2非对称密钥加解密,同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM2密文格式转换,指定密文参数,生成标准ASN.1密文 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM2密文格式转换,从标准ASN.1密文中,获取密文参数 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/screenshots/EncryptionDecryptionGuidanceSM21.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/screenshots/EncryptionDecryptionGuidanceSM21.png new file mode 100644 index 0000000000000000000000000000000000000000..b308e5b3f846007d12c8dfff31f2e077e64a47b7 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/screenshots/EncryptionDecryptionGuidanceSM21.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/screenshots/EncryptionDecryptionGuidanceSM22.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/screenshots/EncryptionDecryptionGuidanceSM22.png new file mode 100644 index 0000000000000000000000000000000000000000..224b531d5a5d9127bc49ef9d2010bf7103e23945 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM2/screenshots/EncryptionDecryptionGuidanceSM22.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/AppScope/app.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..63f431a6833fe90b98ddce43928a78a07c4709c0 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.encryptiondecryptionguidancesm4", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/AppScope/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..14ef92ce2be9c9c9125bbba64b06dcd9bb6f6893 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "EncryptionDecryptionGuidanceSM4" + } + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/AppScope/resources/base/media/app_icon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/AppScope/resources/base/media/app_icon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/README.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/README.md new file mode 100644 index 0000000000000000000000000000000000000000..ef0628e189d1747c66c3ae6c9349e76d20230118 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/README.md @@ -0,0 +1,85 @@ +# 使用SM4对称密钥加解密(C/C++) + +### 介绍 + +本示例主要展示了使用SM4对称密钥(ECB模式)加解密(C/C++)、使用SM4对称密钥(CBC模式)加解密(C/C++)、使用SM4对称密钥(GCM模式)加解密(C/C++)、使用SM4对称密钥(GCM模式)分段加解密(C/C++)场景 。该工程中展示的代码详细描述可查如下链接。 + +- [使用SM4对称密钥(ECB模式)加解密(C/C++)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-sm4-sym-encrypt-decrypt-ecb-ndk.md) +- [使用SM4对称密钥(CBC模式)加解密(C/C++)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-sm4-sym-encrypt-decrypt-cbc-ndk.md) +- [使用SM4对称密钥(GCM模式)加解密(C/C++)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-sm4-sym-encrypt-decrypt-gcm-ndk.md) +- [使用SM4对称密钥(GCM模式)分段加解密(C/C++)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-sm4-sym-encrypt-decrypt-gcm-by-segment-ndk.md) + +### 效果预览 + +| 首页效果图 | 执行结果图 | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| | | + +### 使用说明 + +1. 运行Index主界面。 +2. 页面呈现上述执行结果图效果,点击不同按钮可以跳转到不同功能页面,点击跳转页面中按钮可以执行对应操作,并更新文本内容。 +3. 运行测试用例EncryptionDecryptionGuidanceSM4.test.ets文件对页面代码进行测试可以全部通过。 + +### 工程目录 + +``` +entry/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ | ├── libentry + │ │ | ├── index.d.ts + │ │ | ├── oh-package.json5 + │ │ | ├── project + │ │ | ├── sm4_cbc_encryption_decryption.cpp + │ │ | ├── sm4_ecb_encryption_decryption.cpp + │ │ | ├── sm4_gcm_encryption_decryption.cpp + │ │ | ├── sm4_gcm_seg_encryption_decryption.cpp + │ │ | ├── file.h + │ │ ├── CMakeList.txt + │ │ ├── napi_init.cpp + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // 使用SM4对称密钥加解密(C/C++)示例代码 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ └── test + │ │ ├── Ability.test.ets + │ │ ├── EncryptionDecryptionGuidanceSM4.test.ets // 自动化测试代码 + │ │ └── List.test.ets +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS 5.0.2 Release及以上。 + +3. DevEco Studio版本:DevEco Studio 5.0.2 Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS 5.0.2 Release及以上。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +```` +git init +git config core.sparsecheckout true +echo CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4 > .git/info/sparse-checkout +git remote add origin https://gitee.com/harmonyos_samples/guide-snippets.git +git pull origin master +```` \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fb5a1ab654ae6715a0ab69a31900ad26dcf2487f --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/build-profile.json5 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.2(14)", + "targetSdkVersion": "5.0.2(14)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/code-linter.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..31862257476bc8403e59fa42b4c924974d7dbe03 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/build-profile.json5 @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + "abiFilters": [ + "arm64-v8a", + "x86_64", + "armeabi-v7a" + ] + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..118bdd4fe7699368a010e04c24f5bfc887cf1298 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libentry.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/CMakeLists.txt b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..908bd14781eac02c94b3a8f090eb547db9225240 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/CMakeLists.txt @@ -0,0 +1,15 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(EncryptionDecryptionGuidanceSM4) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(entry SHARED napi_init.cpp types/project/sm4_ecb_encryption_decryption.cpp types/project/sm4_cbc_encryption_decryption.cpp types/project/sm4_gcm_encryption_decryption.cpp types/project/sm4_gcm_seg_encryption_decryption.cpp) +target_link_libraries(entry PUBLIC libace_napi.z.so libohcrypto.so) \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/napi_init.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0baaf52375e2a0c148ab18e66124bdfd4650b80e --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/napi_init.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "types/project/file.h" + +static napi_value NAPI_Global_testSm4Ecb(napi_env env, napi_callback_info info) +{ + napi_value ret; + napi_create_int32(env, doTestSm4Ecb(), &ret); + return ret; +} + +static napi_value NAPI_Global_testSm4Cbc(napi_env env, napi_callback_info info) +{ + napi_value ret; + napi_create_int32(env, doTestSm4Cbc(), &ret); + return ret; +} + +static napi_value NAPI_Global_testSm4Gcm(napi_env env, napi_callback_info info) +{ + napi_value ret; + napi_create_int32(env, doTestSm4Gcm(), &ret); + return ret; +} + +static napi_value NAPI_Global_testSm4GcmSeg(napi_env env, napi_callback_info info) +{ + napi_value ret; + napi_create_int32(env, doTestSm4GcmSeg(), &ret); + return ret; +} + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + {"testSm4Ecb", nullptr, NAPI_Global_testSm4Ecb, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testSm4Cbc", nullptr, NAPI_Global_testSm4Cbc, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testSm4Gcm", nullptr, NAPI_Global_testSm4Gcm, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testSm4GcmSeg", nullptr, NAPI_Global_testSm4GcmSeg, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "entry", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/libentry/Index.d.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..1286917646204ae04f6dd41ff47c51f525a30037 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const testSm4Ecb: () => number; + +export const testSm4Cbc: () => number; + +export const testSm4Gcm: () => number; + +export const testSm4GcmSeg: () => number; diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/libentry/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6abf3b7c20f22c62aaac6a995a25cae672f73f35 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libentry.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/file.h b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/file.h new file mode 100644 index 0000000000000000000000000000000000000000..2e8def9185a35b9741ae36bda80469796a86fcbf --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/file.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "CryptoArchitectureKit/crypto_common.h" + +OH_Crypto_ErrCode doTestSm4Ecb(); +OH_Crypto_ErrCode doTestSm4Cbc(); +OH_Crypto_ErrCode doTestSm4Gcm(); +OH_Crypto_ErrCode doTestSm4GcmSeg(); \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/sm4_cbc_encryption_decryption.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/sm4_cbc_encryption_decryption.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dcd27349a16efc18221e52476d5d19aea055c0d0 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/sm4_cbc_encryption_decryption.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start crypt_decrypt_sm4_cbc] +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_sym_cipher.h" +#include +// [StartExclude crypt_decrypt_sm4_cbc] +#include "file.h" +// [EndExclude crypt_decrypt_sm4_cbc] + +OH_Crypto_ErrCode doTestSm4Cbc() +{ + OH_CryptoSymKeyGenerator *genCtx = nullptr; + OH_CryptoSymCipher *encCtx = nullptr; + OH_CryptoSymCipher *decCtx = nullptr; + OH_CryptoSymKey *keyCtx = nullptr; + OH_CryptoSymCipherParams *params = nullptr; + Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; + Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; + + char *plainText = const_cast("this is test!"); + Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; + uint8_t iv[16] = {1, 2, 4, 12, 3, 4, 2, 3, 3, 2, 0, 4, 3, 1, 0, 10}; // iv使用安全随机数生成 + Crypto_DataBlob ivBlob = {.data = iv, .len = sizeof(iv)}; + // 生成对称密钥 + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("SM4_128", &genCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 设置参数 + ret = OH_CryptoSymCipherParams_Create(¶ms); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivBlob); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 加密 + ret = OH_CryptoSymCipher_Create("SM4_128|CBC|PKCS7", &encCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(encCtx, &msgBlob, &outUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 解密 + ret = OH_CryptoSymCipher_Create("SM4_128|CBC|PKCS7", &decCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 资源释放 +end: + OH_CryptoSymCipherParams_Destroy(params); + OH_CryptoSymCipher_Destroy(encCtx); + OH_CryptoSymCipher_Destroy(decCtx); + OH_CryptoSymKeyGenerator_Destroy(genCtx); + OH_CryptoSymKey_Destroy(keyCtx); + OH_Crypto_FreeDataBlob(&outUpdate); + OH_Crypto_FreeDataBlob(&decUpdate); + return ret; +} +// [End crypt_decrypt_sm4_cbc] diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/sm4_ecb_encryption_decryption.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/sm4_ecb_encryption_decryption.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4aec3a3200811be0aba5a97451f091dc8e0bdf98 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/sm4_ecb_encryption_decryption.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start crypt_decrypt_sm4_ecb] +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_sym_cipher.h" +#include +// [StartExclude crypt_decrypt_sm4_ecb] +#include "file.h" +// [EndExclude crypt_decrypt_sm4_ecb] + +OH_Crypto_ErrCode doTestSm4Ecb() +{ + OH_CryptoSymKeyGenerator *genCtx = nullptr; + OH_CryptoSymCipher *encCtx = nullptr; + OH_CryptoSymCipher *decCtx = nullptr; + OH_CryptoSymKey *keyCtx = nullptr; + OH_CryptoSymCipherParams *params = nullptr; + char *plainText = const_cast("this is test!"); + Crypto_DataBlob input = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; + Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; + Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; + + // 随机生成对称密钥 + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("SM4_128", &genCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + // 创建参数 + ret = OH_CryptoSymCipherParams_Create(¶ms); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 加密操作 + ret = OH_CryptoSymCipher_Create("SM4_128|ECB|PKCS7", &encCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(encCtx, &input, &outUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 解密操作 + ret = OH_CryptoSymCipher_Create("SM4_128|ECB|PKCS7", &decCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + // 释放资源 +end: + OH_CryptoSymCipherParams_Destroy(params); + OH_CryptoSymCipher_Destroy(encCtx); + OH_CryptoSymCipher_Destroy(decCtx); + OH_CryptoSymKeyGenerator_Destroy(genCtx); + OH_CryptoSymKey_Destroy(keyCtx); + OH_Crypto_FreeDataBlob(&outUpdate); + OH_Crypto_FreeDataBlob(&decUpdate); + return ret; +} +// [End crypt_decrypt_sm4_ecb] diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/sm4_gcm_encryption_decryption.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/sm4_gcm_encryption_decryption.cpp new file mode 100644 index 0000000000000000000000000000000000000000..875841d78c2b4ba61f0f95e48d3fae15f5ae8617 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/sm4_gcm_encryption_decryption.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start crypt_decrypt_sm4_gcm] +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_sym_cipher.h" +#include +// [StartExclude crypt_decrypt_sm4_gcm] +#include "file.h" +// [EndExclude crypt_decrypt_sm4_gcm] + +OH_Crypto_ErrCode doTestSm4Gcm() +{ + OH_CryptoSymKeyGenerator *genCtx = nullptr; + OH_CryptoSymCipher *encCtx = nullptr; + OH_CryptoSymCipher *decCtx = nullptr; + OH_CryptoSymKey *keyCtx = nullptr; + OH_CryptoSymCipherParams *params = nullptr; + + Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; + Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; + + uint8_t aad[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + uint8_t tag[16] = {0}; + uint8_t iv[12] = {1, 2, 4, 12, 3, 4, 2, 3, 3, 2, 0, 4}; // iv使用安全随机数生成 + Crypto_DataBlob ivData = {.data = iv, .len = sizeof(iv)}; + Crypto_DataBlob aadData = {.data = aad, .len = sizeof(aad)}; + Crypto_DataBlob tagData = {.data = tag, .len = sizeof(tag)}; + Crypto_DataBlob tagOutPut = {.data = nullptr, .len = 0}; + char *plainText = const_cast("this is test!"); + Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; + // 生成对称密钥 + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("SM4_128", &genCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 设置参数 + ret = OH_CryptoSymCipherParams_Create(¶ms); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivData); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_AAD_DATABLOB, &aadData); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tagData); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 加密 + ret = OH_CryptoSymCipher_Create("SM4_128|GCM|PKCS7", &encCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Update(encCtx, &msgBlob, &outUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(encCtx, nullptr, &tagOutPut); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 解密 + ret = OH_CryptoSymCipher_Create("SM4_128|GCM|PKCS7", &decCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tagOutPut); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(decCtx, &outUpdate, &decUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 释放资源 +end: + OH_CryptoSymCipherParams_Destroy(params); + OH_CryptoSymCipher_Destroy(encCtx); + OH_CryptoSymCipher_Destroy(decCtx); + OH_CryptoSymKeyGenerator_Destroy(genCtx); + OH_CryptoSymKey_Destroy(keyCtx); + OH_Crypto_FreeDataBlob(&outUpdate); + OH_Crypto_FreeDataBlob(&decUpdate); + OH_Crypto_FreeDataBlob(&tagOutPut); + return ret; +} +// [End crypt_decrypt_sm4_gcm] diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/sm4_gcm_seg_encryption_decryption.cpp b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/sm4_gcm_seg_encryption_decryption.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f4952621bbf45feb668c28d6ce4de62d77f5bf9 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/cpp/types/project/sm4_gcm_seg_encryption_decryption.cpp @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start crypt_decrypt_sm4_gcm_seg] +#include +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_sym_cipher.h" + +#define OH_CRYPTO_GCM_TAG_LEN 16 +#define OH_CRYPTO_MAX_TEST_DATA_LEN 128 + +OH_Crypto_ErrCode doTestSm4GcmSeg() +{ + OH_CryptoSymKeyGenerator *genCtx = nullptr; + OH_CryptoSymCipher *encCtx = nullptr; + OH_CryptoSymCipher *decCtx = nullptr; + OH_CryptoSymKey *keyCtx = nullptr; + OH_CryptoSymCipherParams *params = nullptr; + + char *plainText = const_cast("aaaaa.....bbbbb.....ccccc.....ddddd.....eee"); + Crypto_DataBlob msgBlob = {.data = (uint8_t *)(plainText), .len = strlen(plainText)}; + uint8_t aad[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + uint8_t tagArr[16] = {0}; + uint8_t iv[12] = {1, 2, 4, 12, 3, 4, 2, 3, 3, 2, 0, 4}; // iv使用安全随机数生成 + Crypto_DataBlob tag = {.data = nullptr, .len = 0}; + Crypto_DataBlob ivBlob = {.data = iv, .len = sizeof(iv)}; + Crypto_DataBlob aadBlob = {.data = aad, .len = sizeof(aad)}; + Crypto_DataBlob outUpdate = {.data = nullptr, .len = 0}; + Crypto_DataBlob decUpdate = {.data = nullptr, .len = 0}; + Crypto_DataBlob tagInit = {.data = tagArr, .len = sizeof(tagArr)}; + int32_t cipherLen = 0; + int blockSize = 20; + int32_t randomLen = strlen(plainText); + int cnt = randomLen / blockSize; + int rem = randomLen % blockSize; + uint8_t cipherText[OH_CRYPTO_MAX_TEST_DATA_LEN] = {0}; + Crypto_DataBlob cipherBlob; + + // 生成密钥 + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("SM4_128", &genCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 设置参数 + ret = OH_CryptoSymCipherParams_Create(¶ms); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivBlob); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_AAD_DATABLOB, &aadBlob); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tagInit); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 加密 + ret = OH_CryptoSymCipher_Create("SM4_128|GCM|PKCS7", &encCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(encCtx, CRYPTO_ENCRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + for (int i = 0; i < cnt; i++) { + msgBlob.len = blockSize; + ret = OH_CryptoSymCipher_Update(encCtx, &msgBlob, &outUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + msgBlob.data += blockSize; + memcpy(&cipherText[cipherLen], outUpdate.data, outUpdate.len); + cipherLen += outUpdate.len; + } + if (rem > 0) { + msgBlob.len = rem; + ret = OH_CryptoSymCipher_Update(encCtx, (Crypto_DataBlob *)&msgBlob, &outUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + memcpy(&cipherText[cipherLen], outUpdate.data, outUpdate.len); + cipherLen += outUpdate.len; + } + ret = OH_CryptoSymCipher_Final(encCtx, nullptr, &tag); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + + // 解密 + cipherBlob = {.data = reinterpret_cast(cipherText), .len = (size_t)cipherLen}; + msgBlob.data -= strlen(plainText) - rem; + msgBlob.len = strlen(plainText); + ret = OH_CryptoSymCipher_Create("SM4_128|GCM|PKCS7", &decCtx); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tag); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Init(decCtx, CRYPTO_DECRYPT_MODE, keyCtx, params); + if (ret != CRYPTO_SUCCESS) { + goto end; + } + ret = OH_CryptoSymCipher_Final(decCtx, &cipherBlob, &decUpdate); + if (ret != CRYPTO_SUCCESS) { + goto end; + } +end: + OH_CryptoSymCipherParams_Destroy(params); + OH_CryptoSymCipher_Destroy(encCtx); + OH_CryptoSymCipher_Destroy(decCtx); + OH_CryptoSymKeyGenerator_Destroy(genCtx); + OH_CryptoSymKey_Destroy(keyCtx); + OH_Crypto_FreeDataBlob(&outUpdate); + OH_Crypto_FreeDataBlob(&tag); + OH_Crypto_FreeDataBlob(&decUpdate); + return ret; +} +// [End crypt_decrypt_sm4_gcm_seg] diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/ets/entryability/EntryAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..e138da64e6583f1819cf5dd317e3e58634c3fb0b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', err.message ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/ets/pages/Index.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..25fe92595bb59fc8066ec710ad6732b4adcf09b7 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import testNapi from 'libentry.so'; + +@Entry +@Component +struct Index { + @State message: string = 'EncryptionDecryptionGuidanceSM4'; + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(24) + .fontWeight(FontWeight.Bold) + Button($r('app.string.call_sm4_ecb')) + .width('70%') + .onClick(() => { + let ret: number = testNapi.testSm4Ecb(); + this.message = 'SM4_ECBSuccess Result:' + ret.toString(); + }) + Button($r('app.string.call_sm4_cbc')) + .width('70%') + .onClick(() => { + let ret: number = testNapi.testSm4Cbc(); + this.message = 'SM4_CBCSuccess Result:' + ret.toString(); + }) + Button($r('app.string.call_sm4_gcm')) + .width('70%') + .onClick(() => { + let ret: number = testNapi.testSm4Gcm(); + this.message = 'SM4_GCMSuccess Result:' + ret.toString(); + }) + Button($r('app.string.call_sm4_gcm_seg')) + .width('70%') + .onClick(() => { + let ret: number = testNapi.testSm4GcmSeg(); + this.message = 'SM4_GCM_SEGSuccess Result:' + ret.toString(); + }) + } + .width('100%') + .height('100%') + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1a09b9b38fbddd490719685b64f124d11caca7d2 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/element/string.json @@ -0,0 +1,32 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "EncryptionDecryptionGuidanceSM4" + }, + { + "name": "call_sm4_ecb", + "value": "Call SM4_ECB" + }, + { + "name": "call_sm4_cbc", + "value": "Call SM4_CBC" + }, + { + "name": "call_sm4_gcm", + "value": "Call SM4_GCM" + }, + { + "name": "call_sm4_gcm_seg", + "value": "Call SM4_GCM_SEG" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/media/background.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/media/background.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/media/foreground.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/media/foreground.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/media/layered_image.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/media/startIcon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/media/startIcon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/profile/backup_config.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/profile/main_pages.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/dark/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/ohosTest/ets/test/Ability.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceSM4.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceSM4.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..9edb09d835998bd94c7e6ce1688512f48b4019ee --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidanceSM4.test.ets @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, it, expect, beforeAll } from '@ohos/hypium'; +// 导入测试依赖kit +import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; + +const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); +const bundleName = abilityDelegatorRegistry.getArguments().bundleName; +let want: Want; + +export default function encryptionDecryptionGuidanceSM4Test() { + describe('encryptionDecryptionGuidanceSM4Test', () => { + /** + * 打开应用 + */ + beforeAll(async () => { + want = { + bundleName: bundleName, + abilityName: 'EntryAbility' + }; + await delegator.startAbility(want); + const driver = Driver.create(); + await driver.delayMs(1000); + const ability: UIAbility = await delegator.getCurrentTopAbility(); + console.info('get top ability'); + await driver.delayMs(1000); + expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + }) + + /** + * 点击按钮,使用SM4对称密钥(ECB模式)加解密(C/C++) + */ + it('testEncryptionDecryptionGuidanceSM4001', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceSM4001 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Call SM4_ECB')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM4_ECBSuccess Result:0')); + console.info('uitest: testEncryptionDecryptionGuidanceSM4001 end'); + }) + + /** + * 点击按钮,使用SM4对称密钥(CBC模式)加解密(C/C++) + */ + it('testEncryptionDecryptionGuidanceSM4002', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceSM4002 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Call SM4_CBC')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM4_CBCSuccess Result:0')); + console.info('uitest: testEncryptionDecryptionGuidanceSM4002 end'); + }) + + /** + * 点击按钮,使用SM4对称密钥(GCM模式)加解密(C/C++) + */ + it('testEncryptionDecryptionGuidanceSM4003', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceSM4003 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Call SM4_GCM')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM4_GCMSuccess Result:0')); + console.info('uitest: testEncryptionDecryptionGuidanceSM4003 end'); + }) + + /** + * 点击按钮,使用SM4对称密钥(GCM模式)分段加解密(C/C++) + */ + it('testEncryptionDecryptionGuidanceSM4004', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidanceSM4004 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Call SM4_GCM_SEG')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM4_GCM_SEGSuccess Result:0')); + console.info('uitest: testEncryptionDecryptionGuidanceSM4004 end'); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/ohosTest/ets/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..47f0cfd409a9319481bf90d463c363b9d8c267bd --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; +import encryptionDecryptionGuidanceSM4Test from './EncryptionDecryptionGuidanceSM4.test'; + +export default function testsuite() { + abilityTest(); + encryptionDecryptionGuidanceSM4Test(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/ohosTest/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/test/LocalUnit.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/hvigor/hvigor-config.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/ohosTest.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..8bb265dabdfbc2303a7ceafa217f1e5abee56c36 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/ohosTest.md @@ -0,0 +1,11 @@ +# 使用SM4对称密钥加解密(C/C++)测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ----------------------------------------------------- | ------------ | ---- | -------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 点击按钮,使用SM4对称密钥(ECB模式)加解密(C/C++) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM4对称密钥(CBC模式)加解密(C/C++) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM4对称密钥(GCM模式)加解密(C/C++) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM4对称密钥(GCM模式)分段加解密(C/C++) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/screenshots/EncryptionDecryptionGuidanceSM41.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/screenshots/EncryptionDecryptionGuidanceSM41.png new file mode 100644 index 0000000000000000000000000000000000000000..ca830d94eecd54ff91fc2c74c4987ffbb07fc84c Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/screenshots/EncryptionDecryptionGuidanceSM41.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/screenshots/EncryptionDecryptionGuidanceSM42.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/screenshots/EncryptionDecryptionGuidanceSM42.png new file mode 100644 index 0000000000000000000000000000000000000000..3f60efaee8a1541131aea0fef2533efed9c940e2 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4/screenshots/EncryptionDecryptionGuidanceSM42.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/AppScope/app.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..294445e445be36d70125c75c4560ac1c85f43004 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.encryptiondecryptionguidancesm4arkts", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/AppScope/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..6f3ea08d13b0a3d9fe2cb3908fe869e52edbb441 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "EncryptionDecryptionGuidanceSM4ArkTs" + } + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/AppScope/resources/base/media/app_icon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/AppScope/resources/base/media/app_icon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/README.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/README.md new file mode 100644 index 0000000000000000000000000000000000000000..94280c8039a09bd40a25352bb52ae035f1615c62 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/README.md @@ -0,0 +1,84 @@ +# 使用SM4对称密钥加解密(ArkTS) + +### 介绍 + +本示例主要展示了使用SM4对称密钥(ECB模式)加解密(ArkTS)、使用SM4对称密钥(CBC模式)加解密(ArkTS)、使用SM4对称密钥(GCM模式)加解密(ArkTS)、使用SM4对称密钥(GCM模式)分段加解密(ArkTS)场景 。该工程中展示的代码详细描述可查如下链接。 + +- [使用SM4对称密钥(ECB模式)加解密(ArkTS)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-sm4-sym-encrypt-decrypt-ecb.md) +- [使用SM4对称密钥(CBC模式)加解密(ArkTS)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-sm4-sym-encrypt-decrypt-cbc.md) +- [使用SM4对称密钥(GCM模式)加解密(ArkTS)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-sm4-sym-encrypt-decrypt-gcm.md) +- [使用SM4对称密钥(GCM模式)分段加解密(ArkTS)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-sm4-sym-encrypt-decrypt-gcm-by-segment.md) + +### 效果预览 + +| 首页效果图 | 执行结果图 | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| | | + +### 使用说明 + +1. 运行Index主界面。 +2. 页面呈现上述执行结果图效果,点击不同按钮可以跳转到不同功能页面,点击跳转页面中按钮可以执行对应操作,并更新文本内容。 +3. 运行测试用例EncryptionDecryptionGuidanceSM4ArkTs.test.ets文件对页面代码进行测试可以全部通过。 + +### 工程目录 + +``` +entry/src/ + ├── main + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // 使用SM4对称密钥加解密(ArkTS)示例代码 + │ │ ├── sm4_cbc_encryption_decryption + │ │ | ├── sm4_cbc_encryption_decryption_asynchronous.ets + │ │ | ├── sm4_cbc_encryption_decryption_synchronous.ets + │ │ ├── sm4_ecb_encryption_decryption + │ │ | ├── sm4_ecb_encryption_decryption_asynchronous.ets + │ │ | ├── sm4_ecb_encryption_decryption_synchronous.ets + │ │ ├── sm4_gcm_encryption_decryption + │ │ | ├── sm4_gcm_encryption_decryption_asynchronous.ets + │ │ | ├── sm4_gcm_encryption_decryption_synchronous.ets + │ │ ├── sm4_gcm_seg_encryption_decryption + │ │ | ├── sm4_gcm_seg_encryption_decryption_asynchronous.ets + │ │ | ├── sm4_gcm_seg_encryption_decryption_synchronous.ets + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ └── test + │ │ ├── Ability.test.ets + │ │ ├── EncryptionDecryptionGuidanceSM4ArkTs.test.ets // 自动化测试代码 + │ │ └── List.test.ets +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS 5.0.2 Release及以上。 + +3. DevEco Studio版本:DevEco Studio 5.0.2 Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS 5.0.2 Release及以上。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +```` +git init +git config core.sparsecheckout true +echo CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs > .git/info/sparse-checkout +git remote add origin https://gitee.com/harmonyos_samples/guide-snippets.git +git pull origin master +```` \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fb5a1ab654ae6715a0ab69a31900ad26dcf2487f --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/build-profile.json5 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.2(14)", + "targetSdkVersion": "5.0.2(14)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/code-linter.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/.gitignore b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/build-profile.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e7569e3056e27af38e9991b7ea73ec10f3ba8a05 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/build-profile.json5 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c9cb6c8174858277c9b0d465a51547dcab16d5ff --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/entryability/EntryAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..edc2839f203ba057c186e19b0cbbbf80c8faa8b3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/Index.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..a49d552f7f29c6368439e32d289c258d619c0db7 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { router } from '@kit.ArkUI'; + +@Entry +@Component +struct Index { + goToSample(url: string): void { + router.pushUrl({ + url: url, + }, router.RouterMode.Single, (err) => { + if (err) { + console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); + return; + } + console.info('pushUrl success'); + }) + } + + build() { + Column({ space: 5 }) { + Text('EncryptionDecryptionGuidanceSM4ArkTs').fontSize(23).fontWeight(FontWeight.Bolder) + Button($r('app.string.sm4_cbc_encryption_decryption_asynchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/sm4_cbc_encryption_decryption/sm4_cbc_encryption_decryption_asynchronous') + }) + Button($r('app.string.sm4_cbc_encryption_decryption_synchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/sm4_cbc_encryption_decryption/sm4_cbc_encryption_decryption_synchronous') + }) + Button($r('app.string.sm4_ecb_encryption_decryption_asynchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/sm4_ecb_encryption_decryption/sm4_ecb_encryption_decryption_asynchronous') + }) + Button($r('app.string.sm4_ecb_encryption_decryption_synchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/sm4_ecb_encryption_decryption/sm4_ecb_encryption_decryption_synchronous') + }) + Button($r('app.string.sm4_gcm_encryption_decryption_asynchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/sm4_gcm_encryption_decryption/sm4_gcm_encryption_decryption_asynchronous') + }) + Button($r('app.string.sm4_gcm_encryption_decryption_synchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/sm4_gcm_encryption_decryption/sm4_gcm_encryption_decryption_synchronous') + }) + Button($r('app.string.sm4_gcm_seg_encryption_decryption_asynchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/sm4_gcm_seg_encryption_decryption/sm4_gcm_seg_encryption_decryption_asynchronous') + }) + Button($r('app.string.sm4_gcm_seg_encryption_decryption_synchronous')) + .width('70%') + .onClick(() => { + this.goToSample('pages/sm4_gcm_seg_encryption_decryption/sm4_gcm_seg_encryption_decryption_synchronous') + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_cbc_encryption_decryption/sm4_cbc_encryption_decryption_asynchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_cbc_encryption_decryption/sm4_cbc_encryption_decryption_asynchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..20d8296d94263f04d7ae1a90c0c263239a985a00 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_cbc_encryption_decryption/sm4_cbc_encryption_decryption_asynchronous.ets @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start async_symmetry_encrypt_decrypt_sm4_cbc] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function generateRandom(len: number) { + let rand = cryptoFramework.createRandom(); + let generateRandSync = rand.generateRandomSync(len); + return generateRandSync; +} + +function genIvParamsSpec() { + let ivBlob = generateRandom(16); // 16 bytes + let ivParamsSpec: cryptoFramework.IvParamsSpec = { + algName: 'IvParamsSpec', + iv: ivBlob + }; + return ivParamsSpec; +} + +let iv = genIvParamsSpec(); + +// 加密消息 +async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('SM4_128|CBC|PKCS7'); + await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, iv); + let encryptData = await cipher.doFinal(plainText); + return encryptData; +} + +// 解密消息 +async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('SM4_128|CBC|PKCS7'); + await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, iv); + 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() { + try { + let keyData = new Uint8Array([7, 154, 52, 176, 4, 236, 150, 43, 237, 9, 145, 166, 141, 174, 224, 131]); + let symKey = await genSymKeyByData(keyData); + let message = 'This is a test'; + 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'); + } + } catch (error) { + console.error(`SM4 “${error}“, error code: ${error.code}`); + } +} +// [End async_symmetry_encrypt_decrypt_sm4_cbc] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance SM4 ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_sm4_cbc_asynchronous')) + .width('70%') + .onClick(async () => { + try { + await main(); + this.message = 'SM4_CBCSuccess'; + } catch { + this.message = 'SM4_CBCFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_cbc_encryption_decryption/sm4_cbc_encryption_decryption_synchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_cbc_encryption_decryption/sm4_cbc_encryption_decryption_synchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..a59d5e8de3d59c99efdf544598fd29a61c488f44 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_cbc_encryption_decryption/sm4_cbc_encryption_decryption_synchronous.ets @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start sync_symmetry_encrypt_decrypt_sm4_cbc] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function generateRandom(len: number) { + let rand = cryptoFramework.createRandom(); + let generateRandSync = rand.generateRandomSync(len); + return generateRandSync; +} + +function genIvParamsSpec() { + let ivBlob = generateRandom(16); // 16 bytes + let ivParamsSpec: cryptoFramework.IvParamsSpec = { + algName: 'IvParamsSpec', + iv: ivBlob + }; + return ivParamsSpec; +} + +let iv = genIvParamsSpec(); + +// 加密消息 +function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('SM4_128|CBC|PKCS7'); + 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('SM4_128|CBC|PKCS7'); + 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('SM4_128'); + let symKey = symGenerator.convertKeySync(symKeyBlob); + console.info('convertKeySync success'); + return symKey; +} + +function main() { + try { + let keyData = new Uint8Array([7, 154, 52, 176, 4, 236, 150, 43, 237, 9, 145, 166, 141, 174, 224, 131]); + let symKey = genSymKeyByData(keyData); + 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'); + } + } catch (error) { + console.error(`SM4 “${error}“, error code: ${error.code}`); + } +} +// [End sync_symmetry_encrypt_decrypt_sm4_cbc] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance SM4 ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_sm4_cbc_synchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = 'SM4_CBCSuccess'; + } catch { + this.message = 'SM4_CBCFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_ecb_encryption_decryption/sm4_ecb_encryption_decryption_asynchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_ecb_encryption_decryption/sm4_ecb_encryption_decryption_asynchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..e250e992a49ebd969dd4a8691cee93dac7a25920 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_ecb_encryption_decryption/sm4_ecb_encryption_decryption_asynchronous.ets @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start async_symmetry_encrypt_decrypt_sm4] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 加密消息 +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 = new Uint8Array([7, 154, 52, 176, 4, 236, 150, 43, 237, 9, 145, 166, 141, 174, 224, 131]); + let symKey = await genSymKeyByData(keyData); + let message = 'This is a test'; + 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'); + } +} +// [End async_symmetry_encrypt_decrypt_sm4] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance SM4 ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_sm4_ecb_asynchronous')) + .width('70%') + .onClick(async () => { + try { + await main(); + this.message = 'SM4_ECBSuccess'; + } catch { + this.message = 'SM4_ECBFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_ecb_encryption_decryption/sm4_ecb_encryption_decryption_synchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_ecb_encryption_decryption/sm4_ecb_encryption_decryption_synchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..615d9d654509e526f66c60f114513ed501fbbed8 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_ecb_encryption_decryption/sm4_ecb_encryption_decryption_synchronous.ets @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start sync_symmetry_encrypt_decrypt_sm4] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +// 加密消息 +function encryptMessage(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); + return encryptData; +} + +// 解密消息 +function decryptMessage(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); + return decryptData; +} + +function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let symGenerator = cryptoFramework.createSymKeyGenerator('SM4_128'); + let symKey = symGenerator.convertKeySync(symKeyBlob); + console.info('convertKeySync success'); + return symKey; +} + +function main() { + let keyData = new Uint8Array([7, 154, 52, 176, 4, 236, 150, 43, 237, 9, 145, 166, 141, 174, 224, 131]); + let symKey = genSymKeyByData(keyData); + 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'); + } +} +// [End sync_symmetry_encrypt_decrypt_sm4] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance SM4 ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_sm4_ecb_synchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = 'SM4_ECBSuccess'; + } catch { + this.message = 'SM4_ECBFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_gcm_encryption_decryption/sm4_gcm_encryption_decryption_asynchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_gcm_encryption_decryption/sm4_gcm_encryption_decryption_asynchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..e5a4de1e019aaa823303fc9ef1b8ee1d02f52997 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_gcm_encryption_decryption/sm4_gcm_encryption_decryption_asynchronous.ets @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start async_symmetry_encrypt_decrypt_sm4_gcm] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function generateRandom(len: number) { + let rand = cryptoFramework.createRandom(); + let generateRandSync = rand.generateRandomSync(len); + return generateRandSync; +} + +function genGcmParamsSpec() { + let ivBlob = generateRandom(12); // 12 bytes + let arr = [1, 2, 3, 4, 5, 6, 7, 8]; // 8 bytes + let dataAad = new Uint8Array(arr); + let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; + arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes + let dataTag = new Uint8Array(arr); + let tagBlob: cryptoFramework.DataBlob = { + data: dataTag + }; + // GCM的authTag在加密时从doFinal结果中获取,在解密时填入init函数的params参数中 + let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { + iv: ivBlob, + aad: aadBlob, + authTag: tagBlob, + algName: 'GcmParamsSpec' + }; + return gcmParamsSpec; +} + +let gcmParams = genGcmParamsSpec(); + +// 加密消息 +async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('SM4_128|GCM|PKCS7'); + await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); + let encryptUpdate = await cipher.update(plainText); + // gcm模式加密doFinal时传入空,获得tag数据,并更新至gcmParams对象中。 + gcmParams.authTag = await cipher.doFinal(null); + return encryptUpdate; +} + +// 解密消息 +async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('SM4_128|GCM|PKCS7'); + await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); + let decryptUpdate = await decoder.update(cipherText); + // gcm模式解密doFinal时传入空,验证init时传入的tag数据,如果验证失败会抛出异常。 + let decryptData = await decoder.doFinal(null); + if (decryptData == null) { + console.info('GCM decrypt success, decryptData is null'); + } + return decryptUpdate; +} + +async function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let sm4Generator = cryptoFramework.createSymKeyGenerator('SM4_128'); + let symKey = await sm4Generator.convertKey(symKeyBlob); + console.info('convertKey success'); + return symKey; +} + +async function main() { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = await genSymKeyByData(keyData); + let message = 'This is a test'; + 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'); + } +} +// [End async_symmetry_encrypt_decrypt_sm4_gcm] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance SM4 ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_sm4_gcm_asynchronous')) + .width('70%') + .onClick(async() => { + try { + await main(); + this.message = 'SM4_GCMSuccess'; + } catch { + this.message = 'SM4_GCMFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_gcm_encryption_decryption/sm4_gcm_encryption_decryption_synchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_gcm_encryption_decryption/sm4_gcm_encryption_decryption_synchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..1ccef7bec4c784f4ea9fb427af70a024b74e9f34 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_gcm_encryption_decryption/sm4_gcm_encryption_decryption_synchronous.ets @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start sync_symmetry_encrypt_decrypt_sm4_gcm] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function generateRandom(len: number) { + let rand = cryptoFramework.createRandom(); + let generateRandSync = rand.generateRandomSync(len); + return generateRandSync; +} + +function genGcmParamsSpec() { + let ivBlob = generateRandom(12); // 12 bytes + let arr = [1, 2, 3, 4, 5, 6, 7, 8]; // 8 bytes + let dataAad = new Uint8Array(arr); + let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; + arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes + let dataTag = new Uint8Array(arr); + let tagBlob: cryptoFramework.DataBlob = { + data: dataTag + }; + // GCM的authTag在加密时从doFinal结果中获取,在解密时填入init函数的params参数中 + let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { + iv: ivBlob, + aad: aadBlob, + authTag: tagBlob, + algName: 'GcmParamsSpec' + }; + return gcmParamsSpec; +} + +let gcmParams = genGcmParamsSpec(); + +// 加密消息 +function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('SM4_128|GCM|PKCS7'); + cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); + let encryptUpdate = cipher.updateSync(plainText); + // gcm模式加密doFinal时传入空,获得tag数据,并更新至gcmParams对象中。 + gcmParams.authTag = cipher.doFinalSync(null); + return encryptUpdate; +} + +// 解密消息 +function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('SM4_128|GCM|PKCS7'); + decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); + let decryptUpdate = decoder.updateSync(cipherText); + // gcm模式解密doFinal时传入空,验证init时传入的tag数据,如果验证失败会抛出异常。 + let decryptData = decoder.doFinalSync(null); + if (decryptData == null) { + console.info('GCM decrypt success, decryptData is null'); + } + return decryptUpdate; +} + +function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let sm4Generator = cryptoFramework.createSymKeyGenerator('SM4_128'); + let symKey = sm4Generator.convertKeySync(symKeyBlob); + console.info('convertKeySync success'); + return symKey; +} + +function main() { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = genSymKeyByData(keyData); + 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'); + } +} +// [End sync_symmetry_encrypt_decrypt_sm4_gcm] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance SM4 ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_sm4_gcm_synchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = 'SM4_GCMSuccess'; + } catch { + this.message = 'SM4_GCMFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_gcm_seg_encryption_decryption/sm4_gcm_seg_encryption_decryption_asynchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_gcm_seg_encryption_decryption/sm4_gcm_seg_encryption_decryption_asynchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..119757d2a0a759c988fe0efa7394f6a4a33ee73d --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_gcm_seg_encryption_decryption/sm4_gcm_seg_encryption_decryption_asynchronous.ets @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start async_symmetry_encrypt_decrypt_sm4_gcm_seg] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function generateRandom(len: number) { + let rand = cryptoFramework.createRandom(); + let generateRandSync = rand.generateRandomSync(len); + return generateRandSync; +} + +function genGcmParamsSpec() { + let ivBlob = generateRandom(12); + let arr = [1, 2, 3, 4, 5, 6, 7, 8]; // 8 bytes + let dataAad = new Uint8Array(arr); + let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; + arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes + let dataTag = new Uint8Array(arr); + let tagBlob: cryptoFramework.DataBlob = { + data: dataTag + }; // The GCM authTag is obtained by doFinal() in encryption and passed in params of init() in decryption. + let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { + iv: ivBlob, + aad: aadBlob, + authTag: tagBlob, + algName: 'GcmParamsSpec' + }; + return gcmParamsSpec; +} + +let gcmParams = genGcmParamsSpec(); + +// 分段加密消息 +async function encryptMessageUpdateBySegment(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('SM4_128|GCM|PKCS7'); + await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); + let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 + let cipherText = new Uint8Array(); + for (let i = 0; i < plainText.data.length; i += updateLength) { + let updateMessage = plainText.data.subarray(i, i + updateLength); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + // 分段update + let updateOutput = await cipher.update(updateMessageBlob); + // 把update的结果拼接起来,得到密文(有些情况下还需拼接doFinal的结果,这取决于分组模式 + // 和填充模式,本例中GCM模式的doFinal结果只包含authTag而不含密文,所以不需要拼接) + let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length); + mergeText.set(cipherText); + mergeText.set(updateOutput.data, cipherText.length); + cipherText = mergeText; + } + gcmParams.authTag = await cipher.doFinal(null); + let cipherBlob: cryptoFramework.DataBlob = { data: cipherText }; + return cipherBlob; +} + +// 分段解密消息 +async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('SM4_128|GCM|PKCS7'); + await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); + let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 + let decryptText = new Uint8Array(); + for (let i = 0; i < cipherText.data.length; i += updateLength) { + let updateMessage = cipherText.data.subarray(i, i + updateLength); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + // 分段update + let updateOutput = await decoder.update(updateMessageBlob); + // 把update的结果拼接起来,得到明文 + let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length); + mergeText.set(decryptText); + mergeText.set(updateOutput.data, decryptText.length); + decryptText = mergeText; + } + let decryptData = await decoder.doFinal(null); + if (decryptData == null) { + console.info('GCM decrypt success, decryptData is null'); + } + let decryptBlob: cryptoFramework.DataBlob = { data: decryptText }; + return decryptBlob; +} + +async function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let sm4Generator = cryptoFramework.createSymKeyGenerator('SM4_128'); + let symKey = await sm4Generator.convertKey(symKeyBlob); + console.info('convertKey success'); + return symKey; +} + +async function sm4() { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = await genSymKeyByData(keyData); + let message = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee'; // 假设信息总共43字节,根据utf-8解码后,也是43字节 + let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; + let encryptText = await encryptMessageUpdateBySegment(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'); + } +} +// [End async_symmetry_encrypt_decrypt_sm4_gcm_seg] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance SM4 ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_sm4_gcm_segmentation_asynchronous')) + .width('70%') + .onClick(async () => { + try { + await sm4(); + this.message = 'SM4_GCMSegmentationSuccess'; + } catch { + this.message = 'SM4_GCMSegmentationFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_gcm_seg_encryption_decryption/sm4_gcm_seg_encryption_decryption_synchronous.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_gcm_seg_encryption_decryption/sm4_gcm_seg_encryption_decryption_synchronous.ets new file mode 100644 index 0000000000000000000000000000000000000000..1b74d85571e2417313d8a880f2dffe9b2b45995a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/ets/pages/sm4_gcm_seg_encryption_decryption/sm4_gcm_seg_encryption_decryption_synchronous.ets @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start sync_symmetry_encrypt_decrypt_sm4_gcm_seg] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function generateRandom(len: number) { + let rand = cryptoFramework.createRandom(); + let generateRandSync = rand.generateRandomSync(len); + return generateRandSync; +} + +function genGcmParamsSpec() { + let ivBlob = generateRandom(12); // 12 bytes + let arr = [1, 2, 3, 4, 5, 6, 7, 8]; // 8 bytes + let dataAad = new Uint8Array(arr); + let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; + arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes + let dataTag = new Uint8Array(arr); + let tagBlob: cryptoFramework.DataBlob = { + data: dataTag + }; // The GCM authTag is obtained by doFinal() in encryption and passed in params of init() in decryption. + let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { + iv: ivBlob, + aad: aadBlob, + authTag: tagBlob, + algName: 'GcmParamsSpec' + }; + return gcmParamsSpec; +} + +let gcmParams = genGcmParamsSpec(); + +// 分段加密消息 +function encryptMessageUpdateBySegment(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { + let cipher = cryptoFramework.createCipher('SM4_128|GCM|PKCS7'); + cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); + let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 + let cipherText = new Uint8Array(); + for (let i = 0; i < plainText.data.length; i += updateLength) { + let updateMessage = plainText.data.subarray(i, i + updateLength); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + // 分段update + let updateOutput = cipher.updateSync(updateMessageBlob); + // 把update的结果拼接起来,得到密文(有些情况下还需拼接doFinal的结果,这取决于分组模式 + // 和填充模式,本例中GCM模式的doFinal结果只包含authTag而不含密文,所以不需要拼接) + let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length); + mergeText.set(cipherText); + mergeText.set(updateOutput.data, cipherText.length); + cipherText = mergeText; + } + gcmParams.authTag = cipher.doFinalSync(null); + let cipherBlob: cryptoFramework.DataBlob = { data: cipherText }; + return cipherBlob; +} + +// 分段解密消息 +function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { + let decoder = cryptoFramework.createCipher('SM4_128|GCM|PKCS7'); + decoder.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); + let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 + let decryptText = new Uint8Array(); + for (let i = 0; i < cipherText.data.length; i += updateLength) { + let updateMessage = cipherText.data.subarray(i, i + updateLength); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + // 分段update + let updateOutput = decoder.updateSync(updateMessageBlob); + // 把update的结果拼接起来,得到明文 + let mergeText = new Uint8Array(decryptText.length + updateOutput.data.length); + mergeText.set(decryptText); + mergeText.set(updateOutput.data, decryptText.length); + decryptText = mergeText; + } + let decryptData = decoder.doFinalSync(null); + if (decryptData == null) { + console.info('GCM decrypt success, decryptData is null'); + } + let decryptBlob: cryptoFramework.DataBlob = { data: decryptText }; + return decryptBlob; +} + +function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let sm4Generator = cryptoFramework.createSymKeyGenerator('SM4_128'); + let symKey = sm4Generator.convertKeySync(symKeyBlob); + console.info('convertKeySync success'); + return symKey; +} + +function main() { + let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); + let symKey = genSymKeyByData(keyData); + let message = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee'; // 假设信息总共43字节,根据utf-8解码后,也是43字节 + let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; + let encryptText = encryptMessageUpdateBySegment(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'); + } +} +// [End sync_symmetry_encrypt_decrypt_sm4_gcm_seg] + +@Entry +@Component +struct Index { + @State message: string = 'Encryption Decryption Guidance SM4 ArkTs'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_sm4_gcm_segmentation_synchronous')) + .width('70%') + .onClick(() => { + try { + main(); + this.message = 'SM4_GCMSegmentationSuccess'; + } catch { + this.message = 'SM4_GCMSegmentationFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/element/string.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..4d2a735257f692af82d98f34e8ec563d2456c433 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/element/string.json @@ -0,0 +1,80 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "EncryptionDecryptionGuidanceSM4ArkTs" + }, + { + "name": "sm4_cbc_encryption_decryption_asynchronous", + "value": "sm4_cbc_encryption_decryption_asynchronous" + }, + { + "name": "sm4_cbc_encryption_decryption_synchronous", + "value": "sm4_cbc_encryption_decryption_synchronous" + }, + { + "name": "sm4_ecb_encryption_decryption_asynchronous", + "value": "sm4_ecb_encryption_decryption_asynchronous" + }, + { + "name": "sm4_ecb_encryption_decryption_synchronous", + "value": "sm4_ecb_encryption_decryption_synchronous" + }, + { + "name": "sm4_gcm_encryption_decryption_asynchronous", + "value": "sm4_gcm_encryption_decryption_asynchronous" + }, + { + "name": "sm4_gcm_encryption_decryption_synchronous", + "value": "sm4_gcm_encryption_decryption_synchronous" + }, + { + "name": "sm4_gcm_seg_encryption_decryption_asynchronous", + "value": "sm4_gcm_seg_encryption_decryption_asynchronous" + }, + { + "name": "call_sm4_cbc_asynchronous", + "value": "Call SM4_CBC Asynchronous" + }, + { + "name": "call_sm4_cbc_synchronous", + "value": "Call SM4_CBC Synchronous" + }, + { + "name": "call_sm4_ecb_asynchronous", + "value": "Call SM4_ECB Asynchronous" + }, + { + "name": "call_sm4_ecb_synchronous", + "value": "Call SM4_ECB Synchronous" + }, + { + "name": "call_sm4_gcm_synchronous", + "value": "Call SM4_GCM Synchronous" + }, + { + "name": "call_sm4_gcm_asynchronous", + "value": "Call SM4_GCM Asynchronous" + }, + { + "name": "call_sm4_gcm_segmentation_asynchronous", + "value": "Call SM4_GCM Segmentation Asynchronous" + }, + { + "name": "call_sm4_gcm_segmentation_synchronous", + "value": "Call SM4_GCM Segmentation Synchronous" + }, + { + "name": "sm4_gcm_seg_encryption_decryption_synchronous", + "value": "sm4_gcm_seg_encryption_decryption_synchronous" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/media/background.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/media/background.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/media/foreground.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/media/foreground.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/media/layered_image.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/media/startIcon.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/media/startIcon.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/profile/backup_config.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/profile/main_pages.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..9d77b2079eb370a9f2d69a429dffe8ad7249ad68 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,13 @@ +{ + "src": [ + "pages/Index", + "pages/sm4_cbc_encryption_decryption/sm4_cbc_encryption_decryption_asynchronous", + "pages/sm4_cbc_encryption_decryption/sm4_cbc_encryption_decryption_synchronous", + "pages/sm4_ecb_encryption_decryption/sm4_ecb_encryption_decryption_asynchronous", + "pages/sm4_ecb_encryption_decryption/sm4_ecb_encryption_decryption_synchronous", + "pages/sm4_gcm_encryption_decryption/sm4_gcm_encryption_decryption_asynchronous", + "pages/sm4_gcm_encryption_decryption/sm4_gcm_encryption_decryption_synchronous", + "pages/sm4_gcm_seg_encryption_decryption/sm4_gcm_seg_encryption_decryption_asynchronous", + "pages/sm4_gcm_seg_encryption_decryption/sm4_gcm_seg_encryption_decryption_synchronous" + ] +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/dark/element/color.json b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/ohosTest/ets/test/Ability.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidance3DESArkTs.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidance3DESArkTs.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..822fd08aa614f2f2e5df3e44d00015784cce5a5a --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/ohosTest/ets/test/EncryptionDecryptionGuidance3DESArkTs.test.ets @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, it, expect, beforeAll } from '@ohos/hypium'; +// 导入测试依赖kit +import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; + +const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); +const bundleName = abilityDelegatorRegistry.getArguments().bundleName; +let want: Want; + +export default function encryptionDecryptionGuidance3DESArkTsTest() { + describe('encryptionDecryptionGuidance3DESArkTsTest', () => { + /** + * 打开应用 + */ + beforeAll(async () => { + want = { + bundleName: bundleName, + abilityName: 'EntryAbility' + }; + await delegator.startAbility(want); + const driver = Driver.create(); + await driver.delayMs(1000); + const ability: UIAbility = await delegator.getCurrentTopAbility(); + console.info('get top ability'); + await driver.delayMs(1000); + expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + }) + + /** + * 点击按钮,使用SM4对称密钥(ECB模式)加解密(ArkTS),异步方法 + */ + it('testEncryptionDecryptionGuidance3DESArkTs001', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs001 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('sm4_cbc_encryption_decryption_asynchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SM4_CBC Asynchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM4_CBCSuccess')); + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs001 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用SM4对称密钥(ECB模式)加解密(ArkTS),同步方法 + */ + it('testEncryptionDecryptionGuidance3DESArkTs002', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs002 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('sm4_cbc_encryption_decryption_synchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SM4_CBC Synchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM4_CBCSuccess')); + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs002 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用SM4对称密钥(CBC模式)加解密(ArkTS),异步方法 + */ + it('testEncryptionDecryptionGuidance3DESArkTs003', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs003 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('sm4_ecb_encryption_decryption_asynchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SM4_ECB Asynchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM4_ECBSuccess')); + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs003 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用SM4对称密钥(CBC模式)加解密(ArkTS),同步方法 + */ + it('testEncryptionDecryptionGuidance3DESArkTs004', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs004 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('sm4_ecb_encryption_decryption_synchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SM4_ECB Synchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM4_ECBSuccess')); + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs004 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用SM4对称密钥(GCM模式)加解密(ArkTS),异步方法 + */ + it('testEncryptionDecryptionGuidance3DESArkTs005', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs005 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('sm4_gcm_encryption_decryption_asynchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SM4_GCM Asynchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM4_GCMSuccess')); + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs005 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用SM4对称密钥(GCM模式)加解密(ArkTS),同步方法 + */ + it('testEncryptionDecryptionGuidance3DESArkTs006', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs006 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('sm4_gcm_encryption_decryption_synchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SM4_GCM Synchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM4_GCMSuccess')); + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs006 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用SM4对称密钥(GCM模式)分段加解密(ArkTS),异步方法 + */ + it('testEncryptionDecryptionGuidance3DESArkTs007', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs007 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('sm4_gcm_seg_encryption_decryption_asynchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SM4_GCM Segmentation Asynchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM4_GCMSegmentationSuccess')); + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs007 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用SM4对称密钥(GCM模式)分段加解密(ArkTS),同步方法 + */ + it('testEncryptionDecryptionGuidance3DESArkTs008', 0, async () => { + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs008 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('sm4_gcm_seg_encryption_decryption_synchronous')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SM4_GCM Segmentation Synchronous')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SM4_GCMSegmentationSuccess')); + console.info('uitest: testEncryptionDecryptionGuidance3DESArkTs008 end'); + await driver.pressBack(); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/ohosTest/ets/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..8f8a843a7f316c7721ee21f37a677acf62ae79bd --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; +import encryptionDecryptionGuidance3DESArkTsTest from './EncryptionDecryptionGuidance3DESArkTs.test'; + +export default function testsuite() { + abilityTest(); + encryptionDecryptionGuidance3DESArkTsTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/ohosTest/module.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/test/List.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/test/LocalUnit.test.ets b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/hvigor/hvigor-config.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/hvigorfile.ts b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/oh-package.json5 b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/ohosTest.md b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..04e17dac5d8ccdd756696f06bb8abc365edc0c6d --- /dev/null +++ b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/ohosTest.md @@ -0,0 +1,15 @@ +# 使用SM4对称密钥加解密(ArkTS)测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ------------------------------------------------------------ | ------------ | ---- | -------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 点击按钮,使用SM4对称密钥(ECB模式)加解密(ArkTS),异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM4对称密钥(ECB模式)加解密(ArkTS),同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM4对称密钥(CBC模式)加解密(ArkTS),异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM4对称密钥(CBC模式)加解密(ArkTS),同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM4对称密钥(GCM模式)加解密(ArkTS),异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM4对称密钥(GCM模式)加解密(ArkTS),同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM4对称密钥(GCM模式)分段加解密(ArkTS),异步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用SM4对称密钥(GCM模式)分段加解密(ArkTS),同步方法 | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | \ No newline at end of file diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/screenshots/EncryptionDecryptionGuidanceSM4ArkTs1.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/screenshots/EncryptionDecryptionGuidanceSM4ArkTs1.png new file mode 100644 index 0000000000000000000000000000000000000000..5a04fcc91e9acedf046ca98eb56aae24fa8da6b8 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/screenshots/EncryptionDecryptionGuidanceSM4ArkTs1.png differ diff --git a/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/screenshots/EncryptionDecryptionGuidanceSM4ArkTs2.png b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/screenshots/EncryptionDecryptionGuidanceSM4ArkTs2.png new file mode 100644 index 0000000000000000000000000000000000000000..f890132c1cb744f007eb13065fb1d96015c825e3 Binary files /dev/null and b/CryptoArchitectureKit/EncryptionDecryption/EncryptionDecryptionGuidanceSM4ArkTs/screenshots/EncryptionDecryptionGuidanceSM4ArkTs2.png differ