android签名机制(1)——了解签名

android中全部运行在设备上的应用都必需要签名,这是系统提供的一种安全认证机制。
下面以问答形式来自我认识下,后面在根据部份内容来进一步研究: html

1.为何用签名机制?若是不签名呢?

为了防止app被反编译替换原有的程序,若是有相同包名的应用存在,新的apk则不能安装在设备上。 就算被反编译,在没有原有私钥的状况,从新签名生成的apk和原来的apk的标志是不一样的。如此,被修改的apk若是安装在原来的设备上,则会提示:
Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES]

若是不签名呢?
直接安装到设备上,则也提示:

从上面能够有个很简易的认知,若是不签名或者签名不对,则不能安装在设备上。 java

若是应用androidmenifast.xml中加了android:sharedUserId="android.uid.system"时,要用平台签名,不然提示
  
更多安装异常能够查找相关信息。 android

2.平台签名?签名有不少种么?

有不少种。在android源码目录build\target\product\security,有以下几种签名密钥。

*.pk8表示的是private key---私钥
*.x509.pem理解为公钥

这里的platform.*即为平台签名的私钥和公钥,加密方式是非对称加密。
从上图能够看出有好几种的签名,4种具体表示可参考当前目录下的README.
安全

The following commands were used to generate the test key pairs:
//制做key的脚本和相关信息
  development/tools/make_key testkey  '/C=US/ST=California/L=Mountain View/O=Android/OU=Android/CN=Android/emailAddress=android@android.com'
  development/tools/make_key platform '/C=US/ST=California/L=Mountain View/O=Android/OU=Android/CN=Android/emailAddress=android@android.com'
  development/tools/make_key shared   '/C=US/ST=California/L=Mountain View/O=Android/OU=Android/CN=Android/emailAddress=android@android.com'
  development/tools/make_key media    '/C=US/ST=California/L=Mountain View/O=Android/OU=Android/CN=Android/emailAddress=android@android.com'

The following standard test keys are currently included:

//testkey--当应用没有指定其余key时,源码默认以此key签名
testkey -- a generic key for packages that do not otherwise specify a key.
//平台签名,系统级应用用此签名
platform -- a test key for packages that are part of the core platform.
//当须要用到联系人的共享数据时,须要此签名
shared -- a test key for things that are shared in the home/contacts process.
//对系统的media/download相关的应用,用此签名
media -- a test key for packages that are part of the media/download system.

These test keys are used strictly in development, and should never be assumed
to convey any sort of validity.  When $BUILD_SECURE=true, the code should not
honor these keys in any context.


signing using the openssl commandline (for boot/system images)
--------------------------------------------------------------

1. convert pk8 format key to pem format
   % openssl pkcs8 -inform DER -nocrypt -in testkey.pk8 -out testkey.pem

2. create a signature using the pem format key
   % openssl dgst -binary -sha1 -sign testkey.pem FILE > FILE.sig

extracting public keys for embedding
------------------------------------
it's a Java tool
but it generates C code
take a look at commands/recovery/Android.mk
you'll see it running $(HOST_OUT_JAVA_LIBRARIES)/dumpkey.jar

从shareed签名,能够联想到:
当A应用须要用到B应用的共享数据时,他们的
android:sharedUserId="xxx"须要一致,签名也须要一致 app

注:当应用工程的Android.mk中未指定LOCAL_CERTIFICATE时,源码编译则使用默认testkey


3.如何区别签名过和没签名的apk?

注:apk支持屡次签名,以最后一次为准
apk是个zip包格式,这里就有个很简单直观的方式---用压缩程序方式打开apk,看下图:

签名过的apk包里有个META-INF文件夹,里面就是对各个资源文件用SHA1加密后的值。

4.如何签名?

loop   android签名机制(2)——如何签名 oop

5.了解加密、数字摘要、数字签名和数字证书

参考:http://www.cnblogs.com/heart-runner/archive/2012/01/30/2332020.html
ui


问题待补充...
 参考:  http://developer.android.com/tools/publishing/app-signing.html   加密

相关文章
相关标签/搜索