import java.util.Arrays;
import static   BH.Bytes_Hex.*;

public class XOREncode {
    public static void main(String[] args)  {
        new XOREncode("Code it.");
    }
    public XOREncode(String theString){
        // The string to encode.
        String letters = theString;
        System.out.println("\nThe String: "+letters);
        byte[] theBytes = letters.getBytes();
        System.out.println("As Hex:     "+ ByteArray2HexString(theBytes));
        byte[] theMask=new byte[8];
        byte[] theEncodedBytes=new byte[8];
        for (int i=0;i<8;i++) {
            theMask[i] = (byte) (Math.random() * (256));
            theEncodedBytes[i]=(byte)(theBytes[i]^theMask[i]);
        }
        //As a Hex String
        String theHex= ByteArray2HexString(theEncodedBytes);
        System.out.println("\nTo Send:    "+theHex);
        //int thelength=theHex.length();
        char[] theHexChars=new char[16];
        for (int i=0;i<16;i=i+1) {
            theHexChars[i] = theHex.charAt(i);
        }

        byte[] DecodedBytes= HexArray2ByteArray(theHexChars);
        for (int i=0;i<8;i++) {
            DecodedBytes[i]=(byte)(DecodedBytes[i]^theMask[i]);
        }
        System.out.println("Decoded:    "+ ByteArray2String(DecodedBytes));

    }
}