ionic 一个基于base64+md5+加密因子的加密解密工具

首先安装:npm install ts-md5 --save

npm install buffer --save

直接上代码:

import { Md5 } from "ts-md5/dist/md5";

import { Buffer } from 'buffer';

export function encode(reqStr) {

//加密因子,这里可以用你自己的。

let key = "2xIV4bSDaVy5uvaahKUeTsoWKq1%PA96mzPc$X";

let reqString = encodeBase64String(reqStr);

reqString = reqString.replace("\n", "");

key = encodeMD5String(key);

let keyLen = key.length;

let strLen = reqString.length;

let rndkey: any = [];

for (var i = 0; i < 128; i++) {

rndkey[i] = key.charCodeAt(i % keyLen);

console.log(rndkey[i]);

}

let result: string='';

for (var i = 0; i < strLen; i++) {

let charCode = reqString.charCodeAt(i) ^ rndkey[i * strLen % 128];

console.log(charCode);

result = result + String.fromCharCode(charCode);

}

return key.substring(0, 16).toUpperCase() + encodeBase64String(result).replace("\n", "");

}

export function decode(encodeStr) {

let key = encodeMD5String("2xIV4bSDaVy5uvaahKUeTsoWKq1%PA96mzPc$X")

let keyLen = key.length;

let encodeResult = decodeBase64String(encodeStr.substring(16));

let strLen = encodeResult.length;

let rndkey: any = [];

for (var i = 0; i < 128; i++) {

rndkey[i] = key.charCodeAt(i % keyLen);

}

let result: string='';

for (var i = 0; i < strLen; i++) {

let charCode = encodeResult.charCodeAt(i) ^ rndkey[i * strLen % 128];

result = result + String.fromCharCode(charCode);

}

return decodeBase64String(result);

}

function encodeBase64String(str) {

return new Buffer(str).toString('base64')

}

function encodeMD5String(key) {

return Md5.hashStr(key).toString();

}

function decodeBase64String(str) {

return new Buffer(str, 'base64').toString()

}

你可能感兴趣的:(ionic 一个基于base64+md5+加密因子的加密解密工具)