Almost the same as higuaro solutions, but with a lot of fixes to make it work, the following code tested and working with higuaro doesnβt work very well, as the characters fall into numbers and when you cancel it, you get one number and damage everything:
public String caesarCipherEncrypt(String plain) { String b64encoded = Base64.getEncoder().encodeToString(plain.getBytes()); // Reverse the string String reverse = new StringBuffer(b64encoded).reverse().toString(); StringBuilder tmp = new StringBuilder(); final int OFFSET = 4; for (int i = 0; i < reverse.length(); i++) { tmp.append((char)(reverse.charAt(i) + OFFSET)); } return tmp.toString(); }
To cancel the procedure back:
public String caesarCipherDecrypte(String secret) { StringBuilder tmp = new StringBuilder(); final int OFFSET = 4; for (int i = 0; i < secret.length(); i++) { tmp.append((char)(secret.charAt(i) - OFFSET)); } String reversed = new StringBuffer(tmp.toString()).reverse().toString(); return new String(Base64.getDecoder().decode(reversed)); }
I hope this will be helpful.
Al-mothafar
source share