Connect using HTTPS on Android

Source: Internet
Author: User

The project will be used. I have investigated this knowledge in the past two days. There is not much time. I would like to give you a brief summary to help unfamiliar friends get started quickly and shorten the process of searching for treasures in the sea of search engines.
In addition to the android development environment, Tomcat is also ready. I used 6.0, and other versions should be similar.
The overall step is 1. Use keystore to generate the key used by the server, use it to configure the Server 2. Import the public key from the client and add it to the trusted certificate library. The following are references.

1. cryptography BASICS (for example, if I am a non-specialist, I need to take a look at it and I still know why)
Http://www.williamlong.info/archives/499.html

2. keytool usage and tomcat configuration
If you are good at English, please read the Tomcat documentation and the SSL section;
If you are not good at English, please refer to the Chinese document or read this article:
Http://www.cn-java.com/www1? Action-viewnews-itemid-3555
Note: This article is relatively old, and the specific configuration method cannot be copied, just refer to its steps. For specific configuration, refer to your Tomcat document, SSL section.

3. Android Configuration
If you do not need to verify the server certificate, do it here
Http://elsila.blog.163.com/blog/static/17319715820101128832427/

If you need to verify the server certificate (which can prevent phishing attacks), I am doing this, but I still have some questions:
A. Export the public key. Access Tomcat through HTTPS in the browser, view its certificate, and save it as a file (saved in X.509 format: XXXX. CER)
B. Import the public key. Put XXXX. Cer in the assets folder of Android to read the certificate through code at run time, leaving two questions for Daniel:

  1. Assetmanager AM = context. getassets ();
  2. Inputstream ins = aM. Open ("robusoft. Cer ");
  3. Try {
  4. // Read the certificate
  5. Certificatefactory cerfactory = certificatefactory. getinstance ("X.509"); // Question 1
  6. Certificate CER = cerfactory. generatecertificate (INS );
  7. // Create a certificate library and import the certificate to the certificate library
  8. Keystore = keystore. getinstance ("PKCS12", "BC"); // Question 2
  9. Keystore. Load (null, null );
  10. Keystore. setcertificateentry ("trust", CER );
  11. Return keystore;
  12. } Finally {
  13. INS. Close ();
  14. }
  15. // Use your certificate library as the trust certificate library
  16. Sslsocketfactory socketfactory = new sslsocketfactory (keystore );
  17. Scheme Sch = new scheme ("HTTPS", fig, 443 );
  18. // Finished
  19. Httpclient mhttpclient = new defaulthttpclient ();
  20. Mhttpclient. getconnectionmanager (). getschemeregistry (). Register (Sch );

Copy code



Question 1: "PKCS12" cannot be used here.
Question 2: "jks" cannot be used here.

In fact, I just don't know how many characters should be written in these two places, which seems to involve patents or something. I have also tried to export the certificate to other formats and tried it for a long time. This is the first channel.


Thanks to the landlord for solving a very troublesome problem, that is, how to import the trust certificate in httpclient. I also answered two questions from the landlord,

Question 1: PKCS12 and jks are the keystore type, not the certificate type, so X.509 cannot be replaced by PKCS12.

Question 2: The keystore type supported on the Android platform seems to have only PKCS12 and does not support jks. Therefore, jks cannot be used instead of PKCS12, but can be replaced on the Windows platform.


Hope to help you!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.