# Decrypt caesar cipher

**URL:** https://discourse.processing.org/t/decrypt-caesar-cipher/22131
**Category:** Coding Questions
**Tags:** homework
**Created:** [June 24, 2020, 4:18pm UTC](https://discourse.processing.org/t/decrypt-caesar-cipher/22131 "2020-06-24T16:18:53Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![homeCoder](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@homeCoder](https://discourse.processing.org/u/homeCoder)
#### Post date: [June 24, 2020, 4:18pm UTC](https://discourse.processing.org/t/decrypt-caesar-cipher/22131/1 "2020-06-24T16:18:53Z")

</div>

thank you thank you very much

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [June 24, 2020, 8:24pm UTC](https://discourse.processing.org/t/decrypt-caesar-cipher/22131/2 "2020-06-24T20:24:35Z")

</div>

I change to this and it seems to work:  
`cBuchstabe = shift(previous, N_MAX_CHARS-move);`

I have modified your code a little bit, specifically I removed the bulky array and did a simple substitution. Full code below.

```java

final int N_MAX_CHARS=26;

String eingabeText = "katze";
String geheimText;                       
String entEingabeText = "";              
int schluessel = 3; //Caesar cipher key

void setup(){
  code();        
  entEingabeText = decode(geheimText, schluessel);      
  println("eingabeText: " + eingabeText);
  println("geheimText: " + geheimText);
  println("entschlüsselter Text: " + entEingabeText);
}

void code(){
  char cBuchstabe;              
  geheimText = "";              

  for (int i=0; i < eingabeText.length(); i++)
  {

    char next = eingabeText.charAt(i);            
    cBuchstabe = shift(next, schluessel);          
    geheimText = geheimText + cBuchstabe;
  }
  println(eingabeText, "=>", geheimText);
}

String decode(String iGeheim, int move){
  char cBuchstabe;
  String oKlartext = "";
  for (int i=0; i < eingabeText.length(); i++)
  {
    char previous = geheimText.charAt(i);
    cBuchstabe = shift(previous, N_MAX_CHARS-move);
    oKlartext = oKlartext + cBuchstabe;
  }
  return oKlartext;
}                                         

char shift(char buchstabe, int move){
  int j = 0;              
  int pos = 0; 

  j=buchstabe-'a';
  pos = (j+move)%N_MAX_CHARS; 

  char newBuchstabe = char(pos+'a');    
  println(buchstabe, "=>", newBuchstabe);
  return newBuchstabe;
}

```
