Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 检测连接是否安全_Android_Ssl Certificate - Fatal编程技术网

Android 检测连接是否安全

Android 检测连接是否安全,android,ssl-certificate,Android,Ssl Certificate,我想检测网络流量是否由Fiddler或类似Fiddler的代理进行监控。如果是这样,我不希望我的应用程序继续进行下去。 为了实现这一目标,我正在做以下事情: 创建密钥库: private KeyStore getKeyStore() { KeyStore keyStore = null; try { // Load CAs from an InputStream // (could be from a resource or ByteArrayIn

我想检测网络流量是否由Fiddler或类似Fiddler的代理进行监控。如果是这样,我不希望我的应用程序继续进行下去。 为了实现这一目标,我正在做以下事情:

  • 创建密钥库:

    private KeyStore getKeyStore() {
        KeyStore keyStore = null;
        try {
            // Load CAs from an InputStream
            // (could be from a resource or ByteArrayInputStream or ...)
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
    
            InputStream caInput = getClass().getResourceAsStream(
                    "/assets/mycert");
    
            Certificate ca;
            try {
                ca = cf.generateCertificate(caInput);
                System.out.println("ca="
                        + ((X509Certificate) ca).getSubjectDN());
    
            } finally {
                caInput.close();
            }
    
            // Create a KeyStore containing our trusted CAs
            String keyStoreType = KeyStore.getDefaultType();
            keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);
    
        } catch (CertificateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (KeyStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return keyStore;
    }
    
  • 创建信任密钥库中CA的信任管理器

    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);
    
  • 创建使用TrustManager的SSLContext

    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);
    
  • 将SSLSocketFactory设置为输出连接

    connection.setSSLSocketFactory(context.getSocketFactory());
    
  • 这总是使连接成功。我不知道这是否是实现我想要的目标的正确方法

    或者还有其他正确的方法吗

    提前谢谢