package elte.java2_utikalauz5.crypto; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SealedObject; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; /** Rejtjelezett objektumok. @link.forrásfájl {@docRoot}/../data/crypto/src Codec.java @link.letöltés {@docRoot}/../data/crypto Codec.jar @since Java 2 Útikalauz programozóknak 5.0 */ public class Codec { private static final String DECODE_COMMAND = "decode"; private static final String ENCODE_COMMAND = "encode"; public static void main(String[] args) throws Exception { parseArgs(args); } private static void parseArgs(String[] args) throws Exception { if (args.length < 1) { help(null, -1); } if (args[0].equalsIgnoreCase(ENCODE_COMMAND)) { if (args.length < 4) { help("Missing argument!", -1); } encode(args[1], args[2], args[3]); } else if (args[0].equalsIgnoreCase(DECODE_COMMAND)) { if (args.length < 3) { help("Missing argument!", -1); } decode(args[1], args[2]); } else { help("Unrecognized command: " + args[0], -1); } } private static void encode(String password, String outFileName, String message) throws Exception { SealedObject so = createSealedObject(password, message); File outFile = new File(outFileName); ObjectOutputStream oos = null; try { outFile.createNewFile(); oos = new ObjectOutputStream(new FileOutputStream(outFile)); oos.writeObject(so); } finally { if (null != oos) { oos.close(); } } } private static void decode(String password, String inFileName) throws Exception { File inFile = new File(inFileName); ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream(inFile)); SealedObject so = (SealedObject) ois.readObject(); Object data = decrypSealedObject(password, so); System.out.println(data.toString()); } finally { if( null != ois ) { ois.close(); } } } private static Object decrypSealedObject(String password, SealedObject so) throws Exception { SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); KeySpec keySpec = new PBEKeySpec(password.toCharArray()); SecretKey key = skf.generateSecret(keySpec); return so.getObject(key); } private static SealedObject createSealedObject(String password, Serializable data) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, IOException, InvalidKeyException { SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); KeySpec keySpec = new PBEKeySpec(password.toCharArray()); SecretKey key = skf.generateSecret(keySpec); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject so = new SealedObject(data, cipher); return so; } private static void help(String message, int exitCode) { if (null != message) { System.err.println(message); } System.err.println("Usage:"); System.err.println("\tCodec encode "); System.err.println("or\tCodec decode "); System.exit(exitCode); } }