Brian's Waste of Time

Fri, 07 Dec 2007

Re: JCE and OpenSSL

Thank you Julius Davies! for not-yet-commons-ssl which does exactly what I was looking for

import org.apache.commons.io.IOUtils;
import org.apache.commons.ssl.OpenSSL;

import java.io.File;
import java.io.FileOutputStream;

public class Foo
{
    public static void main(String[] args) throws Exception
    {
        File f = new File("/tmp/foo");
        FileOutputStream fout = new FileOutputStream(f);
        fout.write(OpenSSL.encrypt("aes256", 
                                   "secret".toCharArray(), 
                                   "hello world\n".getBytes("UTF-8")));
        fout.close();
        Process p = Runtime.getRuntime()
            .exec("openssl enc -pass pass:secret -d -aes256 -a -in /tmp/foo");
        System.out.print(IOUtils.toString(p.getInputStream()));
    }
}

Woo hoo!

3 writebacks [/src/java] permanent link