Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Hausaufgaben
- * Caesar entschlüsseln: Das Entschlüsseln ist das Gleiche wie
- * eine Verschlüsselung mit negativen Key.
- * D.h. a == caesar( caesar(a, 10), -10 )
- */
- public class Verschluesssung {
- private char caesarBuchstabe(char zeichen, int key) {
- // Type casting: (double) 5.5f;
- int verschluesselt = zeichen + key;
- if (verschluesselt > 'Z') {
- verschluesselt = verschluesselt - 26;
- }
- return (char) verschluesselt;
- }
- private char normalerBuchstabe(char zeichen, int key) {
- // Type casting: (double) 5.5f;
- int normal = zeichen - key;
- if (normal > 'Z') {
- normal = normal + 26;
- }
- return (char) normal;
- }
- public String caesar(String text, int key) {
- if (key > 25) {
- return "FEHLER! Key " + key + " ist ungültig!";
- }
- String output = ""; // leerer String
- for (int i = 0; i < text.length(); i = i + 1) {
- char aktuellesZeichen = text.charAt(i);
- output = output + caesarBuchstabe(aktuellesZeichen, key);
- }
- return output;
- }
- public String entCaesar(String text, int key) {
- if (key > 25) {
- return "FEHLER! Key " + key + " ist ungültig!";
- }
- String output = ""; // leerer String
- for (int i = 0; i < text.length(); i = i + 1) {
- char aktuellesZeichen = text.charAt(i);
- output = output + normalerBuchstabe(aktuellesZeichen, key);
- }
- return output;
- }
- public void testEntCaesar() {
- System.out.println("FEHLER! Key 75 ist ungültig <-> " + entCaesar("", 75) );
- System.out.println("A <-> " + entCaesar("B", 1) );
- System.out.println("Y <-> " + entCaesar("Z", 1) );
- System.out.println("HALLO <-> " +entCaesar("LEPPS", 4) );
- System.out.println("HALLO <-> " + entCaesar("LEPPS", 4) );
- }
- public void testCaesar() {
- System.out.println("FEHLER! Key 75 ist ungültig <-> " + caesar("", 75) );
- System.out.println("B <-> " + caesar("A", 1) );
- System.out.println(" <-> " + caesar("Z", 1) );
- System.out.println(caesar("ABCDEFQRSTUVWXYZ", 15) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement