// CS 1501 Fall 2002 // Handout to help you with your Digital Signature Assignment import java.math.*; import java.util.*; public class BigtoBig { public static void main (String [] args) { Random R = new Random(); // Create some data to demonstrate building of VLI // First we have a 16 character byte array for the timestamp byte [] bogus = {'J','R','2','0','0','2','1','0','1','6','2','1','5','5','2','3'}; // Next we have a random VLI for the modulus. This could be a value between 0 // and 2^128-1. BigInteger modulus = new BigInteger(128, R); // Converting the VLI into a byte array. Since the BigInteger is a signed type, // and the value is nonnegative, an extra byte may be present if the number is // >= 2^127. In this case, we need to skip the first byte when storing in our // byte array, since we are storing only 16 bytes. byte [] bogus2 = modulus.toByteArray(); int delta2 = bogus2.length - 16; // This will be 1 if the extra byte is present, // and 0 otherwise. If an extra byte is present // it will be 00000000, so we don't need to copy it // In your program you will calculate the value below, not pick it randomly BigInteger other = new BigInteger(128, R); byte [] bogus3 = other.toByteArray(); int delta3 = bogus3.length - 16; // Same idea with the conversion as for the // number above // Now we are copying all of the bytes into our single byte array. byte [] bigByte = new byte[48]; for (int i = 0; i < bogus.length; i++) { bigByte[i] = bogus[i]; bigByte[i+16] = bogus2[i+delta2]; bigByte[i+32] = bogus3[i+delta3]; } // Generate a single BigInteger that represents all of the data, so it can // be decrypted. Naturally, the decryption and subsequent encryption are // note done here. BigInteger bigI = new BigInteger(1,bigByte); // Now we are testing to see if we can get back the component data from // our BigInteger byte [] bigCopy = bigI.toByteArray(); // Again we must check for the case int deltaBig = bigCopy.length - 48; // where there is an extra byte in // the array byte [] bogusCopy = new byte[16]; // make the arrays to store the data byte [] bogus2Copy = new byte[16]; byte [] bogus3Copy = new byte[16]; for (int i = 0; i < bogusCopy.length; i++) // fill them with the appropriate { // bytes from the overall array bogusCopy[i] = bigCopy[i+deltaBig]; bogus2Copy[i] = bigCopy[i+deltaBig+16]; bogus3Copy[i] = bigCopy[i+deltaBig+32]; } BigInteger modCopy = new BigInteger(1,bogus2Copy); // Create the BigInteger BigInteger otherCopy = new BigInteger(1,bogus3Copy); // objects System.out.println("Original data: "); // Check to see if data matches for (int i = 0; i < bogus.length; i++) System.out.print((char)bogus[i]); System.out.println(); System.out.println(modulus + ":" + other); System.out.println(); System.out.println("Copies: "); for (int i = 0; i < bogusCopy.length; i++) System.out.print((char)bogusCopy[i]); System.out.println(); System.out.println(modCopy + ":" + otherCopy); } }