1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import cipher from '@system.cipher' 17import Logger from './Logger' 18 19const TAG: string = '[CipherModel]' 20const AES_ENCRYPT_KEY: string = 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=' 21const RSA_ENCRYPT_KEY: string = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCx414QSP3RsYWYzf9mkBMiBAXo\n' + 22'6S7Lpva1fKlcuVxjoFC1iMnzD4mC0uiL4k5MNi43J64c7dbqi3qAJjdAtuwQ6NZJ\n' + 23'+Enz0RzmVFh/4yk6lmqRzuEFQqhQqSZzaLq6sq2N2G0Sv2Xl3sLvqAfe2HNm2oBw\n' + 24'jBpApTJ3TeneOo6Z5QIDAQAB' 25const RSA_DECRYPT_KEY: string = 'MIICXgIBAAKBgQCx414QSP3RsYWYzf9mkBMiBAXo6S7Lpva1fKlcuVxjoFC1iMnz\n' + 26'D4mC0uiL4k5MNi43J64c7dbqi3qAJjdAtuwQ6NZJ+Enz0RzmVFh/4yk6lmqRzuEF\n' + 27'QqhQqSZzaLq6sq2N2G0Sv2Xl3sLvqAfe2HNm2oBwjBpApTJ3TeneOo6Z5QIDAQAB\n' + 28'AoGBAKPNtoRQcklxqo+2wQP0j2m3Qqnib1DggjVEgb/8f/LNYQSI3U2QdROemryU\n' + 29'u3y6N3xacZ359PktTrRKfH5+8ohmHGhIuPAnefp6bLvAFUcl4t1xm74Cow62Kyw3\n' + 30'aSbmuTG98dxPA1sXD0jiprdtsq2wQ9CoKNyY7/d/pKoqxNuBAkEA4GytZ60NCTj9\n' + 31'w24jACFeko5YqCFY/TTLoc4SQvWtFMnimRPclLZhtUIK0P8dib71UFedx+AxklgL\n' + 32'A5gjcfo+2QJBAMrqiwyCh3OQ5DhyRPDwt87x1/jg5fy4hhete2ufSf2FoQCVqO+w\n' + 33'PKoljdXmJeS6rGgzGibstuHLrP3tcIho4+0CQD3ZFWzF/xq0jxKlrpWhnJuNCRfE\n' + 34'oO6e9yNvVA8J/5oEDSOcmqSNIp4+RhbUx8InUxnCG6Ryv5aSFu71pYcKrPkCQQCL\n' + 35'RUGcm3ZGTnslduB0knNF+V2ndwzDUQ7P74UXT+PjurTPhujFYiuxCEd6ORVnEOzG\n' + 36'M9TORIgdH8MjIbWsGnndAkEAw9yURDaorE8IYPLF2IEn09g1uzvWPs3phDb6smVx\n' + 37'8GfqIdUNf+aCG5TZK/kXBF1sqcsi7jXMAf4jBlejVbSVZg==' 38 39export class CipherModel { 40 rsaEncrypt(message: string, callback) { 41 let result: Object | string = undefined 42 cipher.rsa({ 43 // 加密 44 action: 'encrypt', 45 // 待加密的文本内容 46 text: message, 47 // base64编码后的加密公钥 48 key: RSA_ENCRYPT_KEY, 49 transformation: 'RSA/None/OAEPWithSHA256AndMGF1Padding', 50 success: (info) => { 51 result = info 52 Logger.info(TAG, `result = ${JSON.stringify(result)}`) 53 callback(result) 54 }, 55 fail: (data, code) => { 56 result = 'Error!' 57 Logger.debug(TAG, `cipher.rsa encrypt fail ${JSON.stringify(code)}: ${JSON.stringify(data)}`) 58 callback(result) 59 }, 60 complete: () => { 61 Logger.info(TAG, `encrypt is success`) 62 } 63 }) 64 } 65 66 rsaDecrypt(message: string, callback) { 67 let result: Object | string = undefined 68 cipher.rsa({ 69 // 解密: 70 action: 'decrypt', 71 // 待解密的内容 72 text: message, 73 // base64编码后的解密私钥 74 key: RSA_DECRYPT_KEY, 75 success: (info) => { 76 result = info 77 Logger.info(TAG, `result = ${JSON.stringify(result)}`) 78 callback(result) 79 }, 80 fail: (data, code) => { 81 result = 'Error!' 82 Logger.debug(TAG, `cipher.rsa decrypt fail ${JSON.stringify(code)}: ${JSON.stringify(data)}`) 83 callback(result) 84 }, 85 complete: () => { 86 Logger.info(TAG, `encrypt is success`) 87 } 88 }) 89 } 90 91 aesEncrypt(message: string, callback) { 92 let result: Object | string = undefined 93 cipher.aes({ 94 // 加密 95 action: 'encrypt', 96 // 待加密的文本内容 97 text: message, 98 // base64编码后的密钥 99 key: AES_ENCRYPT_KEY, 100 transformation: 'AES/CBC/PKCS5Padding', 101 ivOffset: '0', 102 ivLen: '16', 103 success: (info) => { 104 result = info 105 Logger.info(TAG, `result = ${JSON.stringify(result)}`) 106 callback(result) 107 }, 108 fail: (data, code) => { 109 result = 'Error!' 110 Logger.debug(TAG, `cipher.aes encrypt fail ${JSON.stringify(code)}: ${JSON.stringify(data)}`) 111 callback(result) 112 }, 113 complete: () => { 114 Logger.info(TAG, `encrypt is success`) 115 } 116 }) 117 } 118 119 aesDecrypt(message: string, callback) { 120 let result: Object | string = undefined 121 cipher.aes({ 122 // 解密: 123 action: 'decrypt', 124 // 待解密的内容,是base64编码后的一段二进制值 125 text: message, 126 // base64编码后的密钥 127 key: AES_ENCRYPT_KEY, 128 transformation: 'AES/CBC/PKCS5Padding', 129 ivOffset: '0', 130 ivLen: '16', 131 success: (info) => { 132 result = info 133 Logger.info(TAG, `data = ${JSON.stringify(result)}`) 134 callback(result) 135 }, 136 fail: (data, code) => { 137 result = 'Error!' 138 Logger.debug(TAG, `cipher.aes encrypt fail ${JSON.stringify(code)}: ${JSON.stringify(data)}`) 139 callback(result) 140 }, 141 complete: () => { 142 Logger.info(TAG, `encrypt is success`) 143 } 144 }) 145 } 146}