-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrailer Block Helper.html
516 lines (424 loc) · 18.1 KB
/
Trailer Block Helper.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<html>
<header>
<title>Trailer Decoder-Encoder</title>
<style>
:root { --main: #2ea7b3; --secondary: #e5f7ff; --select: #d4e6ee; --background: ghostWhite }
* { font-family: roboto; }
body { background-color: var(--background); text-align: -webkit-center;}
th { background-color: var(--main); }
table, .search div, .search { padding: 2px; border-radius: 5px;}
table.elem:hover { background-color: var(--main); }
table.elem td { background-color: var(--secondary);}
.subtitle {
color: var(--main);
font-weight: bolder;
font-size: small;
}
button, input{
width: 120px;
margin: 5px 0px 5px 0px;
height: 40px;
display: inline-block;
}
.search div:hover { background-color: var(--select); }
.search div { cursor: pointer; }
.search, table.elem td { border: solid var(--main) 1px; }
.search {
display: none;
background-color: var(--background);
min-height: 47px;
max-height: 162px;
position: absolute;
min-width: 240px;
box-shadow: 0 0 5px 0px var(--main);
}
#console, th { color: white; }
#console, td, tr, th { padding: 10px; border-radius: 5px;}
#console, .search { overflow-y: scroll; font-family: "Lucida Sans Typewriter"; }
#console {
width: 200px;
height: 430px;
overflow-y: scroll;
background-color: var(--main);
}
</style>
</header>
<body>
<!---------------------------- ROW 1------------------------------------>
<table><tr><td>
<table class="elem">
<tr><th>
Access Bytes Decoder
</th></tr>
<tr><td>
<input name="inputhex" id="inputhex" placeholder="Coded HEX"></input>
<button onclick="decodehex()">Decode HEX</button>
</td></tr>
</table>
</td><td>
<h1>Trailer Block <br> Decoder-Encoder</h1>
</td><td rowspan="2">
<table class="elem">
<tr><th>
Access Bytes Encoder
</th></tr>
<tr><td style="height: 430px;">
<div class="subtitle">Trailer Access Bits:</div>
<input oninput="encodehex()" type="number" name="inputtrailerbits" id="inputtrailerbits" placeholder="C1-C2-C3"></input>
<button onclick="decodebits(3)">Decode</button>
<div class="subtitle">Data 1 Access Bits:</div>
<input oninput="encodehex()" type="number" name="inputdatabits1" id="inputdatabits1" placeholder="C1-C2-C3"></input>
<button onclick="decodebits(0)">Decode</button>
<div class="subtitle">Data 2 Access Bits:</div>
<input oninput="encodehex()" type="number" name="inputdatabits2" id="inputdatabits2" placeholder="C1-C2-C3"></input>
<button onclick="decodebits(1)">Decode</button>
<div class="subtitle">Data 3 Access Bits:</div>
<input oninput="encodehex()" type="number" name="inputdatabits3" id="inputdatabits3" placeholder="C1-C2-C3"></input>
<button onclick="decodebits(2)">Decode</button>
<br><br><br><br><br>
<div class="subtitle">Result (HEX):</div>
<input name="outputbytesencoder" id="outputbytesencoder" placeholder="-- -- -- --" disabled></input>
</td></tr>
</table>
<!---------------------------- ROW 2------------------------------------>
</td></tr><tr><td>
<table class="elem">
<tr><th>
Trailer Access Bits Generator
</th></tr>
<tr><td style="height: 280px;">
<div class="subtitle">Key A (read-write):</div>
<input name="inputreadkeya" id="inputreadkeya" onclick="readkeya.showOptions()" placeholder="Read With.."></input>
<div class="search" id="optionsreadkeya"></div>
<input name="inputwritekeya" id="inputwritekeya" onclick="writekeya.showOptions()" placeholder="Write With.."></input>
<div class="search" id="optionswritekeya"></div>
<br>
<div class="subtitle">Access Bytes (read-write):</div>
<input name="inputreadaccess" id="inputreadaccess" onclick="readaccess.showOptions()" placeholder="Read With.."></input>
<div class="search" id="optionsreadaccess"></div>
<input name="inputwriteaccess" id="inputwriteaccess" onclick="writeaccess.showOptions()" placeholder="Write With.."></input>
<div class="search" id="optionswriteaccess"></div>
<br>
<div class="subtitle">Key B (read-write):</div>
<input name="inputreadkeyb" id="inputreadkeyb" onclick="readkeyb.showOptions()" placeholder="Read With.."></input>
<div class="search" id="optionsreadkeyb"></div>
<input name="inputwritekeyb" id="inputwritekeyb" onclick="writekeyb.showOptions()" placeholder="Write With.."></input>
<div class="search" id="optionswritekeyb"></div>
<br>
<div class="subtitle">Result (BIN):</div>
<input name="outputtrailergenerator" id="outputtrailergenerator" placeholder="C1-C2-C3" disabled></input>
</td></tr>
</table>
</td><td>
<table class="elem">
<tr><th>
Data Access Bits Generator
</th></tr>
<tr><td style="height: 280px;">
<div class="subtitle">Data Block (read-write):</div>
<input name="inputreaddata" id="inputreaddata" onclick="readdata.showOptions()" placeholder="Read With.."></input>
<div class="search" id="optionsreaddata"></div>
<input name="inputwritedata" id="inputwritedata" onclick="writedata.showOptions()" placeholder="Write With.."></input>
<div class="search" id="optionswritedata"></div>
<br>
<div class="subtitle">For Value Card (increment-decrement):</div>
<input name="inputincrementdata" id="inputincrementdata" onclick="incrementdata.showOptions()" placeholder="Increment With.."></input>
<div class="search" id="optionsincrementdata"></div>
<input name="inputdecrementdata" id="inputdecrementdata" onclick="decrementdata.showOptions()" placeholder="Decrement With.."></input>
<div class="search" id="optionsdecrementdata"></div>
<br>
<br><br><br>
<div class="subtitle">Result (BIN):</div>
<input name="outputdatagenerator" id="outputdatagenerator" placeholder="C1-C2-C3" disabled></input>
</td></tr>
</table>
</td><td>
</td></tr></table>
<h2><b>Decode</b> and <b>Encode</b> the access bytes of a <a href="https://www.amazon.com/s?k=rfid+card&ref=nav_bb_sb">RFID card</a></h2>
<p>The trailer block of a RFID card is where the configurations are. The access bytes are located at the seventh-to-tenth byte of the fourth block of any sector, as seen in the image below.</p>
<img src="https://raw.githubusercontent.com/dip000/RFID-card-encoder-and-decoder/main/RFID%20CARD%20Memalloc.png"></img>
<p>You can reconfigure the access by changing the Access Bit Generators and then copy-paste the resulting bits in the Access Bit Encoder</p>
<p>Note that not all combinations are possible, you can see the possible configurations in the datasheet found <a href="https://www.nxp.com/docs/en/data-sheet/MF1S50YYX_V1.pdf">HERE</a> in page 11 to 13</p>
<br><br>
<body>
<script>
var ielements = {}
var inames = ["inputreadkeya","optionsreadkeya",
"inputwritekeya","optionswritekeya",
"inputreadaccess","optionsreadaccess",
"inputwriteaccess","optionswriteaccess",
"inputreadkeyb","optionsreadkeyb",
"inputwritekeyb","optionswritekeyb",
"outputtrailergenerator",
"inputreaddata","optionsreaddata",
"inputwritedata","optionswritedata",
"inputincrementdata","optionsincrementdata",
"inputdecrementdata","optionsdecrementdata",
"outputdatagenerator",
"inputtrailerbits",
"inputdatabits1","inputdatabits2","inputdatabits3",
"outputbytesencoder",
"inputhex"
];
window.onload = function(){
for(let i in inames) ielements[inames[i]] = document.getElementById( inames[i] );
readkeya = new InputAndOptionsTrailer( ielements.inputreadkeya, ielements.optionsreadkeya, ["Never"]);
writekeya = new InputAndOptionsTrailer( ielements.inputwritekeya, ielements.optionswritekeya, ["Key A", "Key B", "Never"]);
readaccess = new InputAndOptionsTrailer( ielements.inputreadaccess, ielements.optionsreadaccess, ["Key A", "Key A or B"]);
writeaccess = new InputAndOptionsTrailer( ielements.inputwriteaccess, ielements.optionswriteaccess, ["Never", "Key A", "Key B"]);
readkeyb = new InputAndOptionsTrailer( ielements.inputreadkeyb, ielements.optionsreadkeyb, ["Key A", "Never"]);
writekeyb = new InputAndOptionsTrailer( ielements.inputwritekeyb, ielements.optionswritekeyb, ["Key A", "Never", "Key B"]);
readdata = new InputAndOptionsData( ielements.inputreaddata, ielements.optionsreaddata, ["Key A or B", "Key B", "Never"]);
writedata = new InputAndOptionsData( ielements.inputwritedata, ielements.optionswritedata, ["Key A or B", "Never", "Key B"]);
incrementdata = new InputAndOptionsData( ielements.inputincrementdata, ielements.optionsincrementdata, ["Key A or B", "Never", "Key B"]);
decrementdata = new InputAndOptionsData( ielements.inputdecrementdata, ielements.optionsdecrementdata, ["Key A or B", "Never"]);
}
function decodebits(block){
const DATA1=0,DATA2=1,DATA3=2,TRAILER=3;
let config = [];
if(block == TRAILER){
config = InputAndOptionsTrailer.accesstable[ ielements.inputtrailerbits.value ];
printtrailer(config);
updateAccessResult(InputAndOptionsTrailer.updateInputsaccess(), InputAndOptionsTrailer.accesstable, ielements.outputtrailergenerator);
}
else{
switch(block){
case DATA1:
config = InputAndOptionsData.accesstable[ ielements.inputdatabits1.value ];
break;
case DATA2:
config = InputAndOptionsData.accesstable[ ielements.inputdatabits2.value ];
break;
case DATA3:
config = InputAndOptionsData.accesstable[ ielements.inputdatabits3.value ];
break;
default: break;
}
printdata(config);
updateAccessResult(InputAndOptionsData.updateInputsaccess(), InputAndOptionsData.accesstable, ielements.outputdatagenerator);
}
}
function printtrailer(config){
ielements.inputreadkeya.value = config[0];
ielements.inputwritekeya.value = config[1];
ielements.inputreadaccess.value = config[2];
ielements.inputwriteaccess.value = config[3];
ielements.inputreadkeyb.value = config[4];
ielements.inputwritekeyb.value = config[5];
}
function printdata(config){
ielements.inputreaddata.value = config[0];
ielements.inputwritedata.value = config[1];
ielements.inputincrementdata.value = config[2];
ielements.inputdecrementdata.value = config[3];
}
function decodehex(){
//Decoding definitions
let inp = ielements.inputhex.value;
const accesspattern = [2, 0, 4];
const blockpattern = [3, 2, 1, 0];
const invertpattern = [0, 1, 0];
const DATA1=0,DATA2=1,DATA3=2,TRAILER=3;
const DATA = [0,1,2];
let blocksaccess = [];
//Decode
for( i in blockpattern){
let blockaccess = "";
for( j in accesspattern){
blockaccess += ((parseInt(inp[ accesspattern[j] ], 16) >> blockpattern[i]) & 0x01) ^ invertpattern[j];
}
blocksaccess[ blockpattern[i] ] = blockaccess;
}
//Trailer Block. Show and save TRAILER
let config = InputAndOptionsTrailer.accesstable[ blocksaccess[TRAILER] ];
console.log(blocksaccess);
console.log(config);
printtrailer(config);
ielements.inputtrailerbits.value = blocksaccess[TRAILER];
updateAccessResult(InputAndOptionsTrailer.updateInputsaccess(), InputAndOptionsTrailer.accesstable, ielements.outputtrailergenerator);
//Data blocks
//Show DATA1 only. DATA1, DATA2 and DATA3 are saved in inputdatabitsX
config = InputAndOptionsData.accesstable[ blocksaccess[DATA1] ];
console.log( config );
printdata(config)
ielements.inputdatabits1.value = blocksaccess[DATA1];
ielements.inputdatabits2.value = blocksaccess[DATA2];
ielements.inputdatabits3.value = blocksaccess[DATA3];
updateAccessResult(InputAndOptionsData.updateInputsaccess(), InputAndOptionsData.accesstable, ielements.outputdatagenerator);
encodehex();
}
function encodehex(){
//Input Data Formating. HEX Strings to int array array
traileranddatabits = getTrailerAndDatabits();
if(!traileranddatabits) return;
console.log(traileranddatabits);
//Pattern definitions
const accesspattern = [1,0,0,2,2,1];
const blockspattern = [3,2,1,0];
const invertpattern = [1, 1, 0, 1, 0, 0];
const halfbyte = 4;
const totalhalfbytes = 6;
const lastbyte = "69";
let accessbytes = [];
// Decode
for(let i=0; i<totalhalfbytes; i++){
let accessbits = [];
for(let j=0; j<halfbyte; j++){
//Weirdest line:
// 1. block is the current data or trailer block, in datasheet is decoded as [trailer, data3, data2, data1] = [[C1,C2,C3],[C1,C2,C3],[C1,C2,C3],[C1,C2,C3]]
// 2. The access bit [C1,C2,C3] of the block has a pattern: blockpattern
// 3. The encoding also has bit inversions of invertpattern. The XOR1 operation (^1) inverts the bit while XOR0 (^0) does nothing
let block = traileranddatabits[ blockspattern[j] ];
accessbits[j] = block[ accesspattern[i] ] ^ invertpattern[i];
}
accessbytes[i] = accessbits;
}
console.log("BITS: ",accessbytes);
//Output Data Formating. int array array to bit string
let result = '';
for(let i=0; i<totalhalfbytes; i++){
result += bitstohex( accessbytes[i] ).toString();
}
ielements.outputbytesencoder.value = result + lastbyte;
}
function getTrailerAndDatabits(){
//Trailer block
trailerbits = stringtobits( ielements.inputtrailerbits.value );
if(trailerbits.length < 3){
ielements.outputbytesencoder.value = "-- -- -- --";
return false;
}
//Data blocks
let databits = [ielements.inputdatabits1.value,
ielements.inputdatabits2.value,
ielements.inputdatabits3.value];
for(i in databits){
databits[i] = stringtobits( databits[i] );
if(databits[i].length < 3){
ielements.outputbytesencoder.value = "-- -- -- --";
return false;
}
}
return [databits[0], databits[1], databits[2], trailerbits];
}
function stringtobits(string){
let value=[];
for(let i in string){
value[i] = parseInt(string[i]);
}
return value;
}
function bitstohex(bits){
let value=0;
for(let i in bits){
value = (value << 1) | bits[i];
}
return value.toString(16).toUpperCase();;
}
class InputAndOptionsTrailer{
constructor(input, options, elements){
this.input = input;
this.options = options;
this.elements = elements;
generatePrintableOptions(input, options, elements);
generateAutoCloser(input, options,
InputAndOptionsTrailer.updateInputsaccess,
InputAndOptionsTrailer.accesstable,
ielements.outputtrailergenerator );
}
static accesstable = {"000": ["Never","Key A","Key A","Never","Key A","Key A"],
"010": ["Never","Never","Key A","Never","Key A","Never"],
"100": ["Never","Key B","Key A or B","Never","Never","Key B"],
"110": ["Never","Never","Key A or B","Never","Never","Never"],
"001": ["Never","Key A","Key A","Key A","Key A","Key A"],
"011": ["Never","Key B","Key A or B","Key B","Never","Key B"],
"101": ["Never","Never","Key A or B","Key B","Never","Never"],
"111": ["Never","Never","Key A or B","Never","Never","Never"]
};
static updateInputsaccess = function(){
return [ielements.inputreadkeya.value,
ielements.inputwritekeya.value,
ielements.inputreadaccess.value,
ielements.inputwriteaccess.value,
ielements.inputreadkeyb.value,
ielements.inputwritekeyb.value
];
}
showOptions = function(){
this.options.style.display = "block";
}
}
class InputAndOptionsData{
constructor(input, options, elements){
this.input = input;
this.options = options;
this.elements = elements;
generatePrintableOptions(input, options, elements);
generateAutoCloser(input, options,
InputAndOptionsData.updateInputsaccess,
InputAndOptionsData.accesstable,
ielements.outputdatagenerator );
}
static accesstable = {"000": ["Key A or B","Key A or B","Key A or B","Key A or B"],
"010": ["Key A or B","Never","Never","Never"],
"100": ["Key A or B","Key B","Never","Never"],
"110": ["Key A or B","Key B","Key B","Key A or B"],
"001": ["Key A or B","Never","Never","Key A or B"],
"011": ["Key B","Key B","Never","Never"],
"101": ["Key B","Never","Never","Never"],
"111": ["Never","Never","Never","Never"]
};
static updateInputsaccess = function(){
return [
ielements.inputreaddata.value,
ielements.inputwritedata.value,
ielements.inputincrementdata.value,
ielements.inputdecrementdata.value,
];
}
showOptions = function(){
this.options.style.display = "block";
}
}
function updateAccessResult(inputaccess, resultsTable, ioutput){
let output = "";
let keys = Object.keys( resultsTable );
for(let i in keys){
let key = keys[i];
let values = Object.values( resultsTable )[i];
if( inputaccess[i] === "" ) break;
console.log(key, values);
for(let j in values){
let input = inputaccess[j];
let value = values[j];
console.log("j: "+j+"; key: " + key + "; input: " + input + "; value: " + value);
if( input != value ) break;
if(j >= values.length-1) {
output = key;
console.log("-- FOUND MATCH: "+key+" ---");
}
}
}
ioutput.value = output;
}
function generateAutoCloser(input, options, inputaccess, resultsTable, ioutput){
input.addEventListener("focusout", function(){
setTimeout(function(){
options.style.display = "none";
updateAccessResult(inputaccess(), resultsTable, ioutput);
}, 200);
});
}
function generatePrintableOptions(input, options, elements){
for(let i=0; i<elements.length; i++ ){
let row = elements[i];
let elem = document.createElement("div");
elem.innerText = row;
options.appendChild(elem);
elem.addEventListener("click", function(){
input.value = elements[i];
} )
}
}
</script>
</html>