How To Code - Caesar Cipher | Cäsar Verschlüsselung in Java | Typecasting in Java

in #java5 years ago

How To Code - Caesar Cipher | Cäsar Verschlüsselung in Java

Was ist die Caesar-Verschlüsselung?

Die Caesar-Verschlüsselung ist ein einfaches symmetrisches Verschlüsselungsverfahren, das auf der monographischen und monoalphabetischen Substitution basiert. Dies ist eine sehr einfache und Einsteiger-freundliche Verschlüsselung.
Bei der Verschlüsselung wird jeder Buchstabe des Klartexts(Der Text den man verschlüsseln möchte) auf einen Geheimtextbuchstaben abgebildet. Diese Abbildung ergibt sich, indem man die Zeichen eines geordneten Alphabets um eine bestimmte Anzahl nach rechts verschiebt. Das Passwort ist somit die Anzahl, um die die Buchstaben verschoben werden.

Typecasting
Quelle des Bildes

Was ist Typecasting?

Mit Typecasting kann man in Java ASCII-Werte in Buchstaben, oder Buchstaben zurück ASCII-Werte umwandeln. Diese Funktion ruft man mit dem gewünschten Typ vor dem anderen Type in Klammern auf. Hierzu ein Beispiel:

Typecasting Besipiel
public void VonBuchstabeZuZahl(){
        int zahlVonBuchStabeA = (int)'A';
        System.out.println(zahlVonBuchStabeA);
    }
    public void VonZahlZuBuchstabe(){
        int BuchstabeVonZahl65 = (char)65;
        System.out.println(BuchstabeVonZahl65);
    }

Um einen Text zu verschlüsseln muss man eine Instanz/ ein Objekt dieser Klasse erstellen und die Methode encryptChar() verwenden, zum entschlüsseln decryptChar().

Der fertige Code

public class Caesar{
    //CONSTRUKTOR
    public Caesar(){
    }
    //ENCRYPTING
    public String encryptString(String text, int password){
        String ret = "";
        for(int i = 0; i < text.length(); i++){
            ret += encryptChar(text.charAt(i), password);
        }
        return ret;
    }
    private char encryptChar(char chr, int password){
        return (char)((int)chr + password);
    }
    //DECRYPTING
    public String decryptString(String text, int password){
        String ret = "";
        for(int i = 0; i < text.length(); i++){
            ret += decryptChar(text.charAt(i), password);
        }
        return ret;
    }
    private char decryptChar(char chr, int password){
        return (char)((int)chr - password);
    }
}

Wenn es dir geholfen hat lass doch bitte ein Upvote da! Ansonsten schreibe mir bitte konstruktive Kritik.

Sort:  

Congratulations @derelias! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 10 as payout for your posts. Your next target is to reach a total payout of 50

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

You can upvote this notification to help all Steemit users. Learn why here!

Hello @derelias! This is a friendly reminder that you have 3000 Partiko Points unclaimed in your Partiko account!

Partiko is a fast and beautiful mobile app for Steem, and it’s the most popular Steem mobile app out there! Download Partiko using the link below and login using SteemConnect to claim your 3000 Partiko points! You can easily convert them into Steem token!

https://partiko.app/referral/partiko

Coin Marketplace

STEEM 0.27
TRX 0.13
JST 0.032
BTC 62699.43
ETH 2963.61
USDT 1.00
SBD 3.61