Phạm Huy Thiên
1 questions
3 answers
Avatar
1
Phạm Huy Thiên Beginner
Dịch ngược data QRcode VN về thông tin ngân hàng
Dạo gần đây em có thấy anh Dũng chia sẻ về thư viện generate data tạo QR code, nhưng không thấy có thư viện nào dịch ngược lại từ data QR code ra thông tin của ngân hàng.<p> Ai có thuật toán giải mã thì cho em xin với ạ, code js thì càng tốt ạ. </p> <p> Em cảm ơn! </p> <h1><strong>Giải quyết</strong></h1> Cảm ơn anh @tdv12, đoạn code sau hỗ trợ dịch ngược QRcode sang thông tin ngân hàng, có hỗ trợ không có thông tin số tiền và nội dung chuyển tiền:<p> Ngôn ngữ <code class="markdown-inline">JAVA</code> </p> <div class="markdown-block position-relative overflow-auto source-java"> <pre> <span class="pl-k">package</span> com.thiendz.test; <span class="pl-k">import</span> java.util.HashMap; <span class="pl-k">import</span> java.util.Map; <span class="pl-k">public</span> <span class="pl-k">class</span> VietQRDecoder { private <span class="pl-k">static</span> <span class="pl-k">final</span> String PREFIX = <span class="pl-s">"000201010212"</span>; private <span class="pl-k">static</span> <span class="pl-k">final</span> String SUFFIX = <span class="pl-s">"6304"</span>; private <span class="pl-k">static</span> <span class="pl-k">final</span> String PART_11_PREFIX = <span class="pl-s">"0010A00000072701"</span>; private <span class="pl-k">static</span> <span class="pl-k">final</span> String PART_2_PREFIX = <span class="pl-s">"5303704"</span>; private <span class="pl-k">static</span> <span class="pl-k">final</span> String AMOUNT_HEADER = <span class="pl-s">"54"</span>; private <span class="pl-k">static</span> <span class="pl-k">final</span> String PART_21_PREFIX = <span class="pl-s">"5802VN62"</span>; private <span class="pl-k">static</span> <span class="pl-k">final</span> <span class="pl-k">int</span> CHECK_SUM_LENGTH = 4; <span class="pl-k">public</span> <span class="pl-k">static</span> <span class="pl-k">void</span> main(String[] args) { Map&lt;String, String&gt; information = <span class="pl-k">new</span> HashMap&lt;&gt;(); // String input = <span class="pl-s">"00020101021238540010A00000072701240006970436011010137721710208QRIBFTTA530370454061000005802VN62090805Hello63049F12"</span>; // String input = <span class="pl-s">"00020101021238560010A0000007270126000697100501121133666688880208QRIBFTTA53037045802VN6304AAAB"</span>; // String input = <span class="pl-s">"00020101021238580010A000000727012800069704070114190375154450150208QRIBFTTA53037045802VN62120808BTEST001630478FB"</span>; // String input = <span class="pl-s">"00020101021238580010A000000727012800069704070114190375154450150208QRIBFTTA53037045802VN62120808BTEST00263049629"</span>; String input = <span class="pl-s">"00020101021238580010A000000727012800069704150114190375154450150208QRIBFTTA5303704540410005802VN6304DDD5"</span>; String parts = input.substring( PREFIX.length(), input.length() - SUFFIX.length() - CHECK_SUM_LENGTH ); parts = parts.substring(2); <span class="pl-k">int</span> part1Length = Integer.parseInt(parts.substring(0, 2)); parts = parts.substring(2); String part1 = parts.substring(0, part1Length); part1 = part1.substring(PART_11_PREFIX.length()); <span class="pl-k">int</span> part12Length = Integer.parseInt(part1.substring(0, 2)); part1 = part1.substring(2); String part12 = part1.substring(0, part12Length); part12 = part12.substring(2); <span class="pl-k">int</span> bankIdLength = Integer.parseInt(part12.substring(0, 2)); part12 = part12.substring(2); String bankId = part12.substring(0, bankIdLength); information.put(<span class="pl-s">"bankId"</span>, bankId); part12 = part12.substring(bankIdLength); part12 = part12.substring(2); <span class="pl-k">int</span> bankAccountLength = Integer.parseInt(part12.substring(0, 2)); part12 = part12.substring(2); String bankAccount = part12.substring(0, bankAccountLength); information.put(<span class="pl-s">"bankAccount"</span>, bankAccount); parts = parts.substring(part1Length); String part2 = parts.substring(PART_2_PREFIX.length()); information.put(<span class="pl-s">"amount"</span>, <span class="pl-s">"0"</span>); <span class="pl-k">if</span> (part2.startsWith(AMOUNT_HEADER)) { part2 = part2.substring(2); <span class="pl-k">int</span> amountLength = Integer.parseInt(part2.substring(0, 2)); part2 = part2.substring(2); String amount = part2.substring(0, amountLength); information.put(<span class="pl-s">"amount"</span>, amount); part2 = part2.substring(amountLength); } information.put(<span class="pl-s">"message"</span>, <span class="pl-s">""</span>); <span class="pl-k">if</span> (part2.length() - PART_21_PREFIX.length() &gt; PART_21_PREFIX.length()) { part2 = part2.substring(PART_21_PREFIX.length()); <span class="pl-k">int</span> part21Length = Integer.parseInt(part2.substring(0, 2)); part2 = part2.substring(2); String part21 = part2.substring(0, part21Length); part21 = part21.substring(2); <span class="pl-k">int</span> messageLength = Integer.parseInt(part21.substring(0, 2)); part21 = part21.substring(2); String message = part21.substring(0, messageLength); information.put(<span class="pl-s">"message"</span>, message); } System.out.println(information); } } </pre> </div><p> Ngôn ngữ <code class="markdown-inline">Js</code> </p> <div class="markdown-block position-relative overflow-auto source-javascript"> <pre> class Qr { <span class="pl-k">static</span> PREFIX = <span class="pl-s">'000201010212'</span>; <span class="pl-k">static</span> SUFFIX = <span class="pl-s">'6304'</span>; <span class="pl-k">static</span> PART_11_PREFIX = <span class="pl-s">'0010A00000072701'</span>; <span class="pl-k">static</span> PART_2_PREFIX = <span class="pl-s">'5303704'</span>; <span class="pl-k">static</span> AMOUNT_HEADER = <span class="pl-s">'54'</span>; <span class="pl-k">static</span> PART_21_PREFIX = <span class="pl-s">'5802VN62'</span>; <span class="pl-k">static</span> CHECK_SUM_LENGTH = 4; <span class="pl-k">static</span> decoder(input) { let parts = input.substring( Qr.PREFIX.length, input.length - Qr.SUFFIX.length - Qr.CHECK_SUM_LENGTH ); parts = parts.substring(2); let part1Length = parseInt(parts.substring(0, 2)); parts = parts.substring(2); let part1 = parts.substring(0, part1Length); part1 = part1.substring(Qr.PART_11_PREFIX.length); let part12Length = parseInt(part1.substring(0, 2)); part1 = part1.substring(2); let part12 = part1.substring(0, part12Length); part12 = part12.substring(2); let bankIdLength = parseInt(part12.substring(0, 2)); part12 = part12.substring(2); let bankId = part12.substring(0, bankIdLength); part12 = part12.substring(bankIdLength); part12 = part12.substring(2); let bankAccountLength = parseInt(part12.substring(0, 2)); part12 = part12.substring(2); let bankAccount = part12.substring(0, bankAccountLength); let amount = 0; parts = parts.substring(part1Length); let part2 = parts.substring(Qr.PART_2_PREFIX.length); console.log(part2) <span class="pl-k">if</span> (part2.startsWith(Qr.AMOUNT_HEADER)) { part2 = part2.substring(2); let amountLength = parseInt(part2.substring(0, 2)); part2 = part2.substring(2); amount = part2.substring(0, amountLength); part2 = part2.substring(amountLength); } let message = <span class="pl-s">''</span>; <span class="pl-k">if</span> (part2.length - Qr.PART_21_PREFIX.length &gt; Qr.PART_21_PREFIX.length) { part2 = part2.substring(Qr.PART_21_PREFIX.length); let part21Length = parseInt(part2.substring(0, 2)); part2 = part2.substring(2); let part21 = part2.substring(0, part21Length); part21 = part21.substring(2); let messageLength = parseInt(part21.substring(0, 2)); part21 = part21.substring(2); message = part21.substring(0, messageLength); } <span class="pl-k">return</span> { bankId, bankAccount, amount, message }; } } console.log(Qr.decoder(<span class="pl-s">'00020101021238580010A000000727012800069704150114190375154450150208QRIBFTTA5303704540410005802VN6304DDD5'</span>)) </pre> </div>
Answer