Compare commits

2 Commits

Author SHA1 Message Date
Michael Micsen Johannessen Wehus
557ba42ea7 Simplified 2023-04-08 00:10:38 +01:00
Michael Micsen Johannessen Wehus
27046c08d5 Started on a 2023-04-07 22:47:31 +01:00
2 changed files with 152 additions and 54 deletions

View File

@@ -3,18 +3,41 @@
<body>
<h2>My First JavaScript</h2>
<h2>Mifare classic converter</h2>
<p>I take absolutely no liabilty for use please verify the files yourself before writing to a card you can end up
locking it</p>
bricking your cards.</p>
<script src="./script.js"></script>
<script>
const blocks = 64 // Number of blocks to rea
let cardSize = 1024 //1k card
let blocks = 64 // 1k card
let filename = Date.now()
function download(filename) {
var element = document.createElement('a');
document.addEventListener("DOMContentLoaded", function (event) {
const nfc = document.getElementById("nfc");
document.getElementById("file-selector").addEventListener("change", (event) => {
console.log(event.target.files[0])
if (event.target.files[0].name.endsWith(".bin")) {
getBinaryFromFile(event.target.files[0]).then((data) => {
filename = event.target.files[0].name.replace(".bin", "")
const hexStr = generateNfcFromBinary(data);
nfc.value = hexStr;
})
} else if (event.target.files[0].name.endsWith(".nfc")) {
getTextFromFile(event.target.files[0]).then((data) => {
filename = event.target.files[0].name.replace(".nfc", "")
nfc.value = data;
})
} else {
alert("Invalid file type");
return
}
});
});
function bin() {
let array = new Array(blocks)
const nfc = document.getElementById("nfc");
const nfc = document.getElementById("nfc");
var text = nfc.value.split("\n");
@@ -33,6 +56,7 @@
}
}
let textHex = ""
console.log("number of blocks", blocks)
for (let i = 0; i < blocks; i++) {
console.log('block', i, array[i])
if (array[i] !== undefined && array[i]?.length == 32) {
@@ -44,59 +68,27 @@
textHex += '00000000000000000000000000000000'
}
}
var filename = Date.now() + ".bin"
console.log(textHex)
var bin = new Array();
for (var i = 0; i < textHex.length / 2; i++) {
var h = textHex.substr(i * 2, 2);
bin[i] = parseInt(h, 16);
}
var byteArray = new Uint8Array(bin);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/octet-stream' }));
a.download = filename;
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
downloadBinFile(textHex, filename + ".bin")
}
document.addEventListener("DOMContentLoaded", function (event) {
const nfc = document.getElementById("nfc");
// Allow for "Upload"
const fileSelector = document.getElementById('file-selector');
fileSelector.addEventListener('change', (event) => {
const fileList = event.target.files;
console.log(fileList);
const reader = new FileReader();
reader.addEventListener('load', (event) => {
console.log(event.target.result)
nfc.value = event.target.result;
});
reader.readAsText(event.target.files[0]);
});
})
function nfc() {
downloadTextFile(document.getElementById("nfc").value, filename + ".nfc")
}
</script>
<button type="button" onclick="download('data.bin');">
Click me to download your file</button>
<p>Paste your .nfc file content here</p>
<br />
<input type="file" id="file-selector" accept=".nfc">
<br />
<textarea id="nfc" rows="20" cols="50"></textarea>
<button type="button" onclick="bin()">
Download .bin
</button>
<button type="button" onclick="nfc()">
Download .nfc
</button>
<p>.nfc file content here</p>
<br />
<input type="file" id="file-selector" accept=".bin,.nfc">
<br />
<textarea id="nfc" rows="20" cols="90"></textarea>
</body>
</html>

106
script.js Normal file
View File

@@ -0,0 +1,106 @@
function downloadBinFile(dataAsHexStr, fileName) {
var bin = new Array();
for (var i = 0; i < dataAsHexStr.length / 2; i++) {
var h = dataAsHexStr.substr(i * 2, 2);
bin[i] = parseInt(h, 16);
}
console.log(dataAsHexStr)
var byteArray = new Uint8Array(bin);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/octet-stream' }));
a.download = fileName;
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
}
function downloadTextFile(text, filename) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function generateNfcFromBinary(data, uidLength = 4) {
const dataLen = data.length;
const uid = data.slice(0, uidLength);
if (dataLen != 1024 && dataLen != 4096) {
alert("Invalid data length");
return;
}
const classicType = (dataLen == 1024) ? "1K" : "4K";
const header =
`#Generated at https://micsen.github.io/flipperNfcToBin/
Filetype: Flipper NFC device
Version: 3
# Nfc device type can be UID, Mifare Ultralight, Mifare Classic
Device type: Mifare Classic
# UID, ATQA and SAK are common for all formats
UID: ${printHex(uid)}
ATQA: ${(uidLength == 4) ? "00 04" : "00 44"}
SAK: 08
# Mifare Classic specific data
Mifare Classic type: ${classicType}
Data format version: 2
# Mifare Classic blocks, '??' means unknown data
`
let dataStr = "";
for (let i = 0; i < data.length; i += 16) {
const block = i / 16
let bytes = printHex(data.slice(i, i + 16))
dataStr += "Block " + block + ": " + bytes + "\n"
}
return header + dataStr;
}
function getBinaryFromFile(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => {
const data = new Uint8Array(e.target.result);
resolve(data);
};
reader.onerror = (e) => {
reject(e);
};
reader.readAsArrayBuffer(file);
});
}
function getTextFromFile(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => {
const data = e.target.result
resolve(data);
};
reader.onerror = (e) => {
reject(e);
};
reader.readAsText(file);
});
}
function printHex(data, lsbFirst = false) {
var hex = "";
for (var i = 0; i < data.length; i++) {
j = (lsbFirst) ? data.length - i - 1 : i;
hex += data[j].toString(16).padStart(2, '0') + " ";
}
return hex.trim();
}