Data Privacy for Android (Part 2)

Tram Ho

Next in Part 1 we will learn about the remaining methods of protecting personal data in the process of building Android applications:

  • Anti screenshot
  • Encrypt personal data
  • Protect the source code of the application
  • Install biometric protection

Anti screenshot

When we made sure that all log information was not printed when running the application, the cache was deleted when the application exited. But it can still be easy for users to take screenshots related to our application, which can lead to copyright violations. For example, when reading a copyrighted comic, users need to confirm the information before they can open the reading, but here users can still take screenshots of each page and save it. To prevent this, we have an effective way to combat screenshots. We can add the following code to the onCreate () function in the application’s Activity

The above code will notify the system that our application window has FLAG_SECURE , which will prevent screen capture whether intentionally or unintentionally.

When building and running the application on the real machine, at the program window we try to take a screenshot to see a Toast message with the content “Couldn’t capture screenshot” or “Can’t take screenshot due to security policy, “ or a Toast with similar content.

Encrypt personal data

With important personal information when stored, or report files, information of the application is sent to the server we should choose to encrypt its information before being saved to the device memory. or sent.

To do this we can use a fairly powerful and popular algorithm in the community, AES (Advanced Encryption Standard). This algorithm has many different encryption modes: ECB, CBC, CFB, OFB, CTR in which CBC (Cipher Block Channing) is a good security mode and is also quite easy to use.

Here is how to use this algorithm in the application, first we declare the algorithm to use, encryption mode, Padding type is the type.

In the above code we use the AES algorithm to encrypt, the encryption mode is CBC , the Padding type is ” PKCS5Padding “. The “cipher” function takes two parameters:

  • opMode: the mode is encoding or decoding
  • secretKey: key used for encryption as well as decryption, this key must be used consistently in both cases of encryption and decryption, otherwise it will not decode the encrypted information.

Next we create two encrypt functions to encrypt and decrypt to decode as shown below

  • Encrypt function: get an encrypted string and key
  • Decrypt function: get encrypted string and key to decode

To encode a sequence of information in an application, we simply call the following:

With the decode simply call the following

We can see the encryption is quite simple, but it will help our information more secure, avoiding unauthorized access to information.

Protect the source code of the application

When building an application into an APK file for release , the source code for the application will be packaged in the classes.dex file. This leads to the fact that if our application is distributed to users, it is likely that the source code of the application will be easily stolen or reused for other purposes. This is very dangerous and can lead to serious problems when the application is commercial products, related to user information, credit cards, bank accounts …

In order to limit the disclosure of source code, we can use an available method, ProGuard . This is a tool for optimizing Java bytecode code, it helps Java and Android applications significantly reduce the size when it is possible to remove unnecessary variables and functions in the application. ProGuard is advertised to reduce the size of the application to 90% and operate 20% faster, but its useful function is also in the ability to minimize the ” reverse ” of the source code when it can. Tampering with class names, variables and functions, it is difficult to read and translate.

To accomplish this we need to pay attention to the following 2 files:

  1. Application build.gradle file: to declare the use of proguard in any build debug or release, source code protection methods.
  2. File proguard-rules.pro : declare functions, classes do not need ProGuard because it is related to the 3rd application, or the application’s input if ProGuard, the file or class name will not find the right name and application could not start.

First we need to determine which Proguard is included in the build of the debug or release application, and usually Proguard only works for the release because it is now complete and ready to distribute to users. is the declaration for enabling ProGuard for the build release from file build.gradle

With the above declaration we can see

  • minifyEnabled: used to activate ProGuard, remove unused code, rename classes, variables and functions when building
  • shrinkResources: used to eliminate unused resources when building
  • proguardFiles: to declare a list of classes, variables, functions and libraries that do not need ProGuard

Next, we will report when the classes, functions, libraries we should ignore need not ProGuard to ensure the program can work properly. Below is a sample file for bypassing ProGuard with libraries outside our application scope

As mentioned, using ProGuard will minimize the source code decompilation of the application, help limit the disclosure of code or eavesdrop on important information. They are essential for building distributed applications on Google Play.

Install biometric protection

Our application can be secure by requiring users to enter a password before opening a certain interface. But there is also the risk of our passwords being accidentally hacked by someone or the hackers themselves.

So in order to ensure that it is only us who cannot access the application, smartphone devices now support biometric authentication. Faces, iris or fingerprints are good examples. We are going to set up biometric authentication on our application to ensure that only we have access to the application and no one else.

First in the build.gradle part of the application, we declare the use of the biometric library

Next, to prevent crashes we need to check if the device supports biometric authentication. If not, then prompt the user to choose an alternative method such as conventional password authentication …

If the device supports biometric authentication, we will call showAuthentication () function to display the authentication information to the user.

This function will display the authentication interface as shown below.

If the user authenticates, then the call will show showListPersion () function, which shows the list of users, we can see through the figure below.

As we can see, the library of biometric authentication is quite simple but very confidential. We should prioritize installation choices if our application needs authentication before it can access the application.

Refer

  1. https://www.raywenderlich.com/6901838-data-privacy-for-android#toc-anchor-009
  2. https://gist.github.com/jidolstar/9ca129d4f3e9632b12a820f0784eb353
Share the news now

Source : Viblo