caTea 블로그

Android SSL 처리 클래스(https) 본문

android

Android SSL 처리 클래스(https)

ZaRas 2015. 6. 17. 16:31
반응형

import java.io.IOException;

import java.net.InetAddress;

import java.net.Socket;

import java.security.cert.X509Certificate;

 

import javax.net.SocketFactory;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSocket;

import javax.net.ssl.SSLSocketFactory;

import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;

 

public class HTTPSSocketFactory extends SSLSocketFactory {

private SSLSocketFactory factory;

String suites[] = {//서버에 설정된 키 넣어주면된다.

"SSL_RSA_WITH_RC4_128_MD5",

};

 

public HTTPSSocketFactory() {

try {

SSLContext sslcontext = null;

 

if (sslcontext == null) {

sslcontext = SSLContext.getInstance("TLSv1");

sslcontext.init(null, new TrustManager[] { new CustomTrustManager() }, new java.security.SecureRandom());

}

 

factory = (SSLSocketFactory) sslcontext.getSocketFactory();

} catch (Exception e) {

e.printStackTrace();

}

}

 

public static SocketFactory getDefault() {

return new HTTPSSocketFactory();

}

 

public Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException {

SSLSocket sock = (SSLSocket)factory.createSocket(socket, s, i, flag);

sock.setEnabledCipherSuites(suites);

return sock;

}

 

public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr1, int j) throws IOException {

SSLSocket sock = (SSLSocket)factory.createSocket(inaddr, i, inaddr1, j);

sock.setEnabledCipherSuites(suites);

return sock;

}

 

public Socket createSocket(InetAddress inaddr, int i) throws IOException {

SSLSocket sock = (SSLSocket)factory.createSocket(inaddr, i);

sock.setEnabledCipherSuites(suites);

return sock;

}

 

public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException {

SSLSocket sock = (SSLSocket)factory.createSocket(s, i, inaddr, j);

sock.setEnabledCipherSuites(suites);

return sock;

}

 

public Socket createSocket(String s, int i) throws IOException {

SSLSocket sock = (SSLSocket)factory.createSocket(s, i);

sock.setEnabledCipherSuites(suites);

return sock;

}

 

public String[] getDefaultCipherSuites() {

return suites;

}

 

public String[] getSupportedCipherSuites() {

return suites;

}

 

public class CustomTrustManager implements X509TrustManager {

public void checkClientTrusted(X509Certificate[] cert, String authType) {

return;

}

 

public void checkServerTrusted(X509Certificate[] cert, String authType) {

return;

}

 

public X509Certificate[] getAcceptedIssuers() {

return new X509Certificate[0];

}

}

 

}




요즘 롤리팝에 문제가 많다고한다.


보안이 강화되었다는건데 https통신을 할때 이 클래스가 필요하다고 한다.


나중에 자세하게 분석해야겠다



HttpsURLConnection conn1 = (HttpsURLConnection) new URL(m_context.getResources().getString(R.string.op_server_url_oper)).openConnection();

conn1.setSSLSocketFactory(new HTTPSSocketFactory());


요렇게 사용하면된다




728x90