2023-01-10 02:20:52 +00:00
|
|
|
/**
|
|
|
|
* 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 )
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log( mac("FFFF010203EEEECCCC7777223311") )
|
|
|
|
|
|
|
|
|
|
|
|
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 )
|
|
|
|
}
|
|
|
|
|
|
|
|
var r = xor("11111111111111111111111111111111", "22222222222222222222222222222222", "33333333333333333333333333333333")
|
|
|
|
console.log( r )
|
|
|
|
|
|
|
|
// 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("");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-09 05:57:49 +00:00
|
|
|
// const DES = require('./jssrc/des.js')
|
|
|
|
// var des = new DES();
|
|
|
|
// var key = hexToBytes("12345678123456781234567812345678")
|
|
|
|
// var ret = des.CalDES(hexToBytes("00000000000000000000000000000000"), key);
|
|
|
|
// console.log( ret )
|
2023-01-11 07:32:36 +00:00
|
|
|
//
|
2023-08-09 05:57:49 +00:00
|
|
|
// des = new DES();
|
|
|
|
// key = hexToBytes("12345678123456781234567812345670")
|
|
|
|
// ret = des.CalDES(hexToBytes("00000000000000000000000000000000"), key);
|
2023-01-11 07:32:36 +00:00
|
|
|
// console.log( ret )
|
|
|
|
//
|
2023-08-09 05:57:49 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
const sm4 = require('./jssrc/sm4.js')
|
|
|
|
var ret = sm4.SM4CryptECB("00000000000000000000000000000000", 1 , "11111111111111111111111111111111")
|
|
|
|
console.log( ret )
|
|
|
|
|
2023-01-10 02:20:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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 )
|