开发者

conversion from publickey fromate to string and vise versa

hi am doing simple tcp client server program. also am using chiper method to encrypt the data transfer.Initially client sending the request to server. At server end it accepts the client and it sends the public key of server to client. In client side it uses the public key of server and开发者_Go百科 encrypt the data using that key. here is the problem what am facing to send the sever public key, am converting public key data type to string and am sending it. but am not able to convert it into public key data type. Is the any inbuild method to convert public key to string and vise versa??how to covert it..please guide me...


You can use the PublicKeys method "getEncoded" which gives you an X509 Encoded representation (Byte[]) of the PublicKey. This array can then be sent using the socket connection.

You also can generate a public key from the encoding like that:

KeyFactory rsaKeyFac = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key);  
RSAPublicKey rsaPubKey = (RSAPublicKey)rsaKeyFac.generatePublic(keySpec);
return new PublicKeyImpl(rsaPubKey);    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜