• 读取Android APK文件签名的方法


    在微信开放平台等申请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

  • 相关阅读:
    Http Get与Post的区别
    华为USG6000V防火墙简单配置案例
    华为USG6000V防火墙简单配置案例
    基于三层交换机的VRRP技术--MSTP、VRRP的综合运用
    ci test
    CI框架 default_controller 如何设置为:'目录/Controller' 转
    查看django版本的方法
    Django如何安装指定版本
    django学习--2 模板
    django学习--1
  • 原文地址:https://www.cnblogs.com/leegtang/p/5560385.html
Copyright © 2020-2023  润新知