博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
生成android自签名证书流程
阅读量:7057 次
发布时间:2019-06-28

本文共 2422 字,大约阅读时间需要 8 分钟。

  hot3.png

There is a dearth of SDK documentation on how to work with SSL connections on Android with self-signed certificate. Here is a method that stores a self-signed certificate in the application resource and then later uses that certificate for SSL connections.

1. We create a self-signed server certificate for our SSL server:

keytool -genkey -dname "cn=ssltest, ou=test, o=example, c=US"    -alias ssltest -keypass ssltest -keystore c:\test\ssltest.keystore    -storepass ssltest -validity 180

2. We export the certificate to a file:

keytool -export -alias ssltest -keystore c:\test\ssltest.keystore    -file c:\test\ssltest.cer -storepass ssltest -keypass ssltest

3. Since Android uses the provider from , we  the provider jar bcprov-jdk16-145.jar from BC and store it at C:\androidproject\libs.
4. Now, we import the server certificate to our Android project as a :

keytool -import -alias ssltestcert -file C:\test\ssltest.cer    -keypass ssltestcert -keystore C:\androidproject\res\raw\ssltestcert    -storetype BKS -storepass ssltestcert    -providerClass org.bouncycastle.jce.provider.BouncyCastleProvider    -providerpath c:\androidproject\libs\bcprov-jdk16-145.jar

Note that we give it a store type BKS.

If you use the Eclipse ADK, the ADK will automatically create a resource idssltestcert after you refresh the project.
5. We can now use the server certificate in our Java program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Load the self-signed server certificate
char
[] passphrase =
"ssltestcert"
.toCharArray();
KeyStore ksTrust = KeyStore.getInstance(
"BKS"
);
ksTrust.load(context.getResources().openRawResource(R.raw.ssltestcert),
             
passphrase);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
tmf.init(ksTrust);
 
// Create a SSLContext with the certificate
SSLContext sslContext = SSLContext.getInstance(
"TLS"
);
sslContext.init(
null
, tmf.getTrustManagers(),
new
SecureRandom());
 
// Create a HTTPS connection
URL url =
new
URL(
"https"
,
"10.0.2.2"
,
8443
,
"/ssltest"
);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
 
/* Uncomment the following line of code if you want to skip SSL */
/* hostname verification.  But it should only be done for testing. */
/* See */
/* conn.setHostnameVerifier(new NullVerifier()); */
 
conn.setSSLSocketFactory(sslContext.getSocketFactory());

转载于:https://my.oschina.net/leegq/blog/203681

你可能感兴趣的文章
java.util.Date、java.sql.Date、Time、Timestamp
查看>>
从CentOS风波谈起:Linux企业版如何选择?
查看>>
安装zabbix2.4.8遇到的一些错误
查看>>
Linux:在终端中查看图片和电影
查看>>
我的友情链接
查看>>
位运算实现整数加法
查看>>
实现级联查询
查看>>
js时钟
查看>>
java字符 字符串
查看>>
iterator (迭代器)的应用的用法(一)
查看>>
Java 接口代理
查看>>
关于treeView1_AfterSelect方法中节点的使用(代码介绍)
查看>>
ORA-16009: invalid redo transport destination
查看>>
Oracle 12c ORA-01516: nonexistent log file, data file, or temporary file "10"
查看>>
一对一映射的三种方式以及对lazyload的特别关注
查看>>
麦进斗:magento如何安装子主题
查看>>
Stack Based Windows Buffer Overflow Tutorial
查看>>
单双链表,通过指针变动交换相邻元素
查看>>
redhat中设置环境变量PATH的方法
查看>>
在bootstrap的modal中使用popover和tooltip
查看>>