[jboss-cvs] JBossAS SVN: r95946 - projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Nov 3 06:55:59 EST 2009


Author: alesj
Date: 2009-11-03 06:55:59 -0500 (Tue, 03 Nov 2009)
New Revision: 95946

Modified:
   projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Crypter.java
   projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Tools.java
Log:
Proper cryption.

Modified: projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Crypter.java
===================================================================
--- projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Crypter.java	2009-11-03 10:08:16 UTC (rev 95945)
+++ projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Crypter.java	2009-11-03 11:55:59 UTC (rev 95946)
@@ -21,27 +21,67 @@
  */
 package org.jboss.demos.classloader.crypt;
 
-import java.io.File;
+import java.security.PrivateKey;
+import java.security.PublicKey;
 
+import javax.crypto.Cipher;
+
 /**
  * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
  */
 public class Crypter
 {
-   private File keystore;
+   private String xform;
+   private PrivateKey privateKey;
+   private PublicKey publicKey;
 
-   public Crypter(File keystore)
+   private Crypter(String xform)
    {
-      this.keystore = keystore;
+      if (xform == null)
+         xform = "RSA/NONE/PKCS1PADDING";
+
+      this.xform = xform;
    }
 
-   public byte[] encrypt(byte[] bytes)
+   public Crypter(PrivateKey privateKey)
    {
-      return bytes; // TODO - encrypt
+      this(null, privateKey);
    }
 
-   public byte[] decrypt(byte[] bytes)
+   public Crypter(PublicKey publicKey)
    {
-      return bytes; // TODO - decrypt
+      this(null, publicKey);
    }
+
+   public Crypter(String xform, PrivateKey privateKey)
+   {
+      this(xform);
+      this.privateKey = privateKey;
+   }
+
+   public Crypter(String xform, PublicKey publicKey)
+   {
+      this(xform);
+      this.publicKey = publicKey;
+   }
+
+   public byte[] encrypt(byte[] bytes) throws Exception
+   {
+      if (privateKey == null)
+         throw new IllegalArgumentException("No private key, cannot encrypt.");
+
+      Cipher cipher = Cipher.getInstance(xform);
+      cipher.init(Cipher.ENCRYPT_MODE, privateKey);
+      return cipher.doFinal(bytes);
+   }
+
+   public byte[] decrypt(byte[] bytes) throws Exception
+   {
+      if (publicKey == null)
+         throw new IllegalArgumentException("No public key, cannot decrypt.");
+
+      Cipher cipher = Cipher.getInstance(xform);
+      cipher.init(Cipher.DECRYPT_MODE, publicKey);
+      return cipher.doFinal(bytes);
+   }
 }

Modified: projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Tools.java
===================================================================
--- projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Tools.java	2009-11-03 10:08:16 UTC (rev 95945)
+++ projects/demos/microcontainer/trunk/classloader/src/main/java/org/jboss/demos/classloader/crypt/Tools.java	2009-11-03 11:55:59 UTC (rev 95946)
@@ -22,6 +22,13 @@
 package org.jboss.demos.classloader.crypt;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.PrivateKey;
 import java.util.Arrays;
 
 import org.jboss.virtual.VFS;
@@ -33,47 +40,83 @@
 public class Tools
 {
    private File jarToCrypt;
-   private File keyStore;
+   private PrivateKey key;
 
-   public Tools(File jarToCrypt, File keyStore)
+   public Tools(File jarToCrypt, PrivateKey key)
    {
       this.jarToCrypt = jarToCrypt;
-      this.keyStore = keyStore;
+      this.key = key;
    }
 
    public static void main(String[] args)
    {
-      if (args == null || args.length == 0)
+      if (args == null || args.length < 2)
          throw new IllegalArgumentException("Invalid arguments: " + Arrays.toString(args));
 
       File archive = new File(args[0]);
       if (archive.exists() == false)
          throw new IllegalArgumentException("Archive doesn't exist: " + archive);
 
-      File keystore = new File(args[1]);
-      if (keystore.exists() == false)
-         throw new IllegalArgumentException("Keystore doesn't exist: " + keystore);
+      try
+      {
+         File keystore = new File(args[1]);
+         KeyPair kp = readKeyPair(keystore, true);
+         if (keystore.exists() == false)
+            writeKeyPair(keystore, kp);
 
-      Tools tools = new Tools(archive, keystore);
-      tools.crypt();
+         PrivateKey privateKey = kp.getPrivate();
+         Tools tools = new Tools(archive, privateKey);
+         tools.crypt();
+      }
+      catch (Exception e)
+      {
+         System.err.println(e);
+      }
    }
 
-   public void crypt()
+   public void crypt() throws Exception
    {
+      VirtualFile root = VFS.getRoot(jarToCrypt.toURI());
+      Crypter crypter = new Crypter(key);
+      CryptVisitor visitor = new CryptVisitor(root, crypter);
+      File file = visitor.getFile();
+      if (file.renameTo(jarToCrypt))
+      {
+         System.err.println("Cannot rename file: " + file);
+      }
+   }
+
+   public static KeyPair readKeyPair(File keystore, boolean create) throws Exception
+   {
+      boolean exists = keystore.exists();
+      if (exists == false && create == false)
+         throw new IllegalArgumentException("No such file: " + keystore);
+
+      if (exists)
+      {
+         ObjectInputStream ois = new ObjectInputStream(new FileInputStream(keystore));
+         ois.close();
+         return KeyPair.class.cast(ois.readObject());
+      }
+      else
+      {
+         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
+         kpg.initialize(512); // 512 is the keysize.
+         return kpg.generateKeyPair();
+      }
+   }
+
+   public static void writeKeyPair(File keystore, KeyPair kp) throws Exception
+   {
+      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(keystore));
       try
       {
-         VirtualFile root = VFS.getRoot(jarToCrypt.toURI());
-         Crypter crypter = new Crypter(keyStore);
-         CryptVisitor visitor = new CryptVisitor(root, crypter);
-         File file = visitor.getFile();
-         if (file.renameTo(jarToCrypt))
-         {
-            System.err.println("Cannot rename file: " + file);
-         }
+         oos.writeObject(kp);
+         oos.flush();         
       }
-      catch (Exception e)
+      finally
       {
-         throw new RuntimeException(e);
+         oos.close();
       }
    }
 }
\ No newline at end of file




More information about the jboss-cvs-commits mailing list