Search code examples
javaencryptionencryption-symmetricrot13

How to add more characters and letters to rot13 and Caesar encryption


I want to add the letters "ç," "ğ," "ı," "ö" and "ü" into this encrypter's alphabet, and maybe special chars, too. How can I do that?

    for (int i = 0; i < metin.length(); i++) {
        char harf = metin.charAt(i);
        if       (harf >= 'a' && harf <= 'm') harf += i;
        else if  (harf >= 'A' && harf <= 'M') harf += i;
        else if  (harf >= 'n' && harf <= 'z') harf -= i;
        else if  (harf >= 'N' && harf <= 'Z') harf -= i;
        System.out.print(harf);
    }

Solution

  • You can make the strategy more general like this.

    String text = "abcdefghijklmnopqrstuvwxyz0123456789!$%^&*()äöü";
    
    for (int i = 0; i < text.length(); i++) {
        char ch = text.charAt(i);
        ch--;
        if (ch % 32 < 13)
            ch += 13;
        else if (ch % 32 < 26)
            ch -= 13;
        else if (ch % 32 < 29)
            ch += 3;
        else
            ch -= 3;
        ch++;
        System.out.print(ch);
    }
    

    prints

    nopqrstuvwxyzabcdefghijklm#$%&'()*+,.12[3756ñéÿ