001package org.apache.commons.ssl;
002
003import org.apache.commons.ssl.util.Hex;
004
005import java.io.File;
006import java.io.FileInputStream;
007import java.io.InputStream;
008import java.util.Arrays;
009
010/**
011 * @author Julius Davies
012 * @since 3-Jul-2007
013 */
014public class OpenSSLTest {
015
016    public static void main(String[] args) throws Exception {
017        String path = args[0];
018        File f = new File(path);
019        if (f.isDirectory()) {
020            File[] files = f.listFiles();
021            Arrays.sort(files);
022            for (int i = 0; i < files.length; i++) {
023                process(files[i], 0);
024            }
025        } else {
026            System.out.println("Attempting decrypt!");
027
028            String keyS = "1234567890ABDEF01234567890ABDEFF";
029            String ivS = "1234567890ABDEF01234567890ABDEFF";
030            byte[] key = Hex.decode(keyS.getBytes());
031            byte[] iv = Hex.decode(ivS.getBytes());
032            FileInputStream in = new FileInputStream(f);
033            InputStream decrypted = OpenSSL.decrypt("aes128", key, iv, in);
034            byte[] b = Util.streamToBytes(decrypted);
035            System.out.println(new String(b));
036        }
037    }
038
039
040    private static void process(File f, int depth) {
041        String name = f.getName();
042        if ("CVS".equalsIgnoreCase(name)) {
043            return;
044        }
045        if (name.toUpperCase().startsWith("README")) {
046            return;
047        }
048
049        if (f.isDirectory()) {
050            if (depth <= 3) {
051                File[] files = f.listFiles();
052                Arrays.sort(files);
053                for (int i = 0; i < files.length; i++) {
054                    process(files[i], depth + 1);
055                }
056            } else {
057                System.out.println("IGNORING [" + f + "].  Directory too deep (" + depth + ").");
058            }
059        } else {
060            if (f.isFile() && f.canRead()) {
061                String fileName = f.getName();
062                int x = fileName.indexOf('.');
063                if (x < 0) {
064                    return;
065                }
066                String cipher = fileName.substring(0, x);
067                String cipherPadded = Util.pad(cipher, 20, false);
068                String filePadded = Util.pad(fileName, 25, false);
069                try {
070                    FileInputStream in = new FileInputStream(f);
071                    byte[] encrypted = Util.streamToBytes(in);
072                    char[] pwd = "changeit".toCharArray();
073
074                    byte[] result = OpenSSL.decrypt(cipher, pwd, encrypted);
075                    String s = new String(result, "ISO-8859-1");
076
077                    boolean success = "Hello World!".equals(s);
078                    if (success) {
079                        System.out.println("SUCCESS \t" + cipherPadded + "\t" + filePadded);
080                    } else {
081                        System.out.println("FAILURE*\t" + cipherPadded + "\t" + filePadded + "\tDECRYPT RESULTS DON'T MATCH");
082                    }
083                }
084                catch (Exception e) {
085                    System.out.println("FAILURE*\t" + cipherPadded + "\t" + filePadded + "\t" + e);
086                    e.printStackTrace();
087                }
088            }
089        }
090    }
091
092}