Added: turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/Main.java URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/Main.java?rev=678465&view=auto ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/Main.java (added) +++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/Main.java Mon Jul 21 09:35:30 2008 @@ -0,0 +1,181 @@ +package org.apache.fulcrum.jce.crypto; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; + +/** + * Command line tool for encrypting/decrypting files + * + * file [enc|dec] passwd [file]* + * string [enc|dec] passwd plaintext + * + * @author <a href="mailto:[EMAIL PROTECTED]">Siegfried Goeschl</a> + */ + +public class Main +{ + /** + * Allows testing on the command line. + * + * @param args the command line parameters + */ + public static void main( String[] args ) + { + try + { + if( args.length < 3 ) + { + printHelp(); + throw new IllegalArgumentException("Invalid command line"); + } + + String operationMode = args[0]; + + if( operationMode.equals("file") ) + { + processFiles(args); + } + else if( operationMode.equals("string") ) + { + processString(args); + } + } + catch (Exception e) + { + System.out.println("Error : " + e.getMessage()); + } + } + + /** + * Prints usage information. + */ + public static void printHelp() + { + System.out.println("Main file [enc|dec] passwd source [target]"); + System.out.println("Main string [enc|dec] passwd "); + } + + /** + * Decrypt/encrypt a list of files + * @param args the command line + * @throws Exception the operation failed + */ + public static void processFiles(String[] args) + throws Exception + { + String cipherMode = args[1]; + char[] password = args[2].toCharArray(); + File sourceFile = new File(args[3]); + File targetFile = null; + + if( args.length == 4 ) + { + targetFile = sourceFile; + } + else + { + targetFile = new File(args[4]); + File parentFile = targetFile.getParentFile(); + + if(parentFile != null) + { + parentFile.mkdirs(); + } + } + + processFile(cipherMode,password,sourceFile,targetFile); + } + + /** + * Decrypt/encrypt a single file + * @param cipherMode the mode + * @param password the passwors + * @param sourceFile the file to process + * @param targetFile the targetf file + * @throws Exception the operation failed + */ + public static void processFile(String cipherMode, char[] password, File sourceFile, File targetFile) + throws Exception + { + FileInputStream fis = new FileInputStream(sourceFile); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + if( cipherMode.equals("dec") ) + { + System.out.println("Decrypting " + sourceFile.getAbsolutePath() ); + CryptoUtil.decrypt( fis, baos, password ); + fis.close(); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + FileOutputStream fos = new FileOutputStream(targetFile); + CryptoUtil.copy(bais,fos); + bais.close(); + fos.close(); + } + else if( cipherMode.equals("enc") ) + { + System.out.println("Enrypting " + sourceFile.getAbsolutePath() ); + CryptoUtil.encrypt( fis, baos, password ); + fis.close(); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + FileOutputStream fos = new FileOutputStream(targetFile); + CryptoUtil.copy(bais,fos); + bais.close(); + fos.close(); + } + else + { + String msg = "Don't know what to do with : " + cipherMode; + throw new IllegalArgumentException(msg); + } + } + + /** + * Decrypt/encrypt a string. + * + * @param args the command line + * @throws Exception the operation failed + */ + public static void processString(String[] args) + throws Exception + { + String cipherMode = args[1]; + char[] password = args[2].toCharArray(); + String value = args[3]; + String result = null; + + if( cipherMode.equals("dec") ) + { + result = CryptoUtil.decryptString(value,password); + } + else + { + result = CryptoUtil.encryptString(value,password); + } + + System.out.println( result ); + } +} \ No newline at end of file
Added: turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/MainTest.java URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/MainTest.java?rev=678465&view=auto ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/MainTest.java (added) +++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/MainTest.java Mon Jul 21 09:35:30 2008 @@ -0,0 +1,81 @@ +package org.apache.fulcrum.jce.crypto; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import junit.framework.TestCase; + +/** + * Test suite for crypto functionality + * + * @author <a href="mailto:[EMAIL PROTECTED]">Siegfried Goeschl</a> + */ + +public class MainTest extends TestCase +{ + /** the password to be used */ + private String password; + + /** + * Constructor + * @param name the name of the test case + */ + public MainTest( String name ) + { + super(name); + + this.password = "foobar"; + } + + /** + * @return Returns the password. + */ + protected char[] getPassword() + { + return password.toCharArray(); + } + + /** Encrypt a string on the command line */ + public void testStringEncryption() + { + String[] encryptionArgs = { "string", "enc", this.password, "mysecretpassword"}; + Main.main(encryptionArgs); + String[] decryptionArgs = { "string", "dec", this.password, "9330419fc003b4e1461986782625db13f4c8c81c340a9caa"}; + Main.main(decryptionArgs); + } + + /** Encrypt a text file on the command line */ + public void testFileEncryption1() + { + String[] encryptionArgs = { "file", "enc", this.password, "./src/test/data/plain.txt", "./target/main/plain.enc.txt" }; + String[] decryptionArgs = { "file", "dec", this.password, "./target/main/plain.enc.txt", "./target/main/plain.dec.txt" }; + Main.main(encryptionArgs); + Main.main(decryptionArgs); + } + + /** Encrypt a text file in-place on the command line */ + public void testFileEncryption2() + { + String[] encryptionArgs = { "file", "enc", this.password, "./src/test/data/plain.txt", "./target/main/plain.txt" }; + String[] decryptionArgs = { "file", "dec", this.password, "./target/main/plain.txt" }; + Main.main(encryptionArgs); + Main.main(decryptionArgs); + } + +} \ No newline at end of file Added: turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java?rev=678465&view=auto ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java (added) +++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/SmartDecryptingInputStreamTest.java Mon Jul 21 09:35:30 2008 @@ -0,0 +1,231 @@ +package org.apache.fulcrum.jce.crypto; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; + +import junit.framework.TestCase; + +/** + * Test suite for SmartDecryptingInputStream + * + * @author <a href="mailto:[EMAIL PROTECTED]">Siegfried Goeschl</a> + */ + +public class SmartDecryptingInputStreamTest extends TestCase +{ + /** the password to be used */ + private String password; + + /** the test data directory */ + private File testDataDirectory; + + /** the temp data director */ + private File tempDataDirectory; + + /** + * Constructor + * @param name the name of the test case + */ + public SmartDecryptingInputStreamTest( String name ) + { + super(name); + + this.password = "mysecret"; + this.testDataDirectory = new File( "./src/test/data" ); + this.tempDataDirectory = new File( "./temp" ); + } + + /** + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception + { + CryptoStreamFactoryImpl factory = new CryptoStreamFactoryImpl( + CryptoParameters.SALT, + CryptoParameters.COUNT + ); + + CryptoStreamFactoryImpl.setInstance( factory ); + } + + /** + * @return Returns the password. + */ + protected char[] getPassword() + { + return password.toCharArray(); + } + + /** + * @return Returns the tempDataDirectory. + */ + protected File getTempDataDirectory() + { + return tempDataDirectory; + } + + /** + * @return Returns the testDataDirectory. + */ + protected File getTestDataDirectory() + { + return testDataDirectory; + } + + public void testSmartEmtpyDecryption() throws Exception + { + this.testSmartDecryption("empty.txt","ISO-8859-1"); + } + + public void testSmartTextDecryption() throws Exception + { + this.testSmartDecryption("plain.txt","ISO-8859-1"); + } + + public void testSmartGroovyDecryption() throws Exception + { + this.testSmartDecryption("plain.groovy","ISO-8859-1"); + } + + public void testSmartXmlIso8859Utf8Decryption() throws Exception + { + this.testSmartDecryption("plain-iso-8859-1.xml","ISO-8859-1"); + } + + public void testSmartXmlUtf8Decryption() throws Exception + { + this.testSmartDecryption("plain-utf8.xml","UTF-8"); + } + + public void testSmartXmlUtf16Decryption() throws Exception + { + this.testSmartDecryption("plain-utf16.xml","UTF-16"); + } + + public void testPDFDecryption() throws Exception + { + this.testSmartDecryption("plain.pdf","ISO-8859-1"); + } + + public void testZIPDecryption() throws Exception + { + this.testSmartDecryption("plain.zip","ISO-8859-1"); + } + + /** Test smart decryption for a given file */ + private void testSmartDecryption( String fileName, String enc ) throws Exception + { + File sourceFile = new File( this.getTestDataDirectory(), fileName ); + String plainText = this.loadTextFile(sourceFile,enc); + String smartText = this.smartDecrypt(sourceFile,enc); + byte[] cipherText = this.encryptTextFile(sourceFile); + String decryptedText = this.smartDecrypt(cipherText,enc); + + assertTrue( plainText.length() == smartText.length() ); + assertTrue( plainText.length() == decryptedText.length() ); + assertEquals( plainText, smartText ); + assertEquals( plainText, decryptedText ); + } + + /** + * Loads a plain text file. + * @param file the file to load + */ + private String loadTextFile( File file, String enc ) throws Exception + { + String result = null; + FileInputStream fis = new FileInputStream( file ); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + CryptoUtil.copy(fis,baos); + fis.close(); + result = new String( baos.toByteArray(), enc ); + return result; + } + + /** + * Encrypt a plain text file. + * @param file the file to encrypt + */ + private byte[] encryptTextFile( File file ) throws Exception + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + FileInputStream fis = new FileInputStream( file ); + + CryptoUtil.encrypt( + CryptoStreamFactoryImpl.getInstance(), + fis, + baos, + this.getPassword() + ); + + fis.close(); + + return baos.toByteArray(); + } + + /** + * Use smart decryption on a cipherText. + * + * @param cipherText the encrypted text + * @return the decrypeted content + */ + private String smartDecrypt( byte[] cipherText, String enc ) throws Exception + { + ByteArrayInputStream bais = new ByteArrayInputStream(cipherText); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + SmartDecryptingInputStream sdis = new SmartDecryptingInputStream( + CryptoStreamFactoryImpl.getInstance(), + bais, + this.getPassword() + ); + + CryptoUtil.copy(sdis,baos); + + return new String( baos.toByteArray(), enc ); + } + + /** + * Use smart decryption on a plain text file. + * + * @param file the file to load + * @return the content + */ + private String smartDecrypt( File file, String enc ) throws Exception + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + FileInputStream fis = new FileInputStream( file ); + + SmartDecryptingInputStream sdis = new SmartDecryptingInputStream( + CryptoStreamFactoryImpl.getInstance(), + fis, + this.getPassword() + ); + + CryptoUtil.copy(sdis,baos); + return new String( baos.toByteArray(), enc ); + } + +} Added: turbine/fulcrum/trunk/yaafi-crypto/xdocs/changes.xml URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/xdocs/changes.xml?rev=678465&view=auto ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/xdocs/changes.xml (added) +++ turbine/fulcrum/trunk/yaafi-crypto/xdocs/changes.xml Mon Jul 21 09:35:30 2008 @@ -0,0 +1,34 @@ +<?xml version="1.0"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<document> + <properties> + <title>Fulcrum YAAFI Crytpo Library</title> + <author email="[EMAIL PROTECTED]">Siegfried Goeschl</author> + </properties> + + <body> + <release version="1.0.0" date="as in SVN"> + <action dev="sgoeschl" type="add"> + Moving the code out of YAAFI and make a standalone library + </action> + </release> + </body> +</document> + Added: turbine/fulcrum/trunk/yaafi-crypto/xdocs/index.xml URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/xdocs/index.xml?rev=678465&view=auto ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/xdocs/index.xml (added) +++ turbine/fulcrum/trunk/yaafi-crypto/xdocs/index.xml Mon Jul 21 09:35:30 2008 @@ -0,0 +1,99 @@ +<?xml version="1.0"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> + +<document> + + <properties> + <title>Fulcrum Crypto Library</title> + <author email="[EMAIL PROTECTED]">Siegfried Goeschl</author> + </properties> + + <body> + + <section name="Overview"> + <p> + Fulcrum Crypto Library is an extension library for Fulcrum YAAFI + to support transparent decryption of configuration files. + </p> + </section> + + <section name="Cryptography Support"> + + <p> + The YAAFI container supports decryption of configuration files using + Sun's JCA (Java Crypto Architecture). The encryption/decryption is + based on DES using 56 bit key length. + </p> + + <subsection name="JDK 1.3.x"> + <p> + For JDK 1.3.x the <a href="http://java.sun.com/products/jce/index-122.html"> + Java Cryptography Extension (JCE) 1.2.2</a> needs to be installed. + Furthermore you need to add the jce1_2_2.jar to your Maven repository + and project.xml + </p> + </subsection> + + <subsection name="JDK 1.4.x"> + <p> + The current JDK's have the JCA built-in therefore no extra configuration + is required. + </p> + </subsection> + + <subsection name="JDK 1.5.x"> + <p> + The current JDK's have the JCA built-in therefore no extra configuration + is required. + </p> + </subsection> + + <subsection name="JDK 1.6.x"> + <p> + The current JDK's have the JCA built-in therefore no extra configuration + is required. + </p> + </subsection> + + <subsection name="Availabe Algorithms"> + <table> + <tr> + <th>Provider Version</th> + <th>Algorithms</th> + </tr> + <tr> + <td>SunJCE 1.22</td> + <td>PBEWithMD5AndDES</td> + </tr> + <tr> + <td>SunJCE 1.42</td> + <td> + PBEWithMD5AndDES + </td> + </tr> + </table> + </subsection> + + </section> + + + </body> + +</document>
