Android Example Password Protected QR Codes - Creating and Scanning Password Protected QR Codes

I am going to do this example in (at least) two posts. @BarryW has asked me about password protected QR codes - so, here we go.

The attached B4A project has two classes (all with the code inside them):
1. Class QRcode - this class will create QR codes and store the generated QR code in your device's default /pictures folder. From here it is loaded into an Imageview that is being displayed by the B4A project.
2. Class AESCryptUtilities - this class encrypts the message that you want to be within your QR code. The call to the class requires you to pass a PASSWORD (I have used "Password1" as the password in the attached project") as well as the message to be encrypted (I have used "B4A is a great tool for developing android apps!" as the message to be encrypted).

Once the password and message have been processed by class AESCryptUtilities, the latter will return an encrypted string to the main activity. In this case the returned encrypted string looks as follows:

mnfAFz8apesI3yfJVIYIJJEM51sbklRecNyuhGBtGTxkI7ahQ5HheG2FEPz3L67aCuQXmpgseSSW1vzgWAvcQA==

I then prepended this encrypted string with "PWD:" and passed the following string to the QRCode class:

PWD:mnfAFz8apesI3yfJVIYIJJEM51sbklRecNyuhGBtGTxkI7ahQ5HheG2FEPz3L67aCuQXmpgseSSW1vzgWAvcQA==

This string is then converted into a QR Code and displayed in the ImageView. The "Lock" is a transparent ImageView that overlays the displayed (generated) QR Code - just to give an indicaton that this QR code is encrypted/locked. You can add your own code to combine the two images into a single png/jpg files - search the forum for how to do it.

Why the "PWD:"? Just an example of how I will use it with the scanner (post to follow) to know that this is a password protected QR Code. A QR Code scanner that asks for a password is a very purpose designed scanner that looks for some very special "marks" in the returned string to know it should prompt for a password in order to decrypt the message within the QR Code. You can do it whichever way you want - you can add the password to your code or you can even add the password to the encrypted string one the message has been encrypted by spreading the characters of the password throughout the encrypted message where your code will know where to "pick" out and assemble the password and compare it with the password(s) within your code to enable decryption of the message (you will have to remove the password characters from the returned (encrypted) string in order to pass the original (i.e pre modified) encrypted string to AESCryptUtilities (along with the password) to be decrypted.

So, if you scan the below QR Code you should see the message as:
PWD:mnfAFz8apesI3yfJVIYIJJEM51sbklRecNyuhGBtGTxkI7ahQ5HheG2FEPz3L67aCuQXmpgseSSW1vzgWAvcQA==

1.png


For those that want a head start - modify the code in THIS PROJECT (CLICK HERE) to accommodate decoding of your encrypted QR Codes once scanned. The latest library files are in post #6 of the thread.

You will have to add class AESCryptUtilities to the QR Code scanning project and strip off "PWD:" from the scanned message before passing the encrypted string to AESCryptUtilities to be decrypted (along with the password that you should prompt the user for. Use "PWD:" from the returned scanned message to know that this is a password protected QR Code.

The library files that the attached project makes use of are all in the /Files folder of the B4A project. Copy them to your additional library folder should you not already have them.

Note: In Sub DrawQRCode of the attached project you can set the masking pattern that should be applied to the QR Code as well as the Error Correction Level. Seeing that I have overlayed the image of the lock to the generated QR Code I have used Error Correction Level "H" as it will allow the scanner to decode the QR code much easier due to the abundance of error correction words within the generated QR Code that level H provides for.

B4X:
Sub DrawQRCode
    shape = "s"
    mask_pattern = 3           
    err = "H"
 
    QRcode.Draw_QR_Code(messageQR,err,mask_pattern,Colors.White,Colors.Black,shape)    'Pass the message, the error level, the masking pattern, the background color, and the foreground/pixel color

End Sub

Post #2 (the QR Code scanner that will decode the Password Protected QR Code) to follow in this week.

Enjoy...:)
 

Attachments

  • PasswordQRcodes.zip
    480.3 KB · Views: 1,903
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
This is the scanner part of the project that will handle the password protected QR Code that we created in post #1 above. Posting the following:

1. B4A project
2. 3 x zipped files that contain library files. Copy all but the JavaObject jar and xml to your additional library folder.
3. Copy the JavaObject jar and xml to your B4A main library folder (only including them in case you don't have the latest version of JavaObject)

The project makes use a number of classes that makes use of inline Java code. So, you need to have a recent B4A version that supports inline Java code. The inline Java code classes are the following:

1. AESCryptUtilities (encryption and decryption)
2. StringUtilities (very few of the methods in this calls are used in the attached project. It is a class that I have put together some time ago for my own use)
3. ToastUtilities - it will display a toast message when a successfully decryption took place
4. TTSUtilities - TextToSpeech i.e the attached project will talk to you on various occassions!

So here is the recipe to use the attached B4A project:
1. Start the app
2. Press on the button Start Scan
3. Wait for the TextToSpeech message to finish
4. Point the scanner at the QR Code in post #1 above and scan it.
5. Once the QR Code was successfully scanned it will stop the scanner and ask you to enter the password to decrypt the encrypted message within the QR Code - look at the retuned (encrypted) QR code message at the bottom of the display.
6. Type in anything and listen to the message.
7. Then type in the correct password. As mentioned in post #1 above the encrypted QR Code was created with a password called Password1.
8. Listen to the message and now see the decrypted message at the bottom of the display.

Sure you will figure out the rest and hopefully some will be able to put this to good use i.e make some money!

You should also scan the two QR code that I am posting in this post (i.e post #2). Listen to the TextToSpeech engine reading out the message in the QR Codes....

You cannot scan the same QR Code twice in a row. You need to scan another QR Code in between. If you scan a QR Code and stop the scanner (button Stop Scan) and then restart the scanner you can scan the same QR Code immediately.

SCANNER STARTED
1.png



DISPLAY AFTER THE QR CODE IN POST #1 WAS SCANNED - SEE THE ENCRYPTED MESSAGE AT THE BOTTOM OF THE DISPLAY
2.png



ENTERING THE PASSWORD THAT WAS USED IN POST #1 ABOVE TO CREATE THE ENCRYPTED QR CODE. THE PASSWORD IS Password1. FIRST TYPE IN AN INCORRECT PASSWORD AN LISTEN TO THE TEXT-TO-SPEECH MESSAGE
3.png






PASSWORD SUBMITTED - SEE THE DECRYPTED MESSAGE AT THE BOTTOM OF THE DISPLAY. THIS IS THE MESSAGE THAT WE ENCRYPTED IN POST #1 ABOVE
5.png



NOW SCAN THE FOLLOWING TWO QR CODES AND LISTEN TO THE TEXT-TO-SPEECH ENGINE:

MonaLisa.png



Obama.png



Enjoy Password Protected QR Codes...;)
 

Attachments

  • b4aPasswordProtectedQRCodeReaderView.zip
    20.3 KB · Views: 1,080
  • AndroidLibFiles.zip
    463.2 KB · Views: 1,430
  • JavaObject.zip
    10.2 KB · Views: 853
  • QRCodeReaderViewLibFiles.zip
    35.1 KB · Views: 1,017
Last edited:
Top