Here's another common method of doing encryption, treating the strings as byte arrays and doing XOR operations on the bytes.
String plainText = "Hello World";
String keyText = "klaatubaradanikto";
void setup()
{
size(500,500);
noLoop();
}
void draw()
{
println("Plain: " + plainText);
println("Encrypted: " + encrypt(plainText));
println("Decrypted: " + decrypt(encrypt(plainText)));
}
String encrypt(String plain)
{
byte[] crypted = plain.getBytes();
byte[] keyBytes = keyText.getBytes();
for (int i = 0; i < crypted.length; ++i) {
crypted[i] ^= keyBytes[i % keyBytes.length];
}
return new String(crypted);
}
String decrypt(String plain)
{
return encrypt(plain); // decryption is symmetrical in this example
}