java 签名证书,在java中使用自签名证书

I want to connect to a sms gateway. I found the following code.

public void smsSender(String username, String password, String to,

String text) throws IOException {

try {

String data = "username=" + username + "&password=" + password

+ "&to=" + to + "&text=" + text;

URL url = new URL("https://sendsms.abc.com:1010/sms.php");

HttpURLConnection urlc = (HttpURLConnection) url.openConnection();

urlc.setRequestMethod("POST");

urlc.setDoOutput(true);

urlc.setRequestProperty("Content-type",

"application/x-www-form-urlencoded");

BufferedWriter br = new BufferedWriter(new OutputStreamWriter(

urlc.getOutputStream()));

br.write(data);

br.flush();

BufferedReader rd = new BufferedReader(new InputStreamReader(

urlc.getInputStream()));

String line;

while (null != ((line = rd.readLine()))) {

output = line;

System.out.println(output);

}

rd.close();

} catch (Exception e) {

e.printStackTrace();

}

}

When i try to connect using this method Eclipse sends an error message.

unable to find valid certification path to requested target

The server that i'm trying to access is using self signed certificate. I'm new to this field. How can i solve this problem. Thanks in advance :)

解决方案

To make remote method invocations over SSL, a client needs to trust the certificate of the server. As you said the server has a self-signed certificate, you client needs to be explicitly configured to trust the certificate else the connection fails.

To create a trust relationship between a client and server's self-signed certificate, follow the steps mentioned below,

First you should get the server certificate on your client side.

For that the way I know of is, i.e. hit the server url in a browser

and get the server's certificate and import it in the browser. There might be other ways of getting the server certificate

but you'll have to explore.

Now export the public key as a certificate from the browser to the

client. let it be server.cer.

Now, create the client keystore

keytool -genkey -alias clientkeys -keyalg RSA -keystore

client.keystore -storepass 123456 -keypass 123456 -dname

"CN=localhost, OU=MYOU, O=MYORG, L=MYCITY, S=MYSTATE, C=MY"

create the client certificate

keytool -export -alias clientkeys -keystore client.keystore -storepass

123456 -file client.cer

Now, import the server certificate to the client trust store.

keytool -import -alias serverCert -keystore client.truststore

-storepass clientcert -file server.cer

now load the client keystore as mentioned in erickson's comment in

the link provided by Werner.

Let me know if things are still not clear. But I suggest you read some documentation on google related to SSL Handshaking between a client and a server.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在JavaMail使用签名证书进行签名,您需要执行以下步骤: 1.生成自签名证书。您可以使用Java的keytool工具生成自签名证书。例如,以下命令可以生成一个名为“mycert”的自签名证书: ``` keytool -genkeypair -alias mycert -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore keystore.p12 -validity 3650 ``` 2.将自签名证书添加到Java密钥库。您可以使用以下命令将自签名证书添加到名为“mykeystore”的Java密钥库: ``` keytool -importkeystore -srckeystore keystore.p12 -srcstoretype PKCS12 -destkeystore mykeystore -deststoretype JKS -deststorepass changeit ``` 3.在JavaMail使用签名证书进行签名。以下是一个使用签名证书签名邮件的示例代码: ```java import javax.mail.*; import javax.mail.internet.*; import java.security.Security; import java.util.Properties; public class Mail { public static void main(String[] args) throws Exception { // 设置 Java 安全属性 Security.setProperty("ssl.SocketFactory.provider", "com.sun.net.ssl.internal.ssl.Provider"); Security.setProperty("ssl.SocketFactory.factoryClass", "javax.net.ssl.SSLSocketFactory"); // 创建 JavaMail 会话 Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username", "password"); } }); // 创建邮件消息 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("[email protected]")); message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); message.setSubject("Subject"); // 创建多部分消息 Multipart multipart = new MimeMultipart(); // 创建文本部分 MimeBodyPart textPart = new MimeBodyPart(); textPart.setText("Body"); // 创建签名部分 MimeBodyPart signaturePart = new MimeBodyPart(); signaturePart.setContent("Signature", "application/pgp-signature"); // 将文本部分和签名部分添加到多部分消息 multipart.addBodyPart(textPart); multipart.addBodyPart(signaturePart); // 设置多部分消息为邮件消息的内容 message.setContent(multipart); // 送邮件消息 Transport.send(message); } } ``` 在此示例代码,我们使用了自签名证书进行签名,将签名部分作为多部分消息的一部分添加到消息,然后将多部分消息设置为邮件消息的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值