sHSM/js/mac.js
2024-11-21 17:30:42 +08:00

167 lines
4.4 KiB
JavaScript

var CryptoJS = require("crypto-js");
var fHEX = require("crypto-js/format-hex");
function enc(text, key){
var encrypt = CryptoJS.TripleDES.encrypt(text, key, {
mode: CryptoJS.mode.ECB, // CBC模式
// mode: CryptoJS.mode.ECB, // ECB模式
padding: CryptoJS.pad.NoPadding // padding处理
}
);
var encryptData = encrypt.toString(fHEX); // 加密完成后,转换成字符串
// console.log(encryptData)
return encryptData
}
/**
* int 转 byte
* @param i
*/
function intToByte(i) {
var b = i & 0xFF;
var c = 0;
if (b >= 128) {
c = b % 128;
c = -1 * (128 - c);
} else {
c = b;
}
return c;
}
function stringToHex(str){
var val = "";
for (var i = 0; i < str.length; i++) {
var v = str.charCodeAt(i).toString(16);
if ( v.length < 2 ) {
v = "0" + v;
}
val += v;
}
return val
}
function toHex(d) { return ("0"+(Number(d).toString(16))).slice(-2).toUpperCase() }
function mac( hex ) {
if ( ! /^([A-Fa-f0-9][A-Fa-f0-9])+$/.test( hex ) ) {
throw "hex 格式错误"
}
var checkNum = intToByte(0);
var arrBytes = new Array();
for ( var i = 0 ; i < hex.length; i+=2 ) {
var v = intToByte(parseInt(hex[i] + hex[i+1], 16))
arrBytes.push( v )
checkNum = checkNum ^ v;
}
return toHex( checkNum & 0xFF )
}
function xor(d1, d2, d3) {
if ( 32 !== d1.length || 32 !== d2.length || 32 !== d3.length ) {
throw "输入格式错误"
}
var a1 = hexToBytes(d1);
var a2 = hexToBytes(d2);
var a3 = hexToBytes(d3);
var data = [];
for ( var i = 0; i < 16 ; i++){
data[ i ] = a1[ i ] ^ a2[ i ] ^ a3[ i ]
}
return bytesToHex( data )
}
// Des3
// npm i crypto-js
function hexToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
function bytesToHex(bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
}
return hex.join("");
}
const des = {
CalDES: function (a1, a2) {
a1 = "00000000000000000000000000000000"
a2 = bytesToHex(a2)
// var data = [];
// for ( var i = 0; i < 16 ; i++){
// data[ i ] = a1[ i ] ^ a2[ i ]
// }
// return bytesToHex( data )
var text = CryptoJS.enc.Hex.parse(a1);
var key = CryptoJS.enc.Hex.parse(a2);
return enc(text, key);
}
}
const sm4 = require('./sm4.js')
// 校验值
function checkValue(p1,p2,p3) {
var keyPlaintext = xor(p1, p2, p3)
// 废弃 des 算法
// var ret = des.CalDES(hexToBytes("0000000000000000"), hexToBytes(keyPlaintext))
var ret = sm4.SM4CryptECB("00000000000000000000000000000000", 1, keyPlaintext)
const checkValue = ret.slice(0, 8)
return { checkValue, ret, keyPlaintext }
}
// 校验值
function checkValueKey(l1,l2,l3, protectKey) {
const key = xor(l1,l2,l3)
var ret = sm4.SM4CryptECB("00000000000000000000000000000000", 1, key)
const checkValue = ret.slice(0, 8)
const keyCiphertext = sm4.SM4CryptECB(key, 1 , protectKey)
return { keyCiphertext, checkValue, ret}
}
function decryptionKey(keyCiphertext, checkValue) {
const keyPlaintext = sm4.SM4CryptECB(keyCiphertext, 0, checkValue)
let ret = kcv( keyCiphertext )
const checkData = ret.slice(0, 8)
return { keyPlaintext, checkData }
}
/**
* 计算密钥校验值
* key 是密钥 16 字节的 HEX 格式。
*/
function kcv( key ){
const v = sm4.SM4CryptECB("00000000000000000000000000000000", 1, key)
return v;
}
function decryptKey(value, key){
const v = sm4.SM4CryptECB(value, 0, key)
return v;
}
module.exports = { xor, kcv, decryptKey, checkValue, checkValueKey, decryptionKey }
// const tripledes = require("crypto-js/tripledes");
//
// var value = "123";CryptoJS.enc.Hex.parse("0000000000000000");
// var key = "12345678123456781234567812345678"; CryptoJS.enc.Hex.parse("12345678123456781234567812345678");
//
// var encrypted = tripledes.encrypt(value , key);
// var decrypted = tripledes.decrypt(encrypted, key);
//
// console.log( "encrypted", encrypted.toString('hex') )
// console.log( "decrypted", decrypted )