Advertisement
VIISeptem

Untitled

Mar 11th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. /**
  2.  * Hausaufgaben
  3.  * Caesar entschlüsseln: Das Entschlüsseln ist das Gleiche wie
  4.  * eine Verschlüsselung mit negativen Key.
  5.  * D.h. a == caesar( caesar(a, 10), -10 )
  6.  */
  7.  
  8. public class Verschluesssung {
  9.     private char caesarBuchstabe(char zeichen, int key) {
  10.         // Type casting: (double) 5.5f;
  11.         int verschluesselt = zeichen + key;
  12.        
  13.         if (verschluesselt > 'Z') {
  14.             verschluesselt = verschluesselt - 26;
  15.         }
  16.        
  17.         return (char) verschluesselt;
  18.     }
  19.         private char normalerBuchstabe(char zeichen, int key) {
  20.         // Type casting: (double) 5.5f;
  21.         int normal = zeichen - key;
  22.        
  23.         if (normal > 'Z') {
  24.             normal = normal + 26;
  25.         }
  26.        
  27.         return (char) normal;
  28.     }
  29.     public String caesar(String text, int key) {
  30.         if (key > 25) {
  31.             return "FEHLER! Key " + key + " ist ungültig!";
  32.         }
  33.        
  34.         String output = ""; // leerer String
  35.        
  36.         for (int i = 0; i < text.length(); i = i + 1) {
  37.             char aktuellesZeichen = text.charAt(i);
  38.             output = output + caesarBuchstabe(aktuellesZeichen, key);
  39.         }
  40.        
  41.         return output;
  42.     }
  43.      public String entCaesar(String text, int key) {
  44.         if (key > 25) {
  45.             return "FEHLER! Key " + key + " ist ungültig!";
  46.         }
  47.        
  48.         String output = ""; // leerer String
  49.        
  50.         for (int i = 0; i < text.length(); i = i + 1) {
  51.             char aktuellesZeichen = text.charAt(i);
  52.             output = output + normalerBuchstabe(aktuellesZeichen, key);
  53.         }
  54.        
  55.         return output;
  56.     }
  57.    
  58.      public void testEntCaesar() {
  59.         System.out.println("FEHLER! Key 75 ist ungültig <-> " + entCaesar("", 75) );
  60.         System.out.println("A <-> " + entCaesar("B", 1) );
  61.         System.out.println("Y <-> " + entCaesar("Z", 1) );
  62.         System.out.println("HALLO <-> " +entCaesar("LEPPS", 4) );
  63.          System.out.println("HALLO <-> " + entCaesar("LEPPS", 4) );
  64.     }
  65.    
  66.     public void testCaesar() {
  67.         System.out.println("FEHLER! Key 75 ist ungültig <-> " + caesar("", 75) );
  68.         System.out.println("B <-> " + caesar("A", 1) );
  69.         System.out.println(" <-> " + caesar("Z", 1) );
  70.         System.out.println(caesar("ABCDEFQRSTUVWXYZ", 15) );
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement