android读取keystore证书文件
来源:互联网 发布:js开发工具 编辑:程序博客网 时间:2024/05/21 14:44
需要把关键信息存在keystore证书文件中,但是java本身支持的keystore是jks的,而android不支持jks,只支持bks的,那么就需要将jks类型的证书转换成bks类型的,然后使用android支持的方式去读取证书内容。
android不支持jks如下说明,见stack说明:
http://stackoverflow.com/questions/9312193/does-android-support-jks-keystore-type
例如如下说明:
问题:
I am trying to connect to server using keystore which is provided by server team.While sending service call to server first i created KeyStore Instance by using following apiKeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());It’s returning the keystore type as “BKS”.The Keystore what server team sent is of type “.jks”(somename.jks) So, I am getting exception “Wrong version of key store”.I tried by passing “JKS” to getInstance() of KeyStore by following wayKeyStore keystore = KeyStore.getInstance("JKS"); But here I am getting exception “KeyStore JKS implementation not found”.Here is the piece of code:KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());InputStream instream = mContext.getAssets().open("somename.jks");try { trustStore.load(instream, "password".toCharArray());} finally { try { instream.close(); } catch(Exception ignore) { }}Please guide me to solve this problem
回答见下:
I think Android support 'only' BouncyCastle KeyStores (known as BKS)... You still can use PortecleTo convert it from JKS to BKS, should work like a charm (at least it worked for me when trying to store my .CRT into a BKS format ! ;)'only' meaning, easily here :p, else you'll have to manipulate stuffs
需要转换的工具是Portecle
下载地址为
http://download.csdn.net/download/buxin_2008/4203250
使用方式见下:
http://ssl.chinassl.com/post/1cbcfb77_3b8d5cd
使用其实就是打开已有的keystore文件,然后修改jks为bks,然后重新保存证书就可以了,
最后使用的android读取证书的方式如下:
try { String password = "xxxa"; AssetManager asset = this.getResources().getAssets(); InputStream ceris = asset.open("zlex.keystore");// FileInputStream is = new FileInputStream(keyStorePath); //KeyStore.getInstance(KeyStore.getDefaultType()); KeyStore ks = KeyStore.getInstance("BKS"); ks.load(ceris, password.toCharArray()); ceris.close(); String alias = "xxxb"; RSAPrivateKey privateKey = (RSAPrivateKey) ks.getKey(alias, password.toCharArray()); System.out.println("私钥="+Base64.encode(privateKey.getEncoded())); //----------------------------------------------------------- CertificateFactory certificateFactory = CertificateFactory .getInstance("X509"); InputStream keyis = asset.open("zlex.cer");// FileInputStream in = new FileInputStream(certificatePath); Certificate certificate = certificateFactory.generateCertificate(keyis); keyis.close(); RSAPublicKey publicKey = (RSAPublicKey)certificate.getPublicKey(); System.out.println("公钥="+Base64.encode(publicKey.getEncoded()));} catch (KeyStoreException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (CertificateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (UnrecoverableEntryException e) {// TODO Auto-generated catch blocke.printStackTrace();}
其中xxxa为证书密码,xxxb为证书别名
这里需要将keystore文件,cer文件保存在assets中,如果保存在其他地方,请修改为相应目录,修改相应读取方式。
0 0
- android读取keystore证书文件
- Android keystore 证书文件制作
- 修改android签名证书keystore
- Android生成keystore证书文件及获取证书指纹的方法
- java安全架构____读取keystore文件的密钥对和证书信息
- 获取android系统默认证书KeyStore
- Android设置自定义的keystore调试证书
- android keystore 公钥,私钥,证书完全解析
- Android系统证书platform.x509.pem,platform.pk8转换为.keystore文件
- 【知识整理】生成Android(安卓)app证书的keystore文件
- Android 怎么生成keystore文件
- 从Java Keystore文件中提取私钥、证书
- 从Java Keystore文件中提取私钥、证书 .
- 从Java Keystore文件中提取私钥、证书
- 从Java Keystore文件中提取私钥、证书
- .apk 应用签名文件(.keystore)的生成 --- 证书
- Android Studio中如何获取debug.keystore sha1证书
- Android 修改签名文件keystore文件密码
- 配置 Linux 的访问控制列表(ACL)
- .Net T4 模板 实例(一)
- 用GDB调试程序(六)
- JAVA反射机制
- tornado教程资源
- android读取keystore证书文件
- Nginx透传获取客户端IP地址
- Scala 自学笔记2_类
- Mongo-Hadoop
- C++拾遗--多线程:C语言多线程的引入
- Myql 的严格模式与松散模式设置
- javascript操作字符串函数
- Python单元测试——深入理解unittest
- jQuery UI vs EasyUI