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==
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.
Post #2 (the QR Code scanner that will decode the Password Protected QR Code) to follow in this week.
Enjoy...
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==
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
Last edited: