【C#与Apk二三事】Java版Android Keystore验证方法

这个工具用于验证Keystore的有效性,包括过期时间。Keystore用于对apk签名,可以使用JDK的 Keytool.exe 来生成,如果想要用直观点GUI的方式来生成,可以借助比如Eclipse等工具。
  下面是ApkIDE中提供的 KeystoreVerify.jar 工具中使用的核心代码(Java代码,桌面app使用),也就是Keystore的验证方法:

//引入相关类库
import java.security.KeyStore;
import java.security.cert.X509Certificate;

//验证方法
private String getKeystoreInfo(String keystoreFile, char[] keystorePassword, char[] keyPassword){
        String myerr="";
        try {
            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            keystore.load(new FileInputStream(keystoreFile), keystorePassword);
            Enumeration<String> aliases = keystore.aliases();
            while(aliases.hasMoreElements()){
                String alias = aliases.nextElement();
                if(keystore.getCertificate(alias).getType().equals("X.509")){
                    try{
                        keystore.getKey(alias, keyPassword);
                    }catch(Exception e){
                        if(this.isDoverify) myerr="验证失败!";
                        myerr+="Key Password错误:"+e.getMessage();
                        break;
                    }
                    String inf="";
                    if(this.isDoverify){
                        if(this.keyAlias.equalsIgnoreCase(alias)) inf="验证成功!";
                        else inf="验证失败!别名错误。";
                    }
                    else inf="别名:"+ alias;
                    return inf + " 过期时间:" + dateToString(((X509Certificate) keystore.getCertificate(alias)).getNotAfter());
                }
            }
        } catch (Exception e) {
            if(this.isDoverify) myerr="验证失败!";
            myerr += "Keystore Password错误:"+e.getMessage();
        }
        return myerr;
    }
    
    private String dateToString(Date date){
        SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return fmt.format(date);
    }

by Mandarava(鳗驼螺) 2017.06.19

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,986评论 25 709
  • 服务器https配置 配置https操作说明文档 1、查看服务器环境配置(tomcat和apache合并使用) 2...
    南京杨小兵阅读 12,984评论 0 9
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,833评论 19 139
  • 写在前面 今天使用高德地图为应用添加Key的时候,发现有一项需要用到安全码SHA1,而SHA1存在于Keystor...
    代码咖啡阅读 27,077评论 5 34
  • 看过韩剧的朋友们一定无法忽略其中曝光率超高的韩式咖啡厅:其中质朴、暴露的结构,别具特色的照明灯具,都成为韩式风格的...
    洛洛莉ya阅读 3,604评论 1 5

友情链接更多精彩内容