简体   繁体   English

Java-通过套接字发送证书

[英]Java - Sending certificate through socket

i need to send a v3certificate from the server to the client using socket. 我需要使用套接字从服务器向客户端发送v3证书。 To do this: server side, i generate a certificate which i encode with base64.encode , then i send it to the client. 为此:服务器端,我生成一个证书,并使用base64.encode进行编码,然后将其发送给客户端。 Client side, i receive the string which contain the certificate, 客户端,我收到包含证书的字符串,

Server code: 服务器代码:

 X509Certificate certificate = ...;
 sendAnswer(new String(certificate.getEncoded()));

public static void sendAnswer(String ans) {
    try {
        s.shutdownInput();
        PrintWriter output = new PrintWriter(s.getOutputStream(), true);
        output.println(new String(Base64.encode(ans.getBytes())));
        output.close();

    } catch (IOException ex) {
        Logger.getLogger(serverThread.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Client code 客户代码

 String value = sendMessage(..);//method which receive the certificate from the server

 InputStream inStream = null;
 X509Certificate cert=null;
 inStream = new ByteArrayInputStream(value.getBytes());
 CertificateFactory cf = CertificateFactory.getInstance("X.509","BC");
 cert = (X509Certificate)cf.generateCertificate(inStream);

public static String sendMessage(String url, int port, String tag, byte[] mex1) {

    Socket link;
    String reply = "";

    byte[] replyDec = null;

        link = new Socket(InetAddress.getByName(url), port);
        InputStream i = null;
        try {
            i = link.getInputStream();
        } catch (IOException ex) {
            Logger.getLogger(ClientApp.class.getName()).log(Level.SEVERE, null, ex);
        }
        Scanner input = new Scanner(i);

        while (input.hasNextLine()) {
            reply += input.nextLine();
        }
        replyDec = Base64.decode(reply);
        input.close();
        link.close();


    return new String(replyDec);
}

Almost everything works, in the client side if i print the string i receive i get a text which contain extra character and the certificate data. 在客户端,如果我打印字符串,几乎所有的东西都可以工作,我会收到包含额外字符和证书数据的文本。 But it gives me an error when creating the certificate, client side. 但是在创建证书(客户端)时给了我一个错误。 This is the error: 这是错误:

java.security.cert.CertificateException: java.io.IOException: DER length more than 4 bytes: 111 at org.bouncycastle.jce.provider.JDKX509CertificateFactory.engineGenerateCertificate(Unknown Source) at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:322)

and this is the line from which it comes from 这就是它的来源

cert = (X509Certificate) cf.generateCertificate(inStream);

Anyone can help me? 有人可以帮帮我吗?

Thanks in advance 提前致谢

丢弃所有内容并使用SSL,SSL已经完成了所有工作。

You can send the certificate through socket by stream of bytes: 您可以按字节流通过套接字发送证书:

in sender side after configuration of socket: 配置套接字后,在发送方:

ObjectOutputStream toServer;
toServer = new ObjectOutputStream(socket.getOutputStream());
byte[] frame = theCertificate.getEncoded();
toServer.writeObject(frame);

in receiver side after configuration of socket: 配置插座后,在接收器侧:

ObjectInputStream fromClient;
fromClient = new ObjectInputStream(socket.getInputStream());
byte[] cert = fromClient.readObject();
java.security.cert.Certificate jsCert = CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(cert));

now you can use this certificate. 现在您可以使用此证书。 for example as retrieving the public key: 例如,检索公共密钥:

PublicKey thepublicKey = jsCert.getPublicKey();

It looks possible that your problem might be due to using a PrintWriter to send, and possibly something different to read (scanner). 您的问题似乎可能是由于使用PrintWriter发送而造成的,并且可能与读取(扫描仪)不同。 You could try using a StringWriter and StringReader to have a match at either end and then also you can debug if what you send is a perfect match for what you receive. 您可以尝试使用StringWriter和StringReader在两端进行匹配,然后还可以调试发送的内容是否与接收的内容完全匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM