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:
- Assetmanager AM = context. getassets ();
- Inputstream ins = aM. Open ("robusoft. Cer ");
- Try {
- // Read the certificate
- Certificatefactory cerfactory = certificatefactory. getinstance ("X.509"); // Question 1
- Certificate CER = cerfactory. generatecertificate (INS );
- // Create a certificate library and import the certificate to the certificate library
- Keystore = keystore. getinstance ("PKCS12", "BC"); // Question 2
- Keystore. Load (null, null );
- Keystore. setcertificateentry ("trust", CER );
- Return keystore;
- } Finally {
- INS. Close ();
- }
- // Use your certificate library as the trust certificate library
- Sslsocketfactory socketfactory = new sslsocketfactory (keystore );
- Scheme Sch = new scheme ("HTTPS", fig, 443 );
- // Finished
- Httpclient mhttpclient = new defaulthttpclient ();
- 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! |