Skip to content

Instantly share code, notes, and snippets.

@cangevine
Last active August 29, 2015 14:06
Show Gist options
  • Save cangevine/4375f5fd65bd26e294b3 to your computer and use it in GitHub Desktop.
Save cangevine/4375f5fd65bd26e294b3 to your computer and use it in GitHub Desktop.
String alphabet = "abcdefghijklmnopqrstuvwxyz";
void setup() {
String plaintext = "happy monday, everyone!";
String ciphertext = encrypt(plaintext, 1);
println(ciphertext);
}
void draw() {
}
String encrypt(String msg, int k) {
String encryptedMsg = "";
for (int i = 0; i < msg.length(); i++) {
char ltr = msg.charAt(i);
int plainIndex = alphabet.indexOf(ltr);
if (plainIndex == -1) {
encryptedMsg = encryptedMsg + ltr;
} else {
int cipherIndex = plainIndex + k;
if (cipherIndex > 26) {
cipherIndex = cipherIndex % 26;
}
char cipherLtr = alphabet.charAt(cipherIndex);
encryptedMsg = encryptedMsg + cipherLtr;
}
}
return encryptedMsg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment