Files
cryptoseals/backend/src/main/java/com/crypto/Caesar.java
Francesco Mancuso 6477512940
All checks were successful
Deploy / trigger (push) Successful in 24s
iDropped my alph in hot oil
Signed-off-by: Francesco Mancuso <hello@francescomancuso.it>
2026-04-21 13:55:31 +02:00

43 lines
1.4 KiB
Java

package com.crypto;
public class Caesar {
public static String encode(String plaintext, int key){
String alphabet = "abcdefghijklmnopqrstuvwxyz";
char encoded[] = plaintext.toCharArray();
for(int i = 0; i < plaintext.length(); i++){
if (alphabet.indexOf(encoded[i]) == -1) continue;
encoded[i] = alphabet.charAt((alphabet.indexOf(encoded[i]) + key) % 26);
}
return String.valueOf(encoded);
}
public static String[] decode(String ciphertext){
String alphabet = "abcdefghijklmnopqrstuvwxyz";
char encoded[] = ciphertext.strip().toCharArray();
char decoded[] = new char[ciphertext.length()];
String bruteforce[] = new String[26];
for(int key = 0; key < alphabet.length(); key++){
for(int i = 0; i < ciphertext.length(); i++){
if (alphabet.indexOf(encoded[i]) == -1) {
decoded[i] = encoded[i];
continue;
}
char current = encoded[i];
int pos = alphabet.indexOf(current);
if(pos != -1) {
int newpos = (pos + key) % 26;
decoded[i] = alphabet.charAt(newpos);
} else
decoded[i] = current;
}
bruteforce[key] = String.valueOf(decoded);
}
return bruteforce;
}
}