public class Main {
public static void main(String[] args) {
String msg = "Josh and Robbie are scrubs";
String enc = "sµ¢OÀA´GÀcµ¼C±¹ÀA£¹À]½£¦C¢";
System.out.println(encrypt(msg));
System.out.println(decrypt(enc));
}
public static String decrypt(String msg)
{
String bin = "";
for(int x = 0; x < msg.length(); x++){
bin += String.format("%08d", Integer.parseInt(Integer.toString(msg.charAt(x), 2)));
}
String str = "";
boolean flip = false;
for(int i = 0; i < bin.length(); i++)
{
if (flip)
{
if (bin.charAt(i) == '1') str += "0";
else str += "1";
}
else str += bin.substring(i, i+1);
if(bin.charAt(i) == '1') flip = true;
else flip = false;
}
String output = "";
for(int x = 0; x <= str.length() - 8; x += 8){
output += (char) Integer.parseInt(str.substring(x, x+8), 2);
}
return output;
}
public static String encrypt(String msg)
{
String bin = "";
for(int x = 0; x < msg.length(); x++){
bin += String.format("%08d", Integer.parseInt(Integer.toString(msg.charAt(x), 2)));
}
String str = "";
boolean flip = false;
for(int i = 0; i < bin.length(); i++)
{
if (flip)
{
if (bin.charAt(i) == '1') str += "0";
else str += "1";
}
else str += bin.substring(i, i+1);
if(bin.charAt(i) == '1') flip = !flip;
}
String output = "";
for(int x = 0; x <= str.length() - 8; x += 8){
output += (char) Integer.parseInt(str.substring(x, x+8), 2);
}
return output;
}
}