diff --git a/quick_delivery/4796/CryptoSM2/README.md b/quick_delivery/4796/CryptoSM2/README.md new file mode 100644 index 0000000000000000000000000000000000000000..18e1c48a3377b720f44ddf805711e33baa4064a7 --- /dev/null +++ b/quick_delivery/4796/CryptoSM2/README.md @@ -0,0 +1,69 @@ +# SM2密文转换 + +### 介绍 + +本示例对使用[@kit.CryptoArchitectureKit](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework-0000001820881417)加密后的密文格式进行转换。[@kit.CryptoArchitectureKit](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework-0000001820881417)加密后的密文格式默认为以base64显示的ASN.1格式问题,通过对密文进行base64变换后得到字符数组,以16进制数字显示,再此基础上进行密文格式转换,从ASN.1格式转换为c1c3c2格式的裸密文,再以c1c3c2格式的裸密文进行解密,以验证密文转换的正确性。 + +### 效果预览 + +| 首页 | 数据加密 | base64变换 | 密文格式变换 | 数据解密 | +| -------------------------------------- | ---------------------------------------- | ---------- | ------------ | -------- | +| ![index](screenshots/device/index.jpg) | ![encode](screenshots/device/encode.jpg) | ![hex](screenshots/device/hex.jpg) | ![c1c3c2](screenshots/device/c1c3c2.jpg) | ![decode](screenshots/device/decode.jpg) | + +使用说明 + +1. 点击主页面**加密**按钮,对原始数据使用SM2国密算法进行加密,其内容显示在**加密数据**文本框中, +此时**解密**按钮和**base64转换**按钮使能 +2. 点击主页面**base64转换**按钮,对原始密文进行base64转换,在**加密数据**文本框中显示转换后的密文 +此时**asn.1转换**按钮使能 +3. 点击主页面**asn.1转换**按钮,对密文进行asn.1转换,在**加密数据**文本框中显示转换后的密文 +此时**加密**按钮和**base64转换**按钮去使能 +4. 点击主页面**解密**按钮,对密文进行解密,在**解密数据**文本框中显示解密后的文本 +此时**解密**按钮去使能 + +### 工程目录 + +``` +entry/src/main/ // 代码区 +├── ets +│   ├── entryability +│   │   └── EntryAbility.ets +│   ├── pages +│   │   └── Index.ets // 主页界面 +│   └── utils +│   ├── SM2.ets // SM2加解密 +│   ├── SM2CipherText.ets // 密文格式转换 +│   └── SM2Sequence.ets // ASN.1格式数据结构 +├── module.json5 +└── resources // 应用资源目录 +``` + +### 具体实现 + + +* 对文本加密:在[SM2.ets](entry/src/main/ets/utils/SM2.ets) + 点击加密按钮,调用加密函数实现对文本内容进行加密。点击解密按钮,调用解密函数实现对文本内容进行解密. + 对消息加密的过程中采用[cryptoFramework.Cipher](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-cryptoframework-0000001820881417#ZH-CN_TOPIC_0000001820881417__cipher) + 完成加解密操作。 +* 对密文格式进行转换:在[SM2CipherText.ets](entry/src/main/ets/utils/SM2CipherText.ets) + 点击asn.1按钮,调用密文转换函数实现对密文内容进行转换。 + 完成密文转换操作。 + + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行,支持设备:华为手机。 + +2.HarmonyOS系统:HarmonyOS NEXT Developer Preview1及以上。 + +3.DevEco Studio版本:DevEco Studio NEXT Developer Preview1及以上。 + +4.HarmonyOS SDK版本:HarmonyOS NEXT Developer Preview1 SDK及以上。 \ No newline at end of file diff --git a/quick_delivery/4796/CryptoSM2/entry/src/main/ets/pages/Index.ets b/quick_delivery/4796/CryptoSM2/entry/src/main/ets/pages/Index.ets index 6805f0b36c40a84b4f44910c6e86525b1c2db054..2c1b4d69dffead744b3d400661aad5f8447e9209 100644 --- a/quick_delivery/4796/CryptoSM2/entry/src/main/ets/pages/Index.ets +++ b/quick_delivery/4796/CryptoSM2/entry/src/main/ets/pages/Index.ets @@ -1,6 +1,6 @@ import { decryptSM2, encryptSM2 } from '../utils/SM2'; import { buffer, util } from '@kit.ArkTS'; -import { SM2_CipherText } from '../utils/SM2CipherText'; +import { SM2CipherText } from '../utils/SM2CipherText'; const TEXT_LABEL_WIDTH = '20%'; const TEXT_FONT_SIZE = 15; @@ -15,12 +15,12 @@ const BTN_MARGIN = 15; @Component struct Index { @State message: string = 'Hello World'; - @State enmsg: string = ''; - @State demsg: string = ''; - @State isbase64: Boolean = true; + @State enMsg: string = ''; + @State deMsg: string = ''; + @State isBase64: Boolean = true; @State isc1c3c2: Boolean = false; - @State msg_type: Resource = $r('app.string.text_asn1'); - @State msg_fmt: Resource = $r('app.string.text_hex'); + @State msgType: Resource = $r('app.string.text_asn1'); + @State msgFmt: Resource = $r('app.string.text_base64'); build() { Row() { @@ -42,15 +42,15 @@ struct Index { Text($r('app.string.text_encode_data')) .width(TEXT_LABEL_WIDTH) .fontSize(TEXT_FONT_SIZE) - Text(this.msg_type) + Text(this.msgType) .width(TEXT_LABEL_WIDTH) .fontSize(TEXT_FONT_SIZE) - Text(this.msg_fmt) + Text(this.msgFmt) .width(TEXT_LABEL_WIDTH) .fontSize(TEXT_FONT_SIZE) } - Text(this.enmsg) + Text(this.enMsg) .width(TEXT_VALUE_WIDTH) .margin(TEXT_MARGIN) .fontSize(TEXT_FONT_SIZE) @@ -61,7 +61,7 @@ struct Index { Text($r('app.string.text_decode_data')) .width(TEXT_LABEL_WIDTH) .fontSize(TEXT_FONT_SIZE) - Text(this.demsg) + Text(this.deMsg) .width(TEXT_VALUE_WIDTH) .margin(TEXT_MARGIN) .fontSize(TEXT_FONT_SIZE) @@ -75,14 +75,14 @@ struct Index { .enabled(!this.isc1c3c2) .onClick(() => { encryptSM2(this.message).then((msg: string) => { - if (this.isbase64) { - this.enmsg = msg; + if (this.isBase64) { + this.enMsg = msg; } else { let base64Helper = new util.Base64Helper(); - this.enmsg = buffer.from(base64Helper.decodeSync(msg).buffer).toString('hex'); + this.enMsg = buffer.from(base64Helper.decodeSync(msg).buffer).toString('hex'); } - this.demsg = ''; + this.deMsg = ''; }); }) @@ -90,15 +90,15 @@ struct Index { .borderRadius(BTN_BORDER_RADIUS) .fontSize(BTN_FONT_SIZE) .margin(BTN_MARGIN) - .enabled(this.enmsg != '' && this.demsg == '') + .enabled(this.enMsg != '' && this.deMsg == '') .onClick(() => { - let msg = this.enmsg; - if (!this.isbase64 && !this.isc1c3c2) { + let msg = this.enMsg; + if (!this.isBase64 && !this.isc1c3c2) { let base64Helper = new util.Base64Helper(); - msg = base64Helper.encodeToStringSync(new Uint8Array(buffer.from(this.enmsg, 'hex').buffer)); + msg = base64Helper.encodeToStringSync(new Uint8Array(buffer.from(this.enMsg, 'hex').buffer)); } decryptSM2(msg, this.isc1c3c2).then((msg: string) => { - this.demsg = msg; + this.deMsg = msg; }); }) @@ -106,18 +106,18 @@ struct Index { .borderRadius(BTN_BORDER_RADIUS) .fontSize(BTN_FONT_SIZE) .margin(BTN_MARGIN) - .enabled(!this.isc1c3c2 && this.enmsg != '') + .enabled(!this.isc1c3c2 && this.enMsg != '') .onClick(() => { let base64Helper = new util.Base64Helper(); - if (this.isbase64) { - this.enmsg = buffer.from(base64Helper.decodeSync(this.enmsg).buffer).toString('hex'); - this.isbase64 = false; - this.msg_fmt = $r('app.string.text_hex') + if (this.isBase64) { + this.enMsg = buffer.from(base64Helper.decodeSync(this.enMsg).buffer).toString('hex'); + this.isBase64 = false; + this.msgFmt = $r('app.string.text_hex') } else { - this.enmsg = base64Helper.encodeToStringSync(new Uint8Array(buffer.from(this.enmsg, 'hex').buffer)); - this.isbase64 = true; - this.msg_fmt = $r('app.string.text_base64') + this.enMsg = base64Helper.encodeToStringSync(new Uint8Array(buffer.from(this.enMsg, 'hex').buffer)); + this.isBase64 = true; + this.msgFmt = $r('app.string.text_base64') } }) @@ -125,17 +125,17 @@ struct Index { .borderRadius(BTN_BORDER_RADIUS) .fontSize(BTN_FONT_SIZE) .margin(BTN_MARGIN) - .enabled(!this.isbase64) + .enabled(!this.isBase64) .onClick(() => { if (this.isc1c3c2) { - this.enmsg = new SM2_CipherText().i2d_SM2_CipherText(this.enmsg); + this.enMsg = new SM2CipherText().i2dSM2CipherText(this.enMsg); this.isc1c3c2 = false; - this.msg_type = $r('app.string.text_asn1') + this.msgType = $r('app.string.text_asn1') } else { - this.enmsg = new SM2_CipherText().d2i_SM2_CipherText(this.enmsg); + this.enMsg = new SM2CipherText().d2iSM2CipherText(this.enMsg); this.isc1c3c2 = true; - this.msg_type = $r('app.string.text_c1c3c2') + this.msgType = $r('app.string.text_c1c3c2') } }) } diff --git a/quick_delivery/4796/CryptoSM2/entry/src/main/ets/utils/SM2.ets b/quick_delivery/4796/CryptoSM2/entry/src/main/ets/utils/SM2.ets index ded72ac40534b201983c0193b7c0f3c5a4ccce9c..a4a70573fe8b96d741b97601c0b7be553ddf3900 100644 --- a/quick_delivery/4796/CryptoSM2/entry/src/main/ets/utils/SM2.ets +++ b/quick_delivery/4796/CryptoSM2/entry/src/main/ets/utils/SM2.ets @@ -1,7 +1,7 @@ import { cryptoFramework } from '@kit.CryptoArchitectureKit'; import { print } from '@kit.BasicServicesKit'; import { buffer, util } from '@kit.ArkTS'; -import { SM2_CipherText } from './SM2CipherText'; +import { SM2CipherText } from './SM2CipherText'; async function genECCPubKey(key: string) { let mode: number = 1; @@ -104,7 +104,7 @@ export async function decryptSM2(encryptedStr: string, isc1c3c2: Boolean = false let priKey: string = "3629EFF03FBC86711F6695CBF5590F0F2FCAAA3C269A1CA9BD64FB4C70DF9C9F" if (isc1c3c2) { - let hexStr = new SM2_CipherText().i2d_SM2_CipherText(encryptedStr); + let hexStr = new SM2CipherText().i2dSM2CipherText(encryptedStr); let encryptedArray = new Uint8Array(buffer.from(hexStr, 'hex').buffer); return decryptByPrimalKeyArray(encryptedArray, priKey); } diff --git a/quick_delivery/4796/CryptoSM2/entry/src/main/ets/utils/SM2CipherText.ets b/quick_delivery/4796/CryptoSM2/entry/src/main/ets/utils/SM2CipherText.ets index 6f0d8e93d4824f6fea217cedd936b786dcd8ad04..911f9aeaf0d2d5dc64a9ceb13e2ecc0228b9a26c 100644 --- a/quick_delivery/4796/CryptoSM2/entry/src/main/ets/utils/SM2CipherText.ets +++ b/quick_delivery/4796/CryptoSM2/entry/src/main/ets/utils/SM2CipherText.ets @@ -1,21 +1,21 @@ -import { SM2_SEQUENCE } from './SM2Sequence'; +import { SM2Sequence } from './SM2Sequence'; import { hilog } from '@kit.PerformanceAnalysisKit'; export class ASN1Util { - static BOOLEAN: string = "01"; - static INTEGER: string = "02"; - static BIT_STRING: string = "03"; - static OCTEN_STRING: string = "04"; - static NULL: string = "05"; - static REAL: string = "09"; - static ENUMERATED: string = "0a"; - static SEQUENCE: string = "30"; - static SET: string = "31"; + static readonly BOOLEAN: string = "01"; + static readonly INTEGER: string = "02"; + static readonly BIT_STRING: string = "03"; + static readonly OCTEN_STRING: string = "04"; + static readonly NULL: string = "05"; + static readonly REAL: string = "09"; + static readonly ENUMERATED: string = "0a"; + static readonly SEQUENCE: string = "30"; + static readonly SET: string = "31"; } -export class SM2_CipherText { - i2d_SM2_CipherText(primal_data: string): string { - let sm2_sequence = new SM2_SEQUENCE(); +export class SM2CipherText { + i2dSM2CipherText(primal_data: string): string { + let sm2_sequence = new SM2Sequence(); sm2_sequence.C1x = primal_data.slice(0, 64); primal_data = primal_data.slice(64, primal_data.length); sm2_sequence.C1y = primal_data.slice(0, 64); @@ -35,7 +35,7 @@ export class SM2_CipherText { return standard_data; } - d2i_SM2_CipherText(standard_data: string): string { + d2iSM2CipherText(standard_data: string): string { let message: string = standard_data; if (!message.startsWith(ASN1Util.SEQUENCE)) { this.ciphertextErr(); @@ -49,7 +49,7 @@ export class SM2_CipherText { this.ciphertextErr(); } - let sm2_sequence = new SM2_SEQUENCE(); + let sm2_sequence = new SM2Sequence(); message = this.readC1(sm2_sequence, message); message = this.readC3(sm2_sequence, message); message = this.readC2(sm2_sequence, message); @@ -93,7 +93,7 @@ export class SM2_CipherText { throw new Error("SM2 ciphertext error!"); } - readC1(sm2_sequence: SM2_SEQUENCE, data:string): string { + readC1(sm2_sequence: SM2Sequence, data:string): string { let xy: string[] = []; for (let i = 0; i < 2; i++) { if (data.startsWith("0220")) { @@ -111,7 +111,7 @@ export class SM2_CipherText { return data; } - readC2(sm2_sequence: SM2_SEQUENCE, data:string): string { + readC2(sm2_sequence: SM2Sequence, data:string): string { if (data.startsWith(ASN1Util.OCTEN_STRING)) { data = data.slice(ASN1Util.OCTEN_STRING.length, data.length); let C2_lenHex = this.getLenHex(data); @@ -127,7 +127,7 @@ export class SM2_CipherText { return data; } - readC3(sm2_sequence: SM2_SEQUENCE, data:string): string { + readC3(sm2_sequence: SM2Sequence, data:string): string { if (data.startsWith("0420")) { sm2_sequence.C3 = data.slice(4, 68); data = data.slice(68, data.length); diff --git a/quick_delivery/4796/CryptoSM2/entry/src/main/ets/utils/SM2Sequence.ets b/quick_delivery/4796/CryptoSM2/entry/src/main/ets/utils/SM2Sequence.ets index 21f616da65906c27adfb2320d334edb1620bd396..4a47e8159c490878cc0403efa1a451932babcaa3 100644 --- a/quick_delivery/4796/CryptoSM2/entry/src/main/ets/utils/SM2Sequence.ets +++ b/quick_delivery/4796/CryptoSM2/entry/src/main/ets/utils/SM2Sequence.ets @@ -1,42 +1,42 @@ -export class SM2_SEQUENCE { - private _C1x: string = ""; +export class SM2Sequence { + private c1x: string = ""; public set C1x(value: string) { - this._C1x = value; + this.c1x = value; } public get C1x(): string { - return this._C1x; + return this.c1x; } - private _C1y: string = ""; + private c1y: string = ""; public set C1y(value: string) { - this._C1y = value; + this.c1y = value; } public get C1y(): string { - return this._C1y; + return this.c1y; } - private _C2: string = ""; + private c2: string = ""; public set C2(value: string) { - this._C2 = value; + this.c2 = value; } public get C2(): string { - return this._C2; + return this.c2; } - private _C3: string = ""; + private c3: string = ""; public set C3(value: string) { - this._C3 = value; + this.c3 = value; } public get C3(): string { - return this._C3; + return this.c3; } public toString(): string { diff --git a/quick_delivery/4796/CryptoSM2/entry/src/main/module.json5 b/quick_delivery/4796/CryptoSM2/entry/src/main/module.json5 index 6c07d9d8839ac9716809c6e06c21805d52c138fe..984d6c18bf955dea4de3cfb0e577ef3a09961861 100644 --- a/quick_delivery/4796/CryptoSM2/entry/src/main/module.json5 +++ b/quick_delivery/4796/CryptoSM2/entry/src/main/module.json5 @@ -17,7 +17,7 @@ "name": "EntryAbility", "srcEntry": "./ets/entryability/EntryAbility.ets", "description": "$string:EntryAbility_desc", - "icon": "$media:icon", + "icon": "$media:startIcon", "label": "$string:EntryAbility_label", "startWindowIcon": "$media:startIcon", "startWindowBackground": "$color:start_window_background", diff --git a/quick_delivery/4796/CryptoSM2/entry/src/main/resources/base/element/string.json b/quick_delivery/4796/CryptoSM2/entry/src/main/resources/base/element/string.json index 7f27d2a7c136d5e786376a73b036caab9285e434..1cd83fac5ed87986d30d57420efd1ba646cc6a7a 100644 --- a/quick_delivery/4796/CryptoSM2/entry/src/main/resources/base/element/string.json +++ b/quick_delivery/4796/CryptoSM2/entry/src/main/resources/base/element/string.json @@ -10,7 +10,7 @@ }, { "name": "EntryAbility_label", - "value": "label" + "value": "SM2Data" }, { "name": "text_origin_data", diff --git a/quick_delivery/4796/CryptoSM2/entry/src/main/resources/en_US/element/string.json b/quick_delivery/4796/CryptoSM2/entry/src/main/resources/en_US/element/string.json index 7f27d2a7c136d5e786376a73b036caab9285e434..1cd83fac5ed87986d30d57420efd1ba646cc6a7a 100644 --- a/quick_delivery/4796/CryptoSM2/entry/src/main/resources/en_US/element/string.json +++ b/quick_delivery/4796/CryptoSM2/entry/src/main/resources/en_US/element/string.json @@ -10,7 +10,7 @@ }, { "name": "EntryAbility_label", - "value": "label" + "value": "SM2Data" }, { "name": "text_origin_data", diff --git a/quick_delivery/4796/CryptoSM2/entry/src/main/resources/zh_CN/element/string.json b/quick_delivery/4796/CryptoSM2/entry/src/main/resources/zh_CN/element/string.json index a751d73cdde18efecced9e4f35ed46d315501ee9..0c1dd6dc87fd9cdfb2042d73c72077ae30af0cbb 100644 --- a/quick_delivery/4796/CryptoSM2/entry/src/main/resources/zh_CN/element/string.json +++ b/quick_delivery/4796/CryptoSM2/entry/src/main/resources/zh_CN/element/string.json @@ -6,11 +6,11 @@ }, { "name": "EntryAbility_desc", - "value": "description" + "value": "SM2算法密文转换" }, { "name": "EntryAbility_label", - "value": "label" + "value": "密文转换" }, { "name": "text_origin_data", diff --git a/quick_delivery/4796/CryptoSM2/screenshots/device/c1c3c2.jpg b/quick_delivery/4796/CryptoSM2/screenshots/device/c1c3c2.jpg new file mode 100644 index 0000000000000000000000000000000000000000..bfa5ab4c0824a78aede96db881aef950aa911e03 Binary files /dev/null and b/quick_delivery/4796/CryptoSM2/screenshots/device/c1c3c2.jpg differ diff --git a/quick_delivery/4796/CryptoSM2/screenshots/device/decode.jpg b/quick_delivery/4796/CryptoSM2/screenshots/device/decode.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e83124a848937e888d801edf0bd1d18c797d7d27 Binary files /dev/null and b/quick_delivery/4796/CryptoSM2/screenshots/device/decode.jpg differ diff --git a/quick_delivery/4796/CryptoSM2/screenshots/device/encode.jpg b/quick_delivery/4796/CryptoSM2/screenshots/device/encode.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a653d4c4e4b5ec4f4a55a2cfb26e3efa8475e495 Binary files /dev/null and b/quick_delivery/4796/CryptoSM2/screenshots/device/encode.jpg differ diff --git a/quick_delivery/4796/CryptoSM2/screenshots/device/hex.jpg b/quick_delivery/4796/CryptoSM2/screenshots/device/hex.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e68cacb8f8395ab23fc50df0e94ef2923fab643d Binary files /dev/null and b/quick_delivery/4796/CryptoSM2/screenshots/device/hex.jpg differ diff --git a/quick_delivery/4796/CryptoSM2/screenshots/device/index.jpg b/quick_delivery/4796/CryptoSM2/screenshots/device/index.jpg new file mode 100644 index 0000000000000000000000000000000000000000..43f9ccb809e77593495f567818160036a744952a Binary files /dev/null and b/quick_delivery/4796/CryptoSM2/screenshots/device/index.jpg differ