http://wangtaoenter.iteye.com/blog/1336614
Android 支持HTTPS
关键点:
1.Android仅支持BouncyCastle,BKS密库
2.生成密钥
keytool -genkey -alias tomcat -keyalg RSA -keystore server.keystore -validity 3600
keytool -export -alias tomcat -file server.cer -keystore server.keystore -storepass 123456
keytool -import -alias tomcat -file server.cer -keystore server_trust.keystore -storepass 123456 -storetype BKS -providername "BC"
参见:http://anjxue.iteye.com/blog/1140275
code:
- public class HttpsDemo extends Activity implements OnClickListener
- {
- private static final String TAG = "HttpsDemo";
- private static final String HTTS_URL = "https://192.168.7.39:8443/";
- private EditText editText;
- private Button button;
- /**
- * 私钥密码
- */
- private static final String CLIENT_KET_PASSWORD = "123456";
- /**
- * 信任证书密码
- */
- private static final String CLIENT_TRUST_PASSWORD = "123456";
- /**
- * 使用协议
- */
- private static final String CLIENT_AGREEMENT = "TLS";
- /**
- * 密钥管理器
- */
- private static final String CLIENT_KEY_MANAGER = "X509";
- /**
- * 信任证书管理器
- */
- private static final String CLIENT_TRUST_MANAGER = "X509";
- /**
- * 密库,这里用的是BouncyCastle密库
- */
- private static final String CLIENT_KEY_KEYSTORE = "BKS";
- /**
- * 密库,这里用的是BouncyCastle密库
- */
- private static final String CLIENT_TRUST_KEYSTORE = "BKS";
- private AssetManager mAssetManager = null;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.https_demo);
- mAssetManager = getAssets();
- editText = (EditText) findViewById(R.id.url_entry_text);
- button = (Button) findViewById(R.id.go_url_btn);
- editText.setText(HTTS_URL);
- button.setOnClickListener(this);
- }
- @Override
- public void onClick(View v)
- {
- connect(editText.getText().toString());
- }
- private void connect(String httpsUrl)
- {
- java.net.URL url = null;
- HttpsURLConnection conn = null;
- InputStream inputs = null;
- try
- {
- //取得SSL的SSLContext实例
- SSLContext sslContext = SSLContext.getInstance(CLIENT_AGREEMENT);
- //取得KeyManagerFactory实例
- KeyManagerFactory keyManager = KeyManagerFactory
- .getInstance(CLIENT_KEY_MANAGER);
- //取得TrustManagerFactory的X509密钥管理器
- TrustManagerFactory trustManager = TrustManagerFactory
- .getInstance(CLIENT_TRUST_MANAGER);
- //取得BKS密库实例
- KeyStore keyKeyStore = KeyStore.getInstance(CLIENT_KEY_KEYSTORE);
- KeyStore trustKeyStore = KeyStore
- .getInstance(CLIENT_TRUST_KEYSTORE);
- //加载证书和私钥,通过读取资源文件的方式读取密钥和信任证书(kclient:密钥;lt_client:信任证书)
- InputStream is = mAssetManager.open("trust.keystore");
- //kclient:密钥
- keyKeyStore.load(is, CLIENT_KET_PASSWORD.toCharArray());
- is.reset();
- //lt_client:信任证书
- trustKeyStore.load(is, CLIENT_TRUST_PASSWORD.toCharArray());
- is.close();
- //初始化密钥管理器、信任证书管理器
- keyManager.init(keyKeyStore, CLIENT_KET_PASSWORD.toCharArray());
- trustManager.init(trustKeyStore);
- //初始化SSLContext
- sslContext.init(keyManager.getKeyManagers(),
- trustManager.getTrustManagers(), null);
- url = new URL(HTTS_URL);
- conn = (HttpsURLConnection) url.openConnection();
- conn.setSSLSocketFactory(sslContext.getSocketFactory());
- conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
- conn.setDoInput(true);
- conn.setDoOutput(true);
- conn.setRequestProperty("Accept", "*/*");
- conn.setRequestProperty("Pragma", "No-cache");
- conn.setRequestProperty("Cache-Control", "no-cache");
- conn.setRequestProperty("connection", "keep-alive");
- conn.setRequestProperty("accept-charset", "utf-8");
- conn.setRequestProperty("Content-Type", "text/xml");
- conn.setConnectTimeout(30000);
- conn.setReadTimeout(30000);
- conn.setRequestMethod("GET");
- // 执行到该句就是开始建立连接并取得连接的响应结果
- int code = conn.getResponseCode();
- Log.i(TAG, "http response code is " + code);
- inputs = conn.getInputStream();
- int size = conn.getContentLength();
- Log.i(TAG, "getContentLength" + size);
- byte[] buf = new byte[10000];
- inputs.read(buf);
- Log.d(TAG, "res:" + new String(buf));
- }
- catch (MalformedURLException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- catch (NoSuchAlgorithmException e)
- {
- e.printStackTrace();
- }
- catch (KeyManagementException e)
- {
- e.printStackTrace();
- }
- catch (KeyStoreException e)
- {
- e.printStackTrace();
- }
- catch (CertificateException e)
- {
- e.printStackTrace();
- }
- catch (UnrecoverableKeyException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (conn != null)
- {
- conn.disconnect();
- }
- }
- }
- public class TrustAnyHostnameVerifier implements HostnameVerifier
- {
- public boolean verify(String hostname, SSLSession session)
- {
- return true;
- }
- }
- }