編輯:關於Android編程
Signature[] sigs = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES).signatures; byte[] data = sigs[0].toByteArray();
// alg參數指定算法 MessageDigest algorithm = MessageDigest.getInstance(alg); // 將狀態重圍到初始化,准備好進行計算Hash值。 algorithm.reset(); // 返回計算後的Hash值 algorithm.update(data); // 將Hash值轉為可讀的16進制文本 byte[] dataHash = algorithm.digest();
txtVerify.setText(alg + "\n"); String result = "This soft is a pirated software."; if (alg.equals(MD5)) { txtVerify.append("Original:" + "\n" + HASH_VALUE_MD5 + "\n"); if (HASH_VALUE_MD5.equals(hexInfo)) { result = "This soft is a genuine software"; } } else if (alg.equals(SHA1)) { txtVerify.append("Original:" + "\n" + HASH_VALUE_SHA1 + "\n"); if (HASH_VALUE_SHA1.equals(hexInfo)) { result = "This soft is a genuine software"; } } txtVerify.append("Current:" + ":\n" + hexInfo + "\n"); txtVerify.append(result);
以上代碼中的HASH_VALUE_MD5和HASH_VALUE_SHA1分別是使用Code_Test_Key.keystore簽名後的APK實際計算出的HASH值。
以下信息來自網易:
一、簽名方法if ["$1" == ""]; then從腳本中,我們可以得知,是采用以下步驟生成keypair的。
echo "Create a test certificate key."
echo "Usage: $0 NAME"
echo "Will generate NAME.pk8 and NAME.x509.pem"
echo " /C=US/ST=California/L=Mountain View/O=Android/OU=Android/CN=Android/[email protected]"
return
fi
openssl genrsa -3 -out $1.pem 2048
openssl req -new -x509 -key $1.pem -out $1.x509.pem -days 10000 \
-subj '/C=US/ST=California/L=Mountain View/O=Android/OU=Android/CN=Android/[email protected]'
openssl pkcs8 -in $1.pem -topk8 -outform DER -out $1.pk8 -nocrypt
root@bt:~/bubble example# openssl genrsa -3 -out test.pem 2048
Generating RSA private key, 2048 bit long modulus
...+++
...................................................................................+++
e is 3 (0x3)
第二步:生成x509格式的公鑰證書root@bt:~/bubble example# cat test.pem
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAxJGCXH8i3XgogreH9oMoTaqU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h8zwPD8zU1SXYcMsn6Jf4mavLY4uydB6ZO6nrUzjjJAMDM3irbaqctvWG
G8Bs5AePNpmwImFJ6hU/KCTiwCUFw/Rj6tQYbR37lJaTpINg0nje0M1krSr52Ek8
9Oh9osRPhXek4y6o3tgSbwKBgQCJGITsRCPdeLvnQpK++/Uzq1b3XYTIOD15cSYj
07+hH5Tu2sVB/vdgG+THnGmDZWHqaSbL+Yvn05kLXaYzlohmWr/hx+SCGJPCgL/D
kqx2HYofd7sjXsv17RiHd356S2NiFhD/OpG9uLEDSvaF0XLSbzsR/9nGq/iufd9r
8xQDEwKBgHVOxkAoYn4Q3DXhfeTrxpZEmg7lBTuZAg8A52xH/eVMVTSKwqw+NxfZ
wrU+fGQeruGm5mmOzwOuTDwywcM8qs0/gQVsUVliCMd6IbwDnN4dsNLsZdZp1nV7
rbi0sCmk218gLOQ9p2jv9i0BS8etSEkBwRzF99KgruMGtyd4R39b
-----END RSA PRIVATE KEY-----
root@bt:~/bubble example# openssl req -new -x509 -key test.pem -out test.x509.pem -days 10000
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:California
Locality Name (eg, city) []:Mountain View
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Android
Organizational Unit Name (eg, section) []:Android
Common Name (eg, YOUR name) []:Android
Email Address []:[email protected]
第三步:生成符合PKCS8標注的私鑰文件root@bt:~/bubble example# cat test.x509.pem
-----BEGIN CERTIFICATE-----
MIIEqjCCA5KgAwIBAgIJALNXgLhW3YPbMA0GCSqGSIb3DQEBBQUAMIGUMQswCQYD
VQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNTW91bnRhaW4g
VmlldzEQMA4GA1UEChMHQW5kcm9pZDEQMA4GA1UECxMHQW5kcm9pZDEQMA4GA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wAwEB/zANBgkqhkiG9w0BAQUFAAOCAQEAq/BUGCGWpF1E7WQ/vmoDrZ4r
2XTmyHycQSFpvozeKjvlBD1ESNchEWvxOSFiFrCX/OqFwEn8sQRMClpR4fdXmKrv
5GxrXN3M6ZNOmr7cNGINNeJkKhQfepaUE9bdcdSyzQsgajF0rhYosyW48k7DW5J8
zXoBIrUgGV1KhnIPci+Jg7+QBhXGZxIOyVqDg8q8h8CJgMldyyPNdmhHDAH497Sp
EgIioD/95Gk5DnrHDwlTo54vh5hT2KEjcfkMNEhXpQFgYJog+HYcJwjdFoH0ZA+P
2lZ9o6hT3Dkc1PzdgzehVVa+ys0SZ4pHMPpx2z5T9cR25wO15mC8inkcA5onqQ==
-----END CERTIFICATE-----
可以在http://blog.csdn.net/anxuegang/article/details/6157927看到不同證書的格式的介紹root@bt:~/bubble example# openssl pkcs8 -in test.pem -topk8 -outform DER -out test.pk8 -nocrypt
#!/bin/sh
MY_SDK_HOME=~/platform/android/main/mydroid/
PEM=${MY_SDK_HOME}/build/target/product/security/platform.x509.pem
PK8=${MY_SDK_HOME}/build/target/product/security/platform.pk8
if [ $# -ne 2 ]
then
echo Usage $0 in.apk out.apk
exit 1
fi
java -jar ${MY_SDK_HOME}/out/host/linux-x86/framework/signapk.jar \
${PEM} ${PK8} $1 $2
root@bt:~/bubble example/dis# java -jar signapk.jar test.x509.pem test.pk8 unsig.apk signapk.apk
root@bt:~/bubble example# zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk
第二步:簽名root@bt:~# keytool -genkey -alias dani.keystore -keyalg RSA -validity 4000 -keystore dani.keystore
第三步:優化oot@bt:~/bubble example/dis/FrozenBubbleSignapk/META-INF# jarsigner -keystore dani.keystore -digestalg SHA1 -sigalg MD5withRSA -signedjar signed.apk unsigend.apk dani.keystore
root@bt:~/bubble example# zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk
#!/usr/bin/perl
use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
die "$0 requires one arguments (file pathname) . \n" if $#ARGV !=0;
$filename=shift;
open FH,$filename or die "cannot open $filename";
$sha1 = Digest::SHA1->new;
$sha1->addfile(*FH);
$digest= $sha1->b64digest;
print $digest,"\n";
close FH;
然後將這些摘要信息保存在CERT.MF中。 備注:采用android(Android signapk.jar與Sun jarsigner兩種簽名方法生成的CERT.MF文件弱有不同。
SHA1("Name:
filename"+CR+LF+"SHA1-Digest: "+SHA1(file_content)+CR+LF+CR+LF)
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initSign(privateKey);
je = new JarEntry(CERT_SF_NAME);
je.setTime(timestamp);
outputJar.putNextEntry(je);
writeSignatureFile(manifest,
new SignatureOutputStream(outputJar, signature));
je = new JarEntry(CERT_RSA_NAME);
je.setTime(timestamp);
outputJar.putNextEntry(je);
writeSignatureBlock(signature, publicKey, outputJar);
root@bt:~/bubble example/dis# jarsigner -verify -certs -verbose FrozenBubble-modify-sig.apk
4689 Wed Dec 12 15:56:44 CST 2012 META-INF/MANIFEST.MF
4810 Wed Dec 12 15:56:44 CST 2012 META-INF/DANI_KEY.SF
913 Wed Dec 12 15:56:44 CST 2012 META-INF/DANI_KEY.RSA
sm 29099 Wed Dec 12 15:10:22 CST 2012 assets/levels.txt
X.509, CN=dani lee, OU=taomee, O=taomee, L=shanghai, ST=CA, C=CA
[certificate is valid from 12/12/12 3:43 PM to 11/25/23 3:43 PM]
sm 25213 Wed Dec 12 15:20:14 CST 2012 res/drawable/app_frozen_bubble.png
使用了RecyclerView嵌套RecyclerView的方案。購物車的第一個界面為RecyclerView,每個Item裡面包含一個店鋪。在Item中使用Recycl
進度框import android.content.Context; import android.graphics.Canvas; import android.gra
在Android實際項目開發中,經常要用到ImageView這個組件,如果純粹只是用來顯示某一張圖片,這個原生的組件是很方便的。但有時候為了效果更炫一點,比如很多軟件都有
在android中的Touch分發中,經常可以看到從ACTION_DOWN->ACTION_MOVE->ACTION_UP,當我們不了解它是如何分發的話總感覺