// EncryptClasses.java

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class EncryptClasses
{
  static public void main( String args[] ) throws Exception {
    String keyFilename = args[0];
    String algorithm = "DES";

    // Generate the key
    SecureRandom sr = new SecureRandom();
    byte rawKey[] = Util.readFile( keyFilename );
    DESKeySpec dks = new DESKeySpec( rawKey );
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( algorithm );
    SecretKey key = keyFactory.generateSecret( dks );

    // Create the cipher object, which is used to do the
    // actual encryption
    Cipher ecipher = Cipher.getInstance( algorithm );
    ecipher.init( Cipher.ENCRYPT_MODE, key, sr );

    // For each class file specified on the command line,
    // encrypt the contents and write it back out
    for (int i=1; i<args.length; ++i) {
      String filename = args[i];

      // Read in the class file
      byte classData[] = Util.readFile( filename );

      // Encrypt it
      byte encryptedClassData[] = ecipher.doFinal( classData );

      // Write the encrypted version back out
      Util.writeFile( filename, encryptedClassData );

      System.out.println( "Encrypted "+filename );
    }
  }
}