Android signature mechanism: Generate KeyStore, sign, view signature information

Source: Internet
Author: User

Android's unique security mechanism, in addition to the authority mechanism, the other is the signature mechanism. Signature mechanism is mainly used in the following two main occasions to play its role: Upgrade app and permission check.

Upgrade your App

When a user upgrades an already installed app, if the program's modifications come from the same source, the upgrade installation is allowed, or a hint that the signature is inconsistent cannot be installed.

Permission check

I have mentioned in the specific use of the Android permission Authority mechanism that the protection level for requesting permission is signature or Signatureorsystem, Checks whether the certificate for the permission requester and the permission declarator is consistent.

As for the principle of signature mechanism and other functions, this is not detailed, this article mainly introduces, the signature file key generation, with key to sign the APK file and the method of viewing the signature.

Generate KeyStore

To create the KeyStore, you need to use Keytool.exe (located in the Jdk_xx\jre\bin directory), as follows:

Note

///

Installation path of the JDK under Mac

The Apple system already contains the complete J2SE, with the JDK and JVM (Apple called VM).
If you want to develop code under a Mac system. You can install an IDE (NETBEANS/ECLIPSE, etc.), and then do not need to install the JDK and JVM, Mac has been to help you download, installation is complete, configure a good path. Of course, if you want to upgrade the JDK, then of course you have to download the installation.

In Mac systems, the JDK has a different installation path than Windows, and the default directory is:/system/libray/frameworks/javavm.framwork/.
In this directory there is a versions directory with different versions of the JDK.

///

)

Keytool-genkey-alias demo.keystore-keyalg rsa-validity 40000-keystore demo.keystore

#说明:

#-genkey Generate key

#-alias Demo.keystore alias Demo.keystore

#-KEYALG RSA uses RSA algorithm to encrypt signature

#-validity 40000 expiry 4,000 days

#-keystore Demo.keystore

///

(note here that when generating keystore at the command line, there will be garbled references to the following text:

First of all, we need a keystore, and of course we already have it.
CMD under:
Go to the Bin directory of the JDK, so that the Android.keystore file will be generated in this directory, we need this file when signing
C:\Program Files\java\jdk1.6.0_10\bin>keytool-genkey-alias android.keystore-keyalg rsa-validity 20000-keystore Android.keystore
Enter KeyStore Password:
Enter the new password again:
What is your first and last name?
[Unknown]: qiaoling What is your organizational unit name?
[Unknown]: www.chinaandroid.com
What is your organization's name?
[Unknown]: www.chinaandroid.com
What is the name of your city or region?
[Unknown]: Haidian
What is the name of your state or province?
[Unknown]: BJ
What is the two-letter country code for this unit?
[Unknown]: 86
Cn=qiaoling, ou=www.chinaandroid.com, o=www.chinaandroid.com, L=haidian, ST=BJ, c=86 right?
[No]: Y
Enter <android.keystore> master password (if same as KeyStore password, press ENTER):

Where the parameter-validity is valid for the certificate number of days, here we write 10,000 days larger. There is no echo when you enter the password (even if you lose it) and backspace, tab and so on are the password content, this password is required for the. apk file signature.

Then sign:
In Eclipse, right-click the project that needs to be signed-->android Tools-->export signed application package ...
The following dialog box appears, selecting the project to be signed

Next, select the Android.keystore file location and password that you created above

Next

Next, select the location and name of the signature generation file

Finish, it's done.

)

///

Sign the APK

Use the resulting keystore to sign the APK, using the Jarsigner.exe, which is located in the Jdk_xx\bin directory, the command is as follows:

Jarsigner-verbose-keystore Demo.keystore-signedjar test_signed.apk test.apk demo.keystore

# test_signed.apk is the file after the signature

# test.apk is a file that needs to be signed

It is also important to note that if your JDK version is above 1.7, you will need to add this parameter when you sign the APK:

1

-digestalg Sha1-sigalg Md5withrsa

Otherwise the same will occur: Failure [install_parse_failed_no_certificates] error.

Note

Use Jarsigner to android_infoassistant.apk
The file is signed as follows:
E:\android_publish> "E:\jdk1.6\bin\jarsigner.exe"-verbose-keystore Alex_android.keystore-signedjar android_ assistant.apk assistant.apk Alex_android.keystore

Enter the passphrase for the key:
...... (Note: This is the input password)
The unsigned assistant.apk file is signed as android_assistant.apk with the above command

Finally verify that the signature is successful
E:\android_publish> "E:\jdk1.6\bin\jarsigner.exe"-verify android_assistant.apk
If it succeeds
Output:
Jar verified)

View signature Information

1. View KeyStore Information

1

Keytool-list-keystore Demo.keystore-alias Mykey-v

2. View KeyStore's public key certificate information

1

Keytool-list-keystore Demo.keystore-alias MYKEY-RFC

(Note: Obtain a public key certificate in the BASE64 format, RFC 1421)

3. Check the APK signature information

1

Jarsigner-verify-verbose-certs <your_apk_path.apk>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.