TU Wien:Verteilte Systeme UE (Rausch)/Codestücke als Prüfungsvorbereitung

From VoWi
Jump to navigation Jump to search

TCP[edit]

import java.net.ServerSocket;
import java.net.Socket;

Server[edit]

ServerSocket serverSocket = new ServerSocket(tcp.port);
Socket socket = serverSocket.accept();

Client[edit]

Socket socket = new Socket(host, tcp.port);

UDP[edit]

Send[edit]

DatagramSocket socket = new DatagramSocket();
byte[] buffer = message.getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(host), port);
socket.send(packet);

Receive[edit]

DatagramSocket socket = new DatagramSocket(port); // port for receiving
byte[] buffer = new byte[4096];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String data = new String(packet.getData(),0, packet.getLength());

Streams[edit]

String[edit]

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String request = reader.readLine();

PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
writer.println(message);
writer.flush();

RMI[edit]

import java.rmi.AlreadyBoundException;
import java.rmi.NoSuchObjectException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.Remote;

Registry registry = LocateRegistry.getRegistry(
  this.registryHost,
  this.registryPort
);
INameserverRemote rootServer = (INameserverRemote) registry.lookup(rootId);


Registry registry = LocateRegistry.createRegistry(config.getInt("registry.port"));

RemoteObject remoteObject = new RemoteObject(); // RemoteObject implements Remote
INameserverRemote server = (INameserverRemote) UnicastRemoteObject.exportObject(remoteObject, 0);
registry.bind(name, server);

Shutdown[edit]

try {
  UnicastRemoteObject.unexportObject(remoteObject, true);
} catch (NoSuchObjectException exception) { }

try {
  registry.unbind(rootId);
} catch (Exception exception) { }

try {
  UnicastRemoteObject.unexportObject(registry, true);
} catch (NoSuchObjectException exception) { }

Remote object[edit]

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface INameserverRemote extends Remote {}

Hashing[edit]

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

SecretKeySpec secretKeySpec = new SecretKeySpec(secret_key, "HmacSHA256");
Mac hMac = Mac.getInstance("HMACSHA256");
hMac.init(secretKeySpec);
hMac.update(message.getBytes());
byte[] hash = hMac.doFinal();

boolean isEqual = MessageDigest.isEqual(byteArray1, byteArray2);

Base64[edit]

import java.util.Base64;

String encoded = Base64.getEncoder().encodeToString(message);
String decoded = new String(Base64.getDecoder().decode(message_b64))

Random generation[edit]

import java.security.SecureRandom;

SecureRandom random = new SecureRandom();
byte challenge[] = new byte[32];
random.nextBytes(challenge);

Symmetric encryption (AES)[edit]

Encrypting[edit]

import javax.crypto.SecretKey;
SecureRandom random = new SecureRandom();
random.nextBytes(iv);

KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecretKey secretKey = kgen.generateKey();

Cipher cipher = Cipher.getInstance(AES_MODE);
IvParameterSpec ivspec = new IvParameterSpec(iv);

cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
byte[] encryptedBytes = cipher.doFinal(data.getBytes());

Decrypting[edit]

Cipher cipher = Cipher.getInstance(AES_MODE);

byte[] decodedBytes = Base64.getDecoder().decode(data_b64);

IvParameterSpec ivspec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);

new String(cipher.doFinal(decodedBytes));

Asymmetric encryption (RSA)[edit]

Encrypting[edit]

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
PublicKey publicKey = ...;
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(data.getBytes());

Decrypting[edit]

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
PrivateKey privateKey = ...;
cipher.init(Cipher.DECRYPT_MODE, privateKey);
String decryptedString = new String(cipher.doFinal(decodedBytes));