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.

Ai có thuật toán giải mã thì cho em xin với ạ, code js thì càng tốt ạ.

Em cảm ơn!

Giải quyết

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:

Ngôn ngữ JAVA

package com.thiendz.test;

import java.util.HashMap;
import java.util.Map;

public class VietQRDecoder {

    private static final String PREFIX = "000201010212";
    private static final String SUFFIX = "6304";
    private static final String PART_11_PREFIX = "0010A00000072701";
    private static final String PART_2_PREFIX = "5303704";
    private static final String AMOUNT_HEADER = "54";
    private static final String PART_21_PREFIX = "5802VN62";
    private static final int CHECK_SUM_LENGTH = 4;

    public static void main(String[] args) {
        Map<String, String> information = new HashMap<>();
//        String input = "00020101021238540010A00000072701240006970436011010137721710208QRIBFTTA530370454061000005802VN62090805Hello63049F12";
//        String input = "00020101021238560010A0000007270126000697100501121133666688880208QRIBFTTA53037045802VN6304AAAB";
//        String input = "00020101021238580010A000000727012800069704070114190375154450150208QRIBFTTA53037045802VN62120808BTEST001630478FB";
//        String input = "00020101021238580010A000000727012800069704070114190375154450150208QRIBFTTA53037045802VN62120808BTEST00263049629";
        String input = "00020101021238580010A000000727012800069704150114190375154450150208QRIBFTTA5303704540410005802VN6304DDD5";
        String parts = input.substring(
                PREFIX.length(),
                input.length() - SUFFIX.length() - CHECK_SUM_LENGTH
        );
        parts = parts.substring(2);
        int part1Length = Integer.parseInt(parts.substring(0, 2));
        parts = parts.substring(2);
        String part1 = parts.substring(0, part1Length);

        part1 = part1.substring(PART_11_PREFIX.length());
        int part12Length = Integer.parseInt(part1.substring(0, 2));
        part1 = part1.substring(2);
        String part12 = part1.substring(0, part12Length);

        part12 = part12.substring(2);
        int bankIdLength = Integer.parseInt(part12.substring(0, 2));
        part12 = part12.substring(2);
        String bankId = part12.substring(0, bankIdLength);
        information.put("bankId", bankId);

        part12 = part12.substring(bankIdLength);
        part12 = part12.substring(2);
        int bankAccountLength = Integer.parseInt(part12.substring(0, 2));
        part12 = part12.substring(2);
        String bankAccount = part12.substring(0, bankAccountLength);
        information.put("bankAccount", bankAccount);

        parts = parts.substring(part1Length);
        String part2 = parts.substring(PART_2_PREFIX.length());
        information.put("amount", "0");
        if (part2.startsWith(AMOUNT_HEADER)) {
            part2 = part2.substring(2);
            int amountLength = Integer.parseInt(part2.substring(0, 2));
            part2 = part2.substring(2);
            String amount = part2.substring(0, amountLength);
            information.put("amount", amount);
            part2 = part2.substring(amountLength);
        }

        information.put("message", "");
        if (part2.length() - PART_21_PREFIX.length() > PART_21_PREFIX.length()) {
            part2 = part2.substring(PART_21_PREFIX.length());
            int part21Length = Integer.parseInt(part2.substring(0, 2));
            part2 = part2.substring(2);
            String part21 = part2.substring(0, part21Length);
            part21 = part21.substring(2);
            int messageLength = Integer.parseInt(part21.substring(0, 2));
            part21 = part21.substring(2);
            String message = part21.substring(0, messageLength);
            information.put("message", message);
        }

        System.out.println(information);
    }
}

Ngôn ngữ Js

class Qr {
	static PREFIX = '000201010212';
	static SUFFIX = '6304';
	static PART_11_PREFIX = '0010A00000072701';
	static PART_2_PREFIX = '5303704';
	static AMOUNT_HEADER = '54';
	static PART_21_PREFIX = '5802VN62';
	static CHECK_SUM_LENGTH = 4;

	static 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)
		if (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 = '';
		if (part2.length - Qr.PART_21_PREFIX.length > 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);
		}


		return { bankId, bankAccount, amount, message };
	}
}

console.log(Qr.decoder('00020101021238580010A000000727012800069704150114190375154450150208QRIBFTTA5303704540410005802VN6304DDD5'))

  • Answer
Remain: 5
6 Answers
Avatar
No Name Beginner
No Name Beginner
The Best Answer
mình cũng đang cần
  • 0
  • Reply
Avatar
tvd12 Beginner
tvd12 Beginner
Đây là code dịch ngược viết bằng java, em dùng ChatGPT để convert về js giúp anh với nhé, nếu không được thì anh sẽ convert về js giúp em sau:
public class VietQRDecoder {

    private static final String PREFIX = "000201010212";
    private static final String SUFFIX = "6304";
    private static final String PART_11_PREFIX = "0010A00000072701";
    private static final String PART_2_PREFIX = "530370454";
    private static final String PART_21_PREFIX = "5802VN62";
    private static final int CHECK_SUM_LENGTH = 4;

    public static void main(String[] args) {
        Map<String, String> information = new HashMap<>();
        String input = "00020101021238540010A00000072701240006970436011010137721710208QRIBFTTA530370454061000005802VN62090805Hello63049F12";
        String parts = input.substring(
            PREFIX.length(),
            input.length() - SUFFIX.length() - CHECK_SUM_LENGTH
        );
        parts = parts.substring(2);
        int part1Length = Integer.parseInt(parts.substring(0, 2));
        parts = parts.substring(2);
        String part1 = parts.substring(0, part1Length);

        part1 = part1.substring(PART_11_PREFIX.length());
        int part12Length = Integer.parseInt(part1.substring(0, 2));
        part1 = part1.substring(2);
        String part12 = part1.substring(0, part12Length);

        part12 = part12.substring(2);
        int bankIdLength = Integer.parseInt(part12.substring(0, 2));
        part12 = part12.substring(2);
        String bankId = part12.substring(0, bankIdLength);
        information.put("bankId", bankId);

        part12 = part12.substring(bankIdLength);
        part12 = part12.substring(2);
        int bankAccountLength = Integer.parseInt(part12.substring(0, 2));
        part12 = part12.substring(2);
        String bankAccount = part12.substring(0, bankAccountLength);
        information.put("bankAccount", bankAccount);

        parts = parts.substring(part1Length);
        String part2 = parts.substring(PART_2_PREFIX.length());
        int amountLength = Integer.parseInt(part2.substring(0, 2));
        part2 = part2.substring(2);
        String amount = part2.substring(0, amountLength);
        information.put("amount", amount);

        part2 = part2.substring(amountLength);
        part2 = part2.substring(PART_21_PREFIX.length());
        int part21Length = Integer.parseInt(part2.substring(0, 2));
        part2 = part2.substring(2);
        String part21 = part2.substring(0, part21Length);
        part21 = part21.substring(2);
        int messageLength = Integer.parseInt(part21.substring(0, 2));
        part21 = part21.substring(2);
        String message = part21.substring(0, messageLength);
        information.put("message", message);

        System.out.println(information);
    }
}
  • 0
  • Reply
Em đang thử dùng thuật toán của anh với QRCode của VietQR và lỗi java.lang.NumberFormatException: For input string: "BT"

data ảnh trả về 00020101021238580010A000000727012800069704070114190375154450150208QRIBFTTA53037045802VN62120808BTEST001630478FB

mong anh giúp đỡ!

 –  Phạm Huy Thiên 1690361019000
@tvd12  –  Phạm Huy Thiên 1690362468000
Avatar
@tvd12
  • 0
  • Reply
Em có thể cung cấp cho anh các thông tin về số tài khoản, id ngân hàng, tin nhắn, số tiền và các dữ liệu có trong cái text QR kia được không?  –  tvd12 1690361316000
Avatar
Đây là thông tin ngân hàng:

Ngân hàng: techcombank

STK: 19037515445015

Số tiền: 0

Nội dung: BTEST001

compact.png
  • 0
  • Reply
Avatar
tvd12 Beginner
tvd12 Beginner
Em sẽ cần if else các trường hợp không có dữ liệu ở các trường số tiền, message, ví dụ:
public class VietQRDecoderV2 {

    private static final String PREFIX = "000201010212";
    private static final String SUFFIX = "6304";
    private static final String PART_11_PREFIX = "0010A00000072701";
    private static final String PART_2_PREFIX = "5303704";
    private static final String AMOUNT_HEADER = "54";
    private static final String PART_21_PREFIX = "5802VN62";
    private static final int CHECK_SUM_LENGTH = 4;

    public static void main(String[] args) {
        Map<String, String> information = new HashMap<>();
        String input = "00020101021238580010A000000727012800069704070114190375154450150208QRIBFTTA53037045802VN62120808BTEST001630478FB";
        String parts = input.substring(
            PREFIX.length(),
            input.length() - SUFFIX.length() - CHECK_SUM_LENGTH
        );
        parts = parts.substring(2);
        int part1Length = Integer.parseInt(parts.substring(0, 2));
        parts = parts.substring(2);
        String part1 = parts.substring(0, part1Length);

        part1 = part1.substring(PART_11_PREFIX.length());
        int part12Length = Integer.parseInt(part1.substring(0, 2));
        part1 = part1.substring(2);
        String part12 = part1.substring(0, part12Length);

        part12 = part12.substring(2);
        int bankIdLength = Integer.parseInt(part12.substring(0, 2));
        part12 = part12.substring(2);
        String bankId = part12.substring(0, bankIdLength);
        information.put("bankId", bankId);

        part12 = part12.substring(bankIdLength);
        part12 = part12.substring(2);
        int bankAccountLength = Integer.parseInt(part12.substring(0, 2));
        part12 = part12.substring(2);
        String bankAccount = part12.substring(0, bankAccountLength);
        information.put("bankAccount", bankAccount);

        parts = parts.substring(part1Length);
        String part2 = parts.substring(PART_2_PREFIX.length());
        if (part2.startsWith(AMOUNT_HEADER)) {
            part2 = part2.substring(2);
            int amountLength = Integer.parseInt(part2.substring(0, 2));
            part2 = part2.substring(2);
            String amount = part2.substring(0, amountLength);
            information.put("amount", amount);
            part2 = part2.substring(amountLength);
        }
        part2 = part2.substring(PART_21_PREFIX.length());
        int part21Length = Integer.parseInt(part2.substring(0, 2));
        part2 = part2.substring(2);
        String part21 = part2.substring(0, part21Length);
        part21 = part21.substring(2);
        int messageLength = Integer.parseInt(part21.substring(0, 2));
        part21 = part21.substring(2);
        String message = part21.substring(0, messageLength);
        information.put("message", message);

        System.out.println(information);
    }
}
  • 0
  • Reply
Avatar
Cảm ơn Anh @tvd12, dưới là code dịch ngược QRcode VN data sang thông tin ngân hàng, hỗ trợ không có thông tin số tiền và nội dung:

Ngôn ngữ JAVA:

package com.thiendz.test;

import java.util.HashMap;
import java.util.Map;

public class VietQRDecoder {

    private static final String PREFIX = "000201010212";
    private static final String SUFFIX = "6304";
    private static final String PART_11_PREFIX = "0010A00000072701";
    private static final String PART_2_PREFIX = "5303704";
    private static final String AMOUNT_HEADER = "54";
    private static final String PART_21_PREFIX = "5802VN62";
    private static final int CHECK_SUM_LENGTH = 4;

    public static void main(String[] args) {
        Map<String, String> information = new HashMap<>();
//        String input = "00020101021238540010A00000072701240006970436011010137721710208QRIBFTTA530370454061000005802VN62090805Hello63049F12";
//        String input = "00020101021238560010A0000007270126000697100501121133666688880208QRIBFTTA53037045802VN6304AAAB";
//        String input = "00020101021238580010A000000727012800069704070114190375154450150208QRIBFTTA53037045802VN62120808BTEST001630478FB";
//        String input = "00020101021238580010A000000727012800069704070114190375154450150208QRIBFTTA53037045802VN62120808BTEST00263049629";
        String input = "00020101021238580010A000000727012800069704150114190375154450150208QRIBFTTA5303704540410005802VN6304DDD5";
        String parts = input.substring(
                PREFIX.length(),
                input.length() - SUFFIX.length() - CHECK_SUM_LENGTH
        );
        parts = parts.substring(2);
        int part1Length = Integer.parseInt(parts.substring(0, 2));
        parts = parts.substring(2);
        String part1 = parts.substring(0, part1Length);

        part1 = part1.substring(PART_11_PREFIX.length());
        int part12Length = Integer.parseInt(part1.substring(0, 2));
        part1 = part1.substring(2);
        String part12 = part1.substring(0, part12Length);

        part12 = part12.substring(2);
        int bankIdLength = Integer.parseInt(part12.substring(0, 2));
        part12 = part12.substring(2);
        String bankId = part12.substring(0, bankIdLength);
        information.put("bankId", bankId);

        part12 = part12.substring(bankIdLength);
        part12 = part12.substring(2);
        int bankAccountLength = Integer.parseInt(part12.substring(0, 2));
        part12 = part12.substring(2);
        String bankAccount = part12.substring(0, bankAccountLength);
        information.put("bankAccount", bankAccount);

        parts = parts.substring(part1Length);
        String part2 = parts.substring(PART_2_PREFIX.length());
        information.put("amount", "0");
        if (part2.startsWith(AMOUNT_HEADER)) {
            part2 = part2.substring(2);
            int amountLength = Integer.parseInt(part2.substring(0, 2));
            part2 = part2.substring(2);
            String amount = part2.substring(0, amountLength);
            information.put("amount", amount);
            part2 = part2.substring(amountLength);
        }

        information.put("message", "");
        if (part2.length() - PART_21_PREFIX.length() > PART_21_PREFIX.length()) {
            part2 = part2.substring(PART_21_PREFIX.length());
            int part21Length = Integer.parseInt(part2.substring(0, 2));
            part2 = part2.substring(2);
            String part21 = part2.substring(0, part21Length);
            part21 = part21.substring(2);
            int messageLength = Integer.parseInt(part21.substring(0, 2));
            part21 = part21.substring(2);
            String message = part21.substring(0, messageLength);
            information.put("message", message);
        }

        System.out.println(information);
    }
}

Ngôn ngữ Js:

class Qr {
	static PREFIX = '000201010212';
	static SUFFIX = '6304';
	static PART_11_PREFIX = '0010A00000072701';
	static PART_2_PREFIX = '5303704';
	static AMOUNT_HEADER = '54';
	static PART_21_PREFIX = '5802VN62';
	static CHECK_SUM_LENGTH = 4;

	static 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)
		if (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 = '';
		if (part2.length - Qr.PART_21_PREFIX.length > 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);
		}


		return { bankId, bankAccount, amount, message };
	}
}

console.log(Qr.decoder('00020101021238580010A000000727012800069704150114190375154450150208QRIBFTTA5303704540410005802VN6304DDD5'))

  • 1
  • Reply