Skip to content

Instantly share code, notes, and snippets.

@Friedjof
Last active January 19, 2023 20:55
Show Gist options
  • Select an option

  • Save Friedjof/98b250f3148140d7c785bbd1b4b6244e to your computer and use it in GitHub Desktop.

Select an option

Save Friedjof/98b250f3148140d7c785bbd1b4b6244e to your computer and use it in GitHub Desktop.
Dies sind ein paar Methden, die eine ROT13 Verschlüsselung optional im PinguChat anbieten.
public boolean isEncrypted(byte[] message) {
return message[0] == '&';
}
public byte[] encrypt(byte[] data) {
if (!isEncrypted(data)) {
return data;
}
byte[] encryptedData = new byte[data.length];
for (int i = 1; i < data.length; i++) {
int c = data[i];
if (c >= 'a' && c <= 'z') {
c = (c - 'a' + 13) % 26 + 'a';
} else if (c >= 'A' && c <= 'Z') {
c = (c - 'A' + 13) % 26 + 'A';
}
encryptedData[i] = (byte) c;
}
return encryptedData;
}
public String decrypt(String data) {
byte[] dataBytes = data.getBytes();
byte[] d = decrypt(dataBytes);
return new String(d);
}
public byte[] decrypt(byte[] data) {
if (!isEncrypted(data)) {
return data;
}
byte[] decryptedData = new byte[data.length];
for (int i = 0; i < data.length; i++) {
int c = data[i];
if (c >= 'a' && c <= 'z') {
c = (c - 'a' - 13 + 26) % 26 + 'a';
} else if (c >= 'A' && c <= 'Z') {
c = (c - 'A' - 13 + 26) % 26 + 'A';
}
decryptedData[i] = (byte) c;
}
return decryptedData;
}
@Friedjof
Copy link
Author

Friedjof commented Jan 19, 2023

Einbauen:

Bitte erst einbauen bevor du die Aufgabe geschafft hat und sie sicher auf Artemis liegt. Andernfalls sind die Pinguine ganz traurig und hängen dir ein Plagiat an.

  • handleInput()
    Wie hier gezeigt: displayMessage(new String(decrypt(content), StandardCharsets.UTF_8));
  • getMessagesWithUser
    decrypt wenn der text aus dem Json kommt
  • sendMessage
    Wie hier gezeigt: byte[] buf = encrypt(StandardCharsets.UTF_8.encode(message).array());

Anleitung:

Du kannst auch nicht verschlüsselte Nachrichten lesen. Wenn du ein & vor deine Nachricht schreibst, wird der nachfolgende Text verschlüsselt. Kommen Nachrichten mit einem & als erstes Zeichen an, wird der nachfolgende Text auch entschlüsselt. So kann man mit allen schreiben.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment