ssl socket java_Java SSL/TLS Socket实现

通信端无需向对方证明自己的身份,则称该端处于“客户模式”,否则称其处于“服务器模式”,无论是客户端还是服务器端,都可处于“客户模式”或者“服务器模式”

首先生成服务器端认证证书,使用java自带的keytool工具:

b611c10b4dd6905d60b05cadce4bcef5.png

其中:

-genkey:生成一对非对称密钥

-keyalg:加密算法

-keystore:证书存放路径

-alias:密钥对别名,该别名是公开的

相同的方式,生成客户端认证证书,不过命名为client_rsa.key,别名为clientkey

5aa615651a5b9e2eaa143f50bee9cfbc.png

使用jdk1.5,唯一需要引入的包为log4j-1.2.14.jar

客户端认证:

package com.test.client.auth;

import java.io.FileInputStream;

import java.security.KeyStore;

import java.util.Properties;

import javax.net.ssl.KeyManager;

import javax.net.ssl.KeyManagerFactory;

import javax.net.ssl.SSLContext;

import javax.net.ssl.TrustManager;

import javax.net.ssl.TrustManagerFactory;

import com.test.server.config.Configuration;

public class Auth {

private static SSLContext sslContext;

public static SSLContext getSSLContext() throws Exception{

Properties p = Configuration.getConfig();

String protocol = p.getProperty("protocol");

String sCertificateFile = p.getProperty("serverCertificateFile");

String sCertificatePwd = p.getProperty("serverCertificatePwd");

String sMainPwd = p.getProperty("serverMainPwd");

String cCertificateFile = p.getProperty("clientCertificateFile");

String cCertificatePwd = p.getProperty("clientCertificatePwd");

String cMainPwd = p.getProperty("clientMainPwd");

//KeyStore class is used to save certificate.

char[] c_pwd = sCertificatePwd.toCharArray();

KeyStore keyStore = KeyStore.getInstance("JKS");

keyStore.load(new FileInputStream(sCertificateFile), c_pwd);

//TrustManagerFactory class is used to create TrustManager class.

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");

char[] m_pwd = sMainPwd.toCharArray();

trustManagerFactory.init(keyStore);

//TrustManager class is used to decide weather to trust the certificate

//or not.

TrustManager[] tms = trustManagerFactory.getTrustManagers();

KeyManager[] kms = null;

if(Configuration.getConfig().getProperty("authority").equals("2")){

//KeyStore class is used to save certificate.

c_pwd = cCertificatePwd.toCharArray();

keyStore = KeyStore.getInstance("JKS");

keyStore.load(new FileInputStream(cCertificateFile), c_pwd);

//KeyManagerFactory class is used to create KeyManager class.

KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");

m_pwd = cMainPwd.toCharArray();

keyManagerFactory.init(keyStore, m_pwd);

//KeyManager class is used to choose a certificate

//to prove the identity of the client side.

kms = keyManagerFactory.getKeyManagers();

}

//SSLContext class is used to set all the properties about secure communication.

//Such as protocol type and so on.

sslContext = SSLContext.getInstance(protocol);

sslContext.init(kms, tms, null);

return sslContext;

}

}

客户端主程序:

package com.test.client;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.InetSocketAddress;

import java.net.SocketAddress;

import java.util.Properties;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSocket;

import javax.net.ssl.SSLSocketFactory;

import org.apache.log4j.Logger;

import com.test.client.auth.Auth;

import com.te

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 `socket_set_option` 函数设置 SSL/TLS 加密通信需要先创建一个 SSL/TLS 上下文,然后将该上下文应用到 socket 上。具体的步骤如下: 1. 创建一个 SSL/TLS 上下文,可以使用 OpenSSL 库提供的函数进行创建,比如 `openssl_pkey_new`、`openssl_x509_read`、`openssl_pkcs12_read` 等。 2. 使用 `stream_context_create` 函数创建一个上下文,将 SSL/TLS 上下文作为参数传入。 3. 使用 `stream_socket_enable_crypto` 函数将 SSL/TLS 上下文应用到 socket 上,使其支持 SSL/TLS 加密通信。 下面是一个使用 `socket_set_option` 函数设置 SSL/TLS 加密通信的示例代码: ```php $server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1); // 创建 SSL/TLS 上下文 $sslContext = stream_context_create(); stream_context_set_option($sslContext, 'ssl', 'local_cert', '/path/to/cert.pem'); stream_context_set_option($sslContext, 'ssl', 'local_pk', '/path/to/private/key.pem'); // 将 SSL/TLS 上下文应用到 socket 上 stream_socket_enable_crypto($server, true, STREAM_CRYPTO_METHOD_TLS_SERVER, $sslContext); ``` 在上面的示例代码中,我们使用了 `stream_context_create` 函数创建了一个上下文 `$sslContext`,并使用 `stream_context_set_option` 函数设置了 SSL/TLS 上下文的相关参数,包括证书路径、私钥路径等。然后,我们使用 `stream_socket_enable_crypto` 函数将 SSL/TLS 上下文 `$sslContext` 应用到 socket `$server` 上,使其支持 SSL/TLS 加密通信。 需要注意的是,在使用 SSL/TLS 加密通信时,客户端和服务端都需要支持 SSL/TLS,否则无法建立加密通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值