|
@@ -45,11 +45,11 @@ function SM4() {
|
|
|
0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279
|
|
|
];
|
|
|
|
|
|
- this.GET_ULONG_BE = function(b, i) {
|
|
|
+ this.GET_ULONG_BE = function (b, i) {
|
|
|
return (b[i] & 0xff) << 24 | ((b[i + 1] & 0xff) << 16) | ((b[i + 2] & 0xff) << 8) | (b[i + 3] & 0xff) & 0xffffffff;
|
|
|
}
|
|
|
|
|
|
- this.PUT_ULONG_BE = function(n, b, i) {
|
|
|
+ this.PUT_ULONG_BE = function (n, b, i) {
|
|
|
var t1 = (0xFF & (n >> 24));
|
|
|
var t2 = (0xFF & (n >> 16));
|
|
|
var t3 = (0xFF & (n >> 8));
|
|
@@ -60,17 +60,17 @@ function SM4() {
|
|
|
b[i + 3] = t4 > 128 ? t4 - 256 : t4;
|
|
|
}
|
|
|
|
|
|
- this.SHL = function(x, n) {
|
|
|
+ this.SHL = function (x, n) {
|
|
|
return (x & 0xFFFFFFFF) << n;
|
|
|
}
|
|
|
|
|
|
- this.ROTL = function(x, n) {
|
|
|
+ this.ROTL = function (x, n) {
|
|
|
var s = this.SHL(x, n);
|
|
|
var ss = x >> (32 - n);
|
|
|
return this.SHL(x, n) | x >> (32 - n);
|
|
|
}
|
|
|
|
|
|
- this.sm4Lt = function(ka) {
|
|
|
+ this.sm4Lt = function (ka) {
|
|
|
var bb = 0;
|
|
|
var c = 0;
|
|
|
var a = new Array(4);
|
|
@@ -85,11 +85,11 @@ function SM4() {
|
|
|
return c;
|
|
|
}
|
|
|
|
|
|
- this.sm4F = function(x0, x1, x2, x3, rk) {
|
|
|
+ this.sm4F = function (x0, x1, x2, x3, rk) {
|
|
|
return x0 ^ this.sm4Lt(x1 ^ x2 ^ x3 ^ rk);
|
|
|
}
|
|
|
|
|
|
- this.sm4CalciRK = function(ka) {
|
|
|
+ this.sm4CalciRK = function (ka) {
|
|
|
var bb = 0;
|
|
|
var rk = 0;
|
|
|
var a = new Array(4);
|
|
@@ -104,13 +104,13 @@ function SM4() {
|
|
|
return rk;
|
|
|
}
|
|
|
|
|
|
- this.sm4Sbox = function(inch) {
|
|
|
+ this.sm4Sbox = function (inch) {
|
|
|
var i = inch & 0xFF;
|
|
|
var retVal = SboxTable[i];
|
|
|
return retVal > 128 ? retVal - 256 : retVal;
|
|
|
}
|
|
|
|
|
|
- this.sm4_setkey_enc = function(ctx, key) {
|
|
|
+ this.sm4_setkey_enc = function (ctx, key) {
|
|
|
if (ctx == null) {
|
|
|
alert("ctx is null!");
|
|
|
return false;
|
|
@@ -123,7 +123,7 @@ function SM4() {
|
|
|
this.sm4_setkey(ctx.sk, key);
|
|
|
};
|
|
|
//生成解密密钥
|
|
|
- this.sm4_setkey_dec = function(ctx, key) {
|
|
|
+ this.sm4_setkey_dec = function (ctx, key) {
|
|
|
if (ctx == null) {
|
|
|
Error("ctx is null!");
|
|
|
}
|
|
@@ -139,7 +139,7 @@ function SM4() {
|
|
|
}
|
|
|
|
|
|
|
|
|
- this.sm4_setkey = function(SK, key) {
|
|
|
+ this.sm4_setkey = function (SK, key) {
|
|
|
var MK = new Array(4);
|
|
|
var k = new Array(36);
|
|
|
var i = 0;
|
|
@@ -157,7 +157,7 @@ function SM4() {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
- this.padding = function(input, mode) {
|
|
|
+ this.padding = function (input, mode) {
|
|
|
if (input == null) {
|
|
|
return null;
|
|
|
}
|
|
@@ -174,7 +174,7 @@ function SM4() {
|
|
|
}
|
|
|
return ret;
|
|
|
}
|
|
|
- this.sm4_one_round = function(sk, input, output) {
|
|
|
+ this.sm4_one_round = function (sk, input, output) {
|
|
|
var i = 0;
|
|
|
var ulbuf = new Array(36);
|
|
|
ulbuf[0] = this.GET_ULONG_BE(input, 0);
|
|
@@ -192,7 +192,7 @@ function SM4() {
|
|
|
|
|
|
}
|
|
|
|
|
|
- this.sm4_crypt_ecb = function(ctx, input) {
|
|
|
+ this.sm4_crypt_ecb = function (ctx, input) {
|
|
|
|
|
|
if (input == null) {
|
|
|
alert("input is null!");
|
|
@@ -223,7 +223,7 @@ function SM4() {
|
|
|
return output;
|
|
|
}
|
|
|
|
|
|
- this.sm4_crypt_cbc = function(ctx, iv, input) {
|
|
|
+ this.sm4_crypt_cbc = function (ctx, iv, input) {
|
|
|
if (iv == null || iv.length != 16) {
|
|
|
alert("iv error!");
|
|
|
}
|
|
@@ -291,7 +291,7 @@ function SM4Util() {
|
|
|
this.iv = "";
|
|
|
this.hexString = false;
|
|
|
//加密_ECB
|
|
|
- this.encryptData_ECB = function(plainText) {
|
|
|
+ this.encryptData_ECB = function (plainText) {
|
|
|
try {
|
|
|
var sm4 = new SM4();
|
|
|
var ctx = new SM4_Context();
|
|
@@ -312,23 +312,35 @@ function SM4Util() {
|
|
|
|
|
|
}
|
|
|
//解密_ECB
|
|
|
- this.decryptData_ECB = function(cipherText) {
|
|
|
- try {
|
|
|
- var sm4 = new SM4();
|
|
|
- var ctx = new SM4_Context();
|
|
|
- ctx.isPadding = true;
|
|
|
- ctx.mode = sm4.SM4_ENCRYPT;
|
|
|
- var keyBytes = stringToByte(this.secretKey);
|
|
|
- sm4.sm4_setkey_dec(ctx, keyBytes);
|
|
|
- var decrypted = sm4.sm4_crypt_ecb(ctx, Base64.toUint8Array(cipherText));
|
|
|
- return byteToString(decrypted);
|
|
|
- } catch (e) {
|
|
|
- console.error(e);
|
|
|
- return null;
|
|
|
+ this.decryptData_ECB = function (cipherText) {
|
|
|
+ if (cipherText) {
|
|
|
+ try {
|
|
|
+ var sm4 = new SM4();
|
|
|
+ var ctx = new SM4_Context();
|
|
|
+ ctx.isPadding = true;
|
|
|
+ ctx.mode = sm4.SM4_ENCRYPT;
|
|
|
+ var keyBytes = stringToByte(this.secretKey);
|
|
|
+ sm4.sm4_setkey_dec(ctx, keyBytes);
|
|
|
+ if (typeof cipherText === 'string') {
|
|
|
+ var decrypted = sm4.sm4_crypt_ecb(ctx, Base64.toUint8Array(cipherText));
|
|
|
+ return byteToString(decrypted);
|
|
|
+ } else if (cipherText instanceof Array) {
|
|
|
+ let decrypted = []
|
|
|
+ cipherText.forEach((item) => {
|
|
|
+ let str = sm4.sm4_crypt_ecb(ctx, Base64.toUint8Array(item))
|
|
|
+ decrypted.push(byteToString(str))
|
|
|
+ })
|
|
|
+ return decrypted;
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- this.encryptData_CBC = function(plainText) {
|
|
|
+ this.encryptData_CBC = function (plainText) {
|
|
|
try {
|
|
|
var sm4 = new SM4();
|
|
|
var ctx = new SM4_Context();
|
|
@@ -340,7 +352,7 @@ function SM4Util() {
|
|
|
|
|
|
sm4.sm4_setkey_enc(ctx, keyBytes);
|
|
|
var encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, stringToByte(plainText));
|
|
|
- var cipherText =Base64.fromUint8Array(encrypted);
|
|
|
+ var cipherText = Base64.fromUint8Array(encrypted);
|
|
|
if (cipherText != null && cipherText.trim().length > 0) {
|
|
|
cipherText.replace(/(\s*|\t|\r|\n)/g, "");
|
|
|
}
|
|
@@ -351,7 +363,7 @@ function SM4Util() {
|
|
|
}
|
|
|
}
|
|
|
//解密_CBC
|
|
|
- this.decryptData_CBC = function(cipherText) {
|
|
|
+ this.decryptData_CBC = function (cipherText) {
|
|
|
try {
|
|
|
var sm4 = new SM4();
|
|
|
var ctx = new SM4_Context();
|
|
@@ -393,7 +405,7 @@ function SM4Util() {
|
|
|
return bytes;
|
|
|
}
|
|
|
|
|
|
- function byteToString (arr) {
|
|
|
+ function byteToString(arr) {
|
|
|
if (typeof arr === 'string') {
|
|
|
return arr;
|
|
|
}
|
|
@@ -427,9 +439,8 @@ function getsm4Iv() {
|
|
|
if (token == null || token.length == 0) token = '1234567887654321'
|
|
|
return CryptoJS.MD5(CryptoJS.MD5(token).toString()).toString().toUpperCase().substr(16, 16)
|
|
|
}
|
|
|
-function maskA(str,cd)
|
|
|
-{
|
|
|
- let cdc = str.length-2*cd;
|
|
|
+function maskA(str, cd) {
|
|
|
+ let cdc = str.length - 2 * cd;
|
|
|
if (cdc > 0) {
|
|
|
var reptext = ''
|
|
|
for (let k = 0; k < cdc; k++) {
|
|
@@ -440,88 +451,138 @@ function maskA(str,cd)
|
|
|
else
|
|
|
return str;
|
|
|
}
|
|
|
-function mask(str,type)
|
|
|
-{
|
|
|
+function mask(str, type) {
|
|
|
let strcd = str != null ? str.length : 0;
|
|
|
- if(strcd>0) {
|
|
|
+ if (strcd > 0) {
|
|
|
if (type == 1) //姓名
|
|
|
{
|
|
|
- if (strcd>1)
|
|
|
- {
|
|
|
- let cdc=strcd-1;
|
|
|
+ if (strcd > 1) {
|
|
|
+ let cdc = strcd - 1;
|
|
|
var reptext = ''
|
|
|
for (let k = 0; k < cdc; k++) {
|
|
|
reptext = reptext + '✱'
|
|
|
}
|
|
|
- return reptext+str.substr(cdc)
|
|
|
+ return reptext + str.substr(cdc)
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
+ else {
|
|
|
return str
|
|
|
}
|
|
|
}
|
|
|
else if (type == 2)//身份证号
|
|
|
{
|
|
|
- if (strcd==18)
|
|
|
- {
|
|
|
- return str.substr(0, 3)+'✱✱✱✱✱'+str.substr(8, 1)+'✱✱✱✱✱✱✱'+str.substr(16, 1)+'✱';
|
|
|
+ if (strcd == 18) {
|
|
|
+ return str.substr(0, 3) + '✱✱✱✱✱' + str.substr(8, 1) + '✱✱✱✱✱✱✱' + str.substr(16, 1) + '✱';
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- return maskA(str,1);
|
|
|
+ else {
|
|
|
+ return maskA(str, 1);
|
|
|
}
|
|
|
}
|
|
|
else if (type == 3)//手机号
|
|
|
{
|
|
|
- if (strcd==11)
|
|
|
- {
|
|
|
- return str.substr(0, 2)+'✱✱✱✱✱✱✱✱'+str.substr(10, 1);
|
|
|
+ if (strcd == 11) {
|
|
|
+ return str.substr(0, 2) + '✱✱✱✱✱✱✱✱' + str.substr(10, 1);
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- return maskA(str,2);
|
|
|
+ else {
|
|
|
+ return maskA(str, 2);
|
|
|
}
|
|
|
}
|
|
|
else if (type == 4)//住址
|
|
|
{
|
|
|
- return maskA(str,2);
|
|
|
+ return maskA(str, 2);
|
|
|
}
|
|
|
else if (type == 5)//邮件地址
|
|
|
{
|
|
|
- return maskA(str,2);
|
|
|
+ return maskA(str, 2);
|
|
|
}
|
|
|
else //其他
|
|
|
{
|
|
|
- return maskA(str,1);
|
|
|
+ return maskA(str, 1);
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
+ else {
|
|
|
return str
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+function maskArr(arr,type){
|
|
|
+ let res = []
|
|
|
+ arr.forEach((str) => {
|
|
|
+ let strcd = str != null ? str.length : 0;
|
|
|
+ if (strcd > 0) {
|
|
|
+ if (type == 1) //姓名
|
|
|
+ {
|
|
|
+ if (strcd > 1) {
|
|
|
+ let cdc = strcd - 1;
|
|
|
+ var reptext = ''
|
|
|
+ for (let k = 0; k < cdc; k++) {
|
|
|
+ reptext = reptext + '✱'
|
|
|
+ }
|
|
|
+ res.push(reptext + str.substr(cdc))
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ res.push(str)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (type == 2)//身份证号
|
|
|
+ {
|
|
|
+ if (strcd == 18) {
|
|
|
+ res.push(str.substr(0, 3) + '✱✱✱✱✱' + str.substr(8, 1) + '✱✱✱✱✱✱✱' + str.substr(16, 1) + '✱')
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ res.push(maskA(str, 1))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (type == 3)//手机号
|
|
|
+ {
|
|
|
+ if (strcd == 11) {
|
|
|
+ res.push(str.substr(0, 2) + '✱✱✱✱✱✱✱✱' + str.substr(10, 1))
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ res.push(maskA(str, 2))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (type == 4)//住址
|
|
|
+ {
|
|
|
+ res.push(maskA(str, 2))
|
|
|
+ }
|
|
|
+ else if (type == 5)//邮件地址
|
|
|
+ {
|
|
|
+ res.push(maskA(str, 2))
|
|
|
+ }
|
|
|
+ else //其他
|
|
|
+ {
|
|
|
+ res.push(maskA(str, 1))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ res.push(str)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
export function encrypt_ECB(data) {
|
|
|
var s4 = new SM4Util();
|
|
|
- s4.secretKey=getsm4Key();
|
|
|
- return s4.encryptData_ECB(data);
|
|
|
+ s4.secretKey = getsm4Key();
|
|
|
+ return s4.encryptData_ECB(data);
|
|
|
}
|
|
|
export function decrypt_ECB(data) {
|
|
|
var s4 = new SM4Util();
|
|
|
- s4.secretKey=getsm4Key();
|
|
|
- return s4.decryptData_ECB(data);
|
|
|
+ s4.secretKey = getsm4Key();
|
|
|
+ return s4.decryptData_ECB(data);
|
|
|
}
|
|
|
|
|
|
export function encrypt_CBC(data) {
|
|
|
var s4 = new SM4Util();
|
|
|
- s4.secretKey=getsm4Key();
|
|
|
- s4.iv=getsm4Iv();
|
|
|
- return s4.encryptData_CBC(data);
|
|
|
+ s4.secretKey = getsm4Key();
|
|
|
+ s4.iv = getsm4Iv();
|
|
|
+ return s4.encryptData_CBC(data);
|
|
|
}
|
|
|
export function decrypt_CBC(data) {
|
|
|
var s4 = new SM4Util();
|
|
|
- s4.secretKey=getsm4Key();
|
|
|
- s4.iv=getsm4Iv();
|
|
|
- return s4.decryptData_CBC(data);
|
|
|
+ s4.secretKey = getsm4Key();
|
|
|
+ s4.iv = getsm4Iv();
|
|
|
+ return s4.decryptData_CBC(data);
|
|
|
}
|
|
|
export function decryptData_ECB(rowdata, fields, hides) {
|
|
|
if (rowdata) {
|
|
@@ -530,7 +591,7 @@ export function decryptData_ECB(rowdata, fields, hides) {
|
|
|
for (let j = 0; j < fields.length; j++) {
|
|
|
if (rowdata[fields[j]] != null) {
|
|
|
let zfc = s4.decryptData_ECB(rowdata[fields[j]]);
|
|
|
- rowdata[fields[j]] = mask(zfc, hides[j]);
|
|
|
+ zfc instanceof Array ? rowdata[fields[j]] = maskArr(zfc, hides[j]) : rowdata[fields[j]] = mask(zfc, hides[j])
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -540,7 +601,7 @@ export function decryptData_ECB(rowdata, fields, hides) {
|
|
|
export function decryptRowData_ECB(rowdata, fields, hides) {
|
|
|
if (rowdata && rowdata.length > 0) {
|
|
|
var s4 = new SM4Util();
|
|
|
- s4.secretKey=getsm4Key();
|
|
|
+ s4.secretKey = getsm4Key();
|
|
|
for (let i = 0; i < rowdata.length; i++) {
|
|
|
for (let j = 0; j < fields.length; j++) {
|
|
|
if (rowdata[i][fields[j]] != null) {
|