在微信开放平台等申请API key 和secret时经常要用到apk文件签名,那么如何读取呢?

下面贴一下相关读取源码:

一共两个文件MainActivity和MD5,

package com.lcg.gensinature;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity
{
private Button mCopyBtn;
private Button mGenBtn;
private EditText mPackageEt;
private TextView md5SignTv; public static byte[] getSign(Context paramContext, String paramString)
{ PackageInfo localPackageInfo;
int i;
byte[] arrayOfByte;
try
{
localPackageInfo = paramContext.getPackageManager().getPackageInfo(paramString, 64);
i = 0;
if (i >= localPackageInfo.signatures.length)
{ return null;
}
System.out.println(i);
arrayOfByte = localPackageInfo.signatures[i].toByteArray();
System.out.println(arrayOfByte);
return arrayOfByte;
}
catch (PackageManager.NameNotFoundException localNameNotFoundException)
{
return null;
} } protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.activity_main);
this.mPackageEt = ((EditText)findViewById(R.id.pkgNameEt));
this.mGenBtn = ((Button)findViewById(R.id.getBtn));
this.mGenBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramView)
{
String str1 = MainActivity.this.mPackageEt.getText().toString();
if (TextUtils.isEmpty(str1));
byte[] arrayOfByte; arrayOfByte = MainActivity.getSign(MainActivity.this.getApplicationContext(), str1); String str2 = "";
try
{
str2 = MD5.hexdigest(arrayOfByte);
Log.d("genSinature", "gen md5=" + str2);
System.out.println(str2);
MainActivity.this.md5SignTv.setText(str2); ClipboardManager cmb = (ClipboardManager)MainActivity.this.getSystemService(MainActivity.this.CLIPBOARD_SERVICE);
cmb.setText(str2);
Toast.makeText(MainActivity.this, "已经复制到剪贴板", 1).show();
return;
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
this.md5SignTv = ((TextView)findViewById(R.id.resultTv)); showDialog();
} private void showDialog()
{
AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
localBuilder.setCancelable(false);
localBuilder.setTitle("警告ʾ").setMessage("将读取APP信息");
localBuilder.setPositiveButton("拒绝", null);
localBuilder.setNegativeButton("同意", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface paramDialogInterface, int paramInt)
{
MainActivity.this.finish();
}
});
localBuilder.show();
}
}

  

package com.lcg.gensinature;

import java.security.MessageDigest;

public class MD5
{
private static final char[] hexDigits = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 }; public static String hexdigest(String paramString)
{
try
{
String str = hexdigest(paramString.getBytes());
return str;
}
catch (Exception localException)
{
}
return null;
} public static String hexdigest(byte[] paramArrayOfByte)
{
try
{
MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
System.out.println(paramArrayOfByte);
localMessageDigest.update(paramArrayOfByte);
byte[] arrayOfByte = localMessageDigest.digest();
System.out.println(arrayOfByte); char[] arrayOfChar = new char[32];
int i = 0;
int j = 0;
for(i=0;i<16;i++){ int k = arrayOfByte[i];
int l = j + 1;
arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
j = l + 1;
arrayOfChar[l] = hexDigits[(k & 0xF)];
}
if (i >= 16)
return new String(arrayOfChar);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}

  更多精彩内容请访问 http://www.leegtang.com

读取Android APK文件签名的方法的更多相关文章

  1. android apk的签名和权限问题

    一. android apk的签名问题(http://blog.csdn.net/lyq8479/article/details/6401093) 1.为什么要给Android应用程序签名?      ...

  2. apk文件签名绕过

    声明: 1.本文转载自:http://www.2cto.com/Article/201311/256406.html,为了留作日后参考上传博客 2.如有转载请复试上面连接,尊重原创 apk文件签名绕过 ...

  3. 如何给apk文件签名

    1.签名的意义 为了保证每个应用程序开发商合法ID,防止部分开放商可能通过使用相同的Package Name来混淆替换已经安装的程序,我们需要对我们发布的APK文件进行唯一签名,保证我们每次发布的版本 ...

  4. Visual Studio 2017为Android APK包签名

    Visual Studio 2017为Android APK包签名   为Android APK包签名,可以保证后期的App顺利升级.在Visual Studio 2015中,IDE会自动生成两个AP ...

  5. Android之APK文件签名——keytool和jarsigner

    一.生成密钥库将位置定位在jdk的bin文件中,输入以下命名行:keytool -genkey -alias ChangeBackgroundWidget.keystore -keyalg RSA - ...

  6. [Android]使用platform密钥来给apk文件签名的命令

    1.使用platform密钥对apk进行签名 1.1.进入<Android_Source_Path>/build/target/product/security,找到[platform.p ...

  7. Android APK如何签名

    Android项目以它的包名作为唯一标识,如果在同一设备上安装两个相同的应用,后面安装的应用就会覆盖前面安装的应用.为了避免这种情况的发生,我们需要对作为产品发布的应用进行签名. 签名其实有两个作用: ...

  8. android APK 文件的生成过程

    步骤: 1. 用 aapt工具生成R文件aapt  package  -m -J  gen目录 -M AndroidManifest.xml  -S res目录  -I 编译版本sdk的android ...

  9. (转载)重新对APK文件签名

    1.将证书(debug.keystore)复制到与需要重新签名的apk文件相同的目录下(如:复制到D:\Sign) 2.在cmd中切换到需要重新签名的apk文件的目录下 3.使用WinRAR打开要重新 ...

随机推荐

  1. iostat来对linux硬盘IO性能进行了解

    http://www.php-oa.com/2009/02/03/iostat.html

  2. How to save/read file on different platforms

    You can use standard c functions, such as fopen, fwrite, to save and read file on different platform ...

  3. lsb_release 提示命令不存在

    1. 报错信息 bash: lsb_release: command not found 2. 问题原因 未安装 lsb_release 命令 3. 解决方法 用 yum 命令安装 lsb_relea ...

  4. HDU1016(bfs)

    import java.util.Scanner;public class Main1016 { public static void main(String[] args) { Scanner ci ...

  5. 重载public Primes ():this(2,100)

    当构造函数有多个重载的时候 想通过默认构造函数调用其他的重载的构造函数的话 就可以用:运算符public Primes():this(2, 100){//code }public Primes(int ...

  6. C语言实现电话本 动态开辟 信息存储于文件

    下面是我用C写的一个电话本小项目,实现的功能有:添加 删除 修改 查找 排序 清空 显示,功能还是比较全的,内存也是动态开辟的.能存储于本地,能从本地读出并显示 头文件部分代码,contact.h: ...

  7. 【慕课网学习笔记】Java共享变量的可见性和原子性

    1. Java内存模型(Java Memory Model, JMM) Java的内存模型如下,所有变量都存储在主内存中,每个线程都有自己的工作内存. 共享变量:如果一个变量在多个线程中都使用到了,那 ...

  8. YII中面包屑制作(当前位置:网站首页 >> 会员登陆)

    面包屑制作(当前位置:网站首页 >> 会员登陆) 作用:就是在布局里要显示的信息,但这个信息在不同的页面里显示的内容不一样,例如:网页的title.网页的当前位置等等 1.布局中每个页面t ...

  9. ECMA5 Array 新增API reduce

    1)reduce:相当与迭代: [].reduce(function(previous,current,index,array){ return previous * current;//相当与做阶乘 ...

  10. readonly和const区别

    常量和只读变量的区别 const string name="Xuj"; readonly string name; 1.常量是不可改变的,只读变量只能在构造方法中才能改变其值. 2 ...