A code snippet is here:
public String encode(String s) {
String encodingKey="sixteencharacter";
String result="DEFAULT STRING TO SEND";
try {SecretKeySpec key = new SecretKeySpec(encodingKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
cipher.init(Cipher.ENCRYPT_MODE, key);
result = new String(cipher.doFinal(s.getBytes()));
} catch (Exception e) {System.out.println("ERROR WHILE ENCODING");}
return result;
}
public String decode(String s) {
String decodingKey="sixteencharacter";
String result="DEFAULT STRING TO SEND";
try {SecretKeySpec key = new SecretKeySpec(decodingKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
cipher.init(Cipher.DECRYPT_MODE, key);
result = new String(cipher.doFinal(s.getBytes()));
} catch (Exception e) {System.out.println("ERROR WHILE DECODING");e.printStackTrace();}
return result;
}
And when I try to call a line like, say,
String s=decode(encode("WELCOME:BOB"));
I get the error.
Other Strings I have tried that do not work are "WELCOME:ALICE", "WELCOME:NEMO". However, I have also found strings that do decode properly, such as "WELCOME:HI", "WELCOME:DAVE", "WELCOME:SQUARE", "WELCOME:NOBODY".
I do not know why some strings work and some don't. I modeled the decoding method to mirror the encoding, so I am at a loss to explain any of these results.
Any help is appreciated!