When an Android app is signed, the signing process generates a unique digital signature for the app package file (APK). This signature is based on the contents of the APK and is designed to be difficult to forge. The signature is used to verify that the app has not been tampered with and that it was signed by the developer who claims to have signed it.
The signing process uses a private and public key pair, which is a fundamental component of public key cryptography. The private key is used to generate the digital signature, while the public key is used to verify the signature.
The private key used to sign the app is typically generated by the app developer using a tool such as keytool or OpenSSL. The private key should be kept secret and should only be accessible to the developer or a trusted agent. The public key, on the other hand, is included in the digital certificate that is generated when the APK is signed.
The digital certificate contains information about the app, such as the package name, version number, and the public key used to sign the APK. The certificate is also signed by a trusted certificate authority, such as Google, to ensure that the certificate itself is authentic.
When a user installs an app on their Android device, the Android operating system verifies the digital signature using the public key stored in the app's certificate. If the signature is valid and the certificate is trusted, the app is installed. If the signature is not valid or the certificate is not trusted, the installation is blocked.
The Android signing process also supports the concept of release keys and debug keys. A release key is used to sign the final version of an app that is published to an app store, while a debug key is used to sign test versions of the app during development. The debug key is typically generated automatically by the Android development tools and should not be used to sign the final version of the app.
starting from Android 11, APK signature scheme v2 became mandatory, and jarsigner doesn't support signing APKs using this scheme. Instead, you can use the Android SDK Build Tools' apksigner tool to sign your APK.
keytool -genkey -v -keystore your_keystore_name.keystore -alias your_alias_name -keyalg RSA -keysize 2048 -validity 10000
genkey: Specifies that the keytool command is used for generating a new key pair and associated certificate.v: Human Readablekeystore your_keystore_name.keystore: Specifies the name and location of the Keystore file to be generated. Replace your_keystore_name.keystore with the desired name for your Keystore file.alias your_alias_name: Sets an alias name for the generated key pair. The alias is used later when signing the APK.keyalg RSA: Specifies the algorithm used for generating the key pair. In this case, it uses RSA (Rivest-Shamir-Adleman), a commonly used encryption algorithm.