Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. import java.io.BufferedOutputStream;
  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.ObjectInputStream;
  12. import java.io.ObjectOutputStream;
  13. import java.net.Socket;
  14. import java.net.URL;
  15. import java.nio.channels.Channels;
  16. import java.nio.channels.FileChannel;
  17. import java.nio.channels.ReadableByteChannel;
  18. import java.security.KeyPair;
  19. import java.security.KeyPairGenerator;
  20. import java.security.NoSuchAlgorithmException;
  21. import java.security.PrivateKey;
  22. import java.security.PublicKey;
  23. import javax.crypto.Cipher;
  24. import org.apache.commons.codec.binary.Base64;
  25. import org.omg.CORBA.portable.OutputStream;
  26.  
  27. public class Client {
  28.  
  29. public static final String PUBLIC_KEY_FILE = "C:\\Users\\Neo\\Desktop\\RSA\\klucz.key";
  30.  
  31. public static byte[] encrypt(String text, PublicKey key) {
  32. byte[] cipherText = null;
  33. try {
  34.  
  35. final Cipher cipher = Cipher.getInstance("RSA");
  36.  
  37. cipher.init(Cipher.ENCRYPT_MODE, key);
  38. cipherText = cipher.doFinal(text.getBytes());
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. return cipherText;
  43. }
  44.  
  45. public static void main(String[] args) throws IOException, ClassNotFoundException {
  46. try(
  47. ReadableByteChannel in=Channels.newChannel(
  48. new URL("http://145.239.90.206/public.key").openStream());
  49. FileChannel out=new FileOutputStream("C:\\Users\\Neo\\Desktop\\eklucz.key").getChannel() )
  50. {
  51.  
  52. out.transferFrom(in, 0, Long.MAX_VALUE);
  53. }
  54. System.out.println("Pobralem klucz publiczny wygenerowany przez serwer (145.239.90.206/klucz.key)");
  55.  
  56. try
  57. {
  58. final String originalText = "Tajna wiadomosc BT";
  59. ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("C:\\Users\\Neo\\Desktop\\klucz.key"));
  60. final PublicKey publicKey = (PublicKey) inputStream.readObject();
  61. final byte[] cipherText = encrypt(originalText, publicKey);
  62. String zaszyfrowana_rsa = new Base64().encodeToString(cipherText);
  63. System.out.println("Zaszyfrowana wiadomosc: " + zaszyfrowana_rsa);
  64. System.out.println("Wysylam do serwera.");
  65. Socket clientSocket = new Socket("145.239.90.206", 8888);
  66.  
  67. DataOutputStream dOut = new DataOutputStream(clientSocket.getOutputStream()); // wysylamam do serwera
  68.  
  69. dOut.writeInt(cipherText.length);
  70. dOut.write(cipherText);
  71. clientSocket.close();
  72.  
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement