Last active
January 19, 2023 20:55
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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));getMessagesWithUserdecryptwenn der text aus dem Json kommtsendMessageWie 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.