/**
* COMP3441/9441 Assignment 2
* Session 2, 2003
* * This class implements the messages sent between Alice and Bob. * The message are stored as an array of bytes. */ import java.io.UnsupportedEncodingException; import java.math.BigInteger; public class Message { /** * Constant defining the heads string as "heads" */ public static final String HEADS = "heads"; /** * Constant defining the tails string as "tails" */ public static final String TAILS = "tails"; /** * Constant defining the charset used to convert from bytes to String */ public static final String CHARSET = "US-ASCII"; /** * Variable to store the message */ private byte[] message; /** * Variable to store the length of the random string */ private int randomLength; /** * Constructor to create a message from a String with value * heads or tails (Message.HEADS or Message.TAILS), and an array of random * bytes which must be generated from a SecureRandom object. * * @param headsOrTails must contain either Message.HEADS or Message.TAILS * @param randomBytes an array of random bytes, generated using the * SecureRandom class * @param randomLength the length of the random string */ public Message (String headsOrTails, byte[] randomBytes) { try { // convert the headsOrTails argument to byte array byte[] headsOrTailsBytes = headsOrTails.getBytes(Message.CHARSET); // set the randomLength variable of this Message this.randomLength = randomBytes.length; // copy from the random byte array and the headsOrTails array to message this.message = new byte[headsOrTailsBytes.length + randomBytes.length]; System.arraycopy(headsOrTailsBytes, 0, this.message, 0, headsOrTailsBytes.length); System.arraycopy(randomBytes, 0, this.message, headsOrTailsBytes.length, randomBytes.length); } catch (Throwable t) { t.printStackTrace(); } } /** * Constructor to create a message from an array of bytes. * * @param messageBytes the message as an array of bytes */ public Message (byte[] messageBytes) { this.message = messageBytes; this.randomLength = messageBytes.length - Message.HEADS.length(); } /** * Constructor to create a message from a BigInteger * * @param messageBytes the message as a BigInteger */ public Message (BigInteger messageBytes) { this.message = messageBytes.toByteArray(); this.randomLength = messageBytes.toByteArray().length - Message.HEADS.length(); } /** * Returns this message as an array of bytes. * * @return this message as an array of bytes */ public byte[] getMessageBytes () { return this.message; } /** * Returns this message as a BigInteger * * @return this message as a BigInteger */ public BigInteger getMessage () { return (new BigInteger(this.message)); } /** * Returns the value of this message, excluding the random string. I.e. * it returns Message.HEADS or Message.TAILS. If, however, this message is * encrypted, it will only return gibberish. * * @return Message.HEADS, Message.TAILS, or gibberish */ public String getHeadsOrTails () { String headsOrTails = null; try { headsOrTails = new String(this.message, 0, Message.HEADS.length(), Message.CHARSET); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return headsOrTails; } /** * Returns the random string part of the message. Again, if the message * is encrypted, it will return gibberish. * * @return the random string, or gibberish */ public byte[] getRandomBytes () { byte[] randomBytes = new byte[this.randomLength]; System.arraycopy(this.message, Message.HEADS.length(), randomBytes, 0, randomBytes.length); return randomBytes; } }