Lab 8 - Cryptography 2


  • Protect the app's APK using signing
  • Use Android's KeyStore to generate and store cryptographic keys and use them to sign an app
  • Verify message integrity using HMAC (hash-based message authentication code)
    • generate HMAC to sign a message
    • verify HMAC to check that the received message was not modified

Application signing

If you want to install an application on Android, the apk must be digitally signed with a certificate. For example, when you test your application on the emulator, Android Studio signs the apk with a debug certificate. The first time when you run or debug a project in Android Studio, a debug keystore and certificate is automatically created using the Android SDK tools in $HOME/.android/debug.keystore. Also the keystore is initialized and the key password is set.

As a security measure the debug certificate needs to be used only for testing and for debug builds. This certificate is not secure for using on app stores.

For later runs/debugs Android Studio automatically stores the debug signing configuration so that we do not need to enter it every time we launch the app. The signing configuration contains the keystore location $HOME/.android/debug.keystore, keystore password, key name and key password. This debug signing configuration used at run/debug is not available for editing. You can create a signing config for your release builds.

Steps for generating and uploading key and keystore:

  1. Go to Build > Generate Signed Bundle/APK
  2. Select APK
  3. Under Key store path choose Create new
  4. Complete the fields and then continue with the signing steps below

From here we continue to sign the app with the key stored in the newly created keystore. You can skip the first two steps if you are already in the window at the 3rd step:

  1. Build → Generate Signed Bundle/APK
  2. In the Generate Signed Bundle/APK choose APK
  3. The app module should be selected if not choose it or choose a module from the drop down
  4. Enter the path to your keystore, the alias for the key and the passwords for keystore and key
  5. Enter destination folder for the signed app, enter the release build type, choose the flavor
  6. Choose the APK Signature version your app to support - v2 for Android 7.0+
  7. Finish

For more details on application signing and distribution check App Signing


Task 1 - Sign the application (3p)

In this task will see how Android applications are signed:

  • Create a new application (with an empty activity).
  • Using the steps presented in the lab create a key and keystore and generate the signed APK of the application.
  • Use adb tool to install the APK.

Task 2 - Signing configuration (3p)

In this task will create a signing configuration for different build types.

  1. Create a release signing configuration with the data from Task 1. Auto sign
  2. Build a release version when pressing Run. Check BuildSelect Build Variant view from Android Studio.
  3. Create a release signing configuration using a new key.
  4. Sign the application wth the new release config and use adb to install the signed apk. You should install the new signed apk on top of the one from item #2. What happens with the application?

Task 3 - Generate and Verify HMAC (4p)

Hash-based message authentication code (HMAC) is a mechanism for verifying the authenticity and integrity of a message. You can compute it using a hashing crypto algorithm (e.g. SHA-2 family HMAC) and a secret symmetric key. In Android you can use the standard Java API (javax.crypto) for computing it.

Add an activity to the project. Include an EditText and a Button in the first activity. When the user types a text and presses the button, it will send the text to the second activity through an intent (putExtra). In the second activity, get the message from the Intent and display it in the TextView.

In the first activity generate a symmetric key using KeyGenerator for HmacSha256 algorithm. Save this key in a Singleton (that can be accessed from both activities). Then generate the HMAC of the text introduced by the user (using MAC with HmacSha256 algorithm) and send the HMAC along with the initial message (through the Intent). In the second activity, obtain the HMAC from the Intent, obtain the Singleton, get the symmetric key and recompute the HMAC. If the HMAC is valid (equal with the recomputed one), Display the message “Data is unmodified”.

Send the data and HMAC as byte arrays in the Intent. Use Arrays.equals() for byte arrays comparison.

 String secret = "secret";
 String message = "important message";
 Mac sha256HMAC = Mac.getInstance("HmacSHA256");
 SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(),"HmacSHA256");
 byte[] secretMessageBytes = sha256HMAC.doFinal(message.getBytes())
smd/laboratoare/08.txt · Last modified: 2021/05/13 19:51 by adriana.draghici
CC Attribution-Share Alike 3.0 Unported Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0