소켓을 사용하여 서버에서 클라이언트로 v3certificate를 보내야합니다. 이렇게하려면 : 서버 쪽, 나는 base64.encode로 인코딩 한 인증서를 생성 한 다음 클라이언트에게 보냅니다. 클라이언트 측, 난 인증서가 포함 된 문자열을받을Java - 소켓을 통한 인증서 전송
서버 코드 :
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);
}
}
내가 문자열 난을 인쇄하는 경우 클라이언트 코드가
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);
}
거의 모든 클라이언트 측에서 작동 여분의 문자와 인증서 데이터가 포함 된 텍스트를받습니다. 하지만 그것은 클라이언트 쪽 인증서를 만들 때 저에게 오류를줍니다. 이 오류입니다 :
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)
이는 나를 도울 수
cert = (X509Certificate) cf.generateCertificate(inStream);
사람에서 비롯되는 라인입니까?
미리 감사드립니다.
문자열의 사용은 완전히 잘못되었습니다. 인증서는 바이트 배열이며 바이트 배열을 String에 올바르게 저장할 수 없습니다. –