最近在看harmony。网络通讯最基础的数据加解密需要封装,以下研究了其写法。其实写法和flutter里 甚至android中代码写法 大同小异,具体算法可自定义修改,只展示原理。
原理:
加密:1.json字符串(待加密原文)通过base64执行decodeSync 2.通过算法转换为byte数组 3.byte转string文章来源:https://www.toymoban.com/news/detail-787246.html
解密:1.string json字符串(密文) 转byte 2.通过算法得到新byte数组 3.通过base64的encodeToStringSync 把byte转为string文章来源地址https://www.toymoban.com/news/detail-787246.html
import util from '@ohos.util';
/** * 加解密 位移符占位数 */ static readonly PACKET_HEADER_SIZE: number = 5;
/** * 加密算法 * @param strBuf * @returns */ public decryption(strBuf: string): string { var base64 = new util.Base64Helper(); let pBuffer = base64.decodeSync(strBuf); var strBuffer = ""; var pNewBuffer = new Array(pBuffer.length - CommonConstants.PACKET_HEADER_SIZE); let iSize = pBuffer.length; if (pBuffer[0] != 0) { for (let i = CommonConstants.PACKET_HEADER_SIZE; i < iSize; i++) { let t1 = ((pBuffer[i] & 0xFF) ^ ((pBuffer[0] & 0xFF) ^ (iSize - i))) % 256; if (t1 < 0) { t1 = t1 + 256; } pNewBuffer[i - CommonConstants.PACKET_HEADER_SIZE] = t1; let t2 = ((pNewBuffer[i - CommonConstants.PACKET_HEADER_SIZE] & 0xFF) - ((i - CommonConstants.PACKET_HEADER_SIZE) ^ (pBuffer[0] & 0xFF))) % 256; // 取余数时 负数 加 256 if (t2 < 0) { t2 = t2 + 256; } // pNewBuffer[i - PACKET_HEADER_SIZE] = Convert.ToByte(t2); pNewBuffer[i - CommonConstants.PACKET_HEADER_SIZE] = t2; } strBuffer = this.byteToString(pNewBuffer); } return strBuffer; } /** * 解密算法 * @param strBuf * @param bEncryption * @returns */ public encryption(strBuf: string, bEncryption: boolean): string { var base64 = new util.Base64Helper(); var pSndBuf = new Array(); pSndBuf = this.stringToByte(strBuf); var iSize = pSndBuf.length; let pTempBuffer = new Uint8Array(pSndBuf.length + CommonConstants.PACKET_HEADER_SIZE); if (bEncryption) { var rd = Math.random() * 1000; pTempBuffer[0] = rd % 128; } else { pTempBuffer[0] = 0; } var iLength = (iSize + CommonConstants.PACKET_HEADER_SIZE); var byteLength = new Array(iLength); pTempBuffer[1] = byteLength[0]; pTempBuffer[2] = byteLength[1]; if (pTempBuffer[0] != 0) { for (let i = 0; i < iSize; i++) { pTempBuffer[CommonConstants.PACKET_HEADER_SIZE + i] = (((pSndBuf[i] & 0xFF) + (i ^ ((pTempBuffer[0] & 0xFF)))) % 256); pTempBuffer[CommonConstants.PACKET_HEADER_SIZE + i] = (((pTempBuffer[CommonConstants.PACKET_HEADER_SIZE + i] & 0xFF) ^ ((pTempBuffer[0] & 0xFF) ^ (iSize - i))) % 256); } } else { for (let i = 0; i < iSize; i++) { pTempBuffer[CommonConstants.PACKET_HEADER_SIZE + i] = pSndBuf[i]; } } return base64.encodeToStringSync(pTempBuffer); } /** * string转byte * @param str * @returns */ public stringToByte(str) { var bytes = new Array(); var len, c; len = str.length; for (var i = 0; i < len; i++) { c = str.charCodeAt(i); if (c >= 0x010000 && c <= 0x10FFFF) { bytes.push(((c >> 18) & 0x07) | 0xf0); bytes.push(((c >> 12) & 0x3F) | 0x80); bytes.push(((c >> 6) & 0x3f) | 0x80); bytes.push((c & 0x3F) | 0x80); } else if (c >= 0x000800 && c <= 0x00FFF) { bytes.push(((c >> 12) & 0x07) | 0xf0); bytes.push(((c >> 6) & 0x3F) | 0x80); bytes.push((c & 0x3F) | 0x80); } else if (c >= 0x000800 && c <= 0x0007FF) { bytes.push(((c >> 6) & 0x3F) | 0x80); bytes.push((c & 0x3F) | 0x80); } else { bytes.push(c & 0xFF) } } return bytes; } /** * byte转string * @param str * @returns */ public byteToString(arr): string { if (typeof arr === 'string') { return arr; } var str = '', _arr = arr; for (var i = 0; i < _arr.length; i++) { var one = _arr[i].toString(2), v = one.match(/^1+?(?=0)/); if (v && one.length == 8) { var bytesLength = v[0].length; var store = _arr[i].toString(2).slice(7 - bytesLength); for (var st = 1; st < bytesLength; st++) { store += _arr[st + i].toString(2).slice(2); } str += String.fromCharCode(parseInt(store, 2)); i += bytesLength - 1; } else { str += String.fromCharCode(_arr[i]); } } return str; }
用法: CommonUtils.encryption(needEncryption.toString(),true); CommonUtils.decryption(resultJson.Body.toString());
到了这里,关于鸿蒙harmony加解密算法 写法封装的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!