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

Aus VoWi
Zur Navigation springen Zur Suche springen

TCP[Bearbeiten | Quelltext bearbeiten]

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

Server[Bearbeiten | Quelltext bearbeiten]

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

Client[Bearbeiten | Quelltext bearbeiten]

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

UDP[Bearbeiten | Quelltext bearbeiten]

Send[Bearbeiten | Quelltext bearbeiten]

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

Receive[Bearbeiten | Quelltext bearbeiten]

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[Bearbeiten | Quelltext bearbeiten]

String[Bearbeiten | Quelltext bearbeiten]

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[Bearbeiten | Quelltext bearbeiten]

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[Bearbeiten | Quelltext bearbeiten]

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[Bearbeiten | Quelltext bearbeiten]

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

public interface INameserverRemote extends Remote {}

Hashing[Bearbeiten | Quelltext bearbeiten]

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[Bearbeiten | Quelltext bearbeiten]

import java.util.Base64;

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

Random generation[Bearbeiten | Quelltext bearbeiten]

import java.security.SecureRandom;

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

Symmetric encryption (AES)[Bearbeiten | Quelltext bearbeiten]

Encrypting[Bearbeiten | Quelltext bearbeiten]

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[Bearbeiten | Quelltext bearbeiten]

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)[Bearbeiten | Quelltext bearbeiten]

Encrypting[Bearbeiten | Quelltext bearbeiten]

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

Decrypting[Bearbeiten | Quelltext bearbeiten]

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