Author: gk Date: Thu Dec 20 16:34:05 2018 New Revision: 1849416 URL: http://svn.apache.org/viewvc?rev=1849416&view=rev Log: - adjust method with related classes - add MainJ8.java cli
Added: turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/ turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/MainJ8.java (with props) Modified: turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryJ8Impl.java turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtilJ8.java turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilJ8Test.java turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/Main.java Modified: turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryJ8Impl.java URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryJ8Impl.java?rev=1849416&r1=1849415&r2=1849416&view=diff ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryJ8Impl.java (original) +++ turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoStreamFactoryJ8Impl.java Thu Dec 20 16:34:05 2018 @@ -62,6 +62,9 @@ import javax.crypto.spec.PBEParameterSpe public final class CryptoStreamFactoryJ8Impl extends CryptoStreamFactoryImpl implements CryptoStreamFactoryJ8 { + private static final int salt_size = 128; + private static final int key_size = 128; + /** the default instance */ private static CryptoStreamFactoryJ8 instance; @@ -104,6 +107,17 @@ public final class CryptoStreamFactoryJ8 this.providerName = PROVIDERNAME; this.algorithm = CryptoParameters.ALGORITHM_J8; } + + /** + * Constructor + */ + public CryptoStreamFactoryJ8Impl(String algo) throws GeneralSecurityException + { + this.salt = generateSalt(); + this.count = CryptoParameters.COUNT_J8; + this.providerName = PROVIDERNAME; + this.algorithm = algo; + } /** * Constructor @@ -169,7 +183,7 @@ public final class CryptoStreamFactoryJ8 SecretKeyFactory keyFactory; String algorithm = this.getAlgorithm(); - PBEKeySpec keySpec = new PBEKeySpec(password, (salt == null)? this.getSalt(): salt, this.getCount(), 128 ); + PBEKeySpec keySpec = new PBEKeySpec(password, (salt == null)? this.getSalt(): salt, this.getCount(), key_size ); byte[] encodedTmp = null; try { if( this.getProviderName() == null ) @@ -218,7 +232,7 @@ public final class CryptoStreamFactoryJ8 byte[] salt = null; byte[] iv = null; if (mode == Cipher.DECRYPT_MODE) { - salt = Arrays.copyOfRange(input, 0, 128 / 8); + salt = Arrays.copyOfRange(input, 0, salt_size / 8); iv = Arrays.copyOfRange(input, salt.length, salt.length + 128 / 8); ciphertext = Arrays.copyOfRange(input, salt.length + iv.length, input.length);// cut out salt and iv } @@ -265,7 +279,7 @@ public final class CryptoStreamFactoryJ8 SecureRandom random; try { random = SecureRandom.getInstance("SHA1PRNG"); - byte[] salt = new byte[128 / 8]; + byte[] salt = new byte[salt_size / 8]; random.nextBytes(salt); return salt; } catch (NoSuchAlgorithmException e) { Modified: turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtilJ8.java URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtilJ8.java?rev=1849416&r1=1849415&r2=1849416&view=diff ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtilJ8.java (original) +++ turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/CryptoUtilJ8.java Thu Dec 20 16:34:05 2018 @@ -54,24 +54,6 @@ public final class CryptoUtilJ8 extends return CryptoUtilJ8.instance; } - - /** - * Encrypts a string into a hex string. - * - * @param factory the factory to create the crypto streams - * @param plainText the plain text to be encrypted - * @param password the password for encryption - * @return the encrypted string - * @throws GeneralSecurityException accessing JCE failed - * @throws IOException accessing the souce failed - */ - @Override - public String encryptString(CryptoStreamFactory factory, String plainText, char[] password) - throws GeneralSecurityException, IOException { - ByteArrayOutputStream bais = new ByteArrayOutputStream(); - encrypt(factory, plainText, bais, password); - return HexConverter.toString(bais.toByteArray()); - } /** * Copies from a source to a target object using encryption and a caller @@ -114,6 +96,7 @@ public final class CryptoUtilJ8 extends } /** + * * @return the CryptoStreamFactory to be used */ public CryptoStreamFactory getCryptoStreamFactory() { Added: turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/MainJ8.java URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/MainJ8.java?rev=1849416&view=auto ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/MainJ8.java (added) +++ turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/MainJ8.java Thu Dec 20 16:34:05 2018 @@ -0,0 +1,192 @@ +package org.apache.fulcrum.jce.crypto.cli; + +/* + * 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; + +import org.apache.fulcrum.jce.crypto.CryptoParameters; +import org.apache.fulcrum.jce.crypto.CryptoUtilJ8; + +/** + * Command line tool for encrypting/decrypting a file or string + * + * file [enc|dec] passwd [file]* + * string [enc|dec] passwd plaintext + * + * @author <a href="mailto:siegfried.goes...@it20one.at">Siegfried Goeschl</a> + */ + +public class MainJ8 +{ + /** + * Allows usage 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("\r\n*** Command line tool for encrypting/decrypting strings/files ***\r\n*** algorithm based on "+ CryptoParameters.ALGORITHM_J8+ "***\r\n"); + System.out.println( "*** Usage: ***\r\n"); + System.out.println("java -cp target\\classes; "+ MainJ8.class.getName()+ " <operation mode:file:string> <coding mode:enc|dec> <password> <path|string> [target]\r\ne.g.\r\n"); + System.out.println( MainJ8.class.getSimpleName()+ " file [enc|dec] passwd source [target]"); + System.out.println(MainJ8.class.getSimpleName() + " string [enc|dec] passwd source"); + } + + /** + * 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(); + + CryptoUtilJ8 cryptoUtilJ8 = CryptoUtilJ8.getInstance(); + + if( cipherMode.equals("dec") ) + { + System.out.println("Decrypting " + sourceFile.getAbsolutePath() ); + cryptoUtilJ8.decrypt( fis, baos, password ); + fis.close(); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + FileOutputStream fos = new FileOutputStream(targetFile); + CryptoUtilJ8.copy(bais,fos); + bais.close(); + fos.close(); + } + else if( cipherMode.equals("enc") ) + { + System.out.println("Enrypting " + sourceFile.getAbsolutePath() ); + cryptoUtilJ8.encrypt( fis, baos, password ); + fis.close(); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + FileOutputStream fos = new FileOutputStream(targetFile); + CryptoUtilJ8.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; + + CryptoUtilJ8 cryptoUtilJ8 = CryptoUtilJ8.getInstance(); + + if( cipherMode.equals("dec") ) + { + result = cryptoUtilJ8.decryptString(value,password); + } + else + { + result = cryptoUtilJ8.encryptString(value,password); + } + + System.out.println( result ); + } +} \ No newline at end of file Propchange: turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/MainJ8.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilJ8Test.java URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilJ8Test.java?rev=1849416&r1=1849415&r2=1849416&view=diff ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilJ8Test.java (original) +++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/CryptoUtilJ8Test.java Thu Dec 20 16:34:05 2018 @@ -2,7 +2,9 @@ package org.apache.fulcrum.jce.crypto; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import java.io.ByteArrayOutputStream; import java.io.File; import org.junit.Before; @@ -24,6 +26,8 @@ public class CryptoUtilJ8Test { /** the temp data director */ private File tempDataDirectory; + + private CryptoUtilJ8 cryptoUtilJ8; /** * Constructor @@ -44,9 +48,7 @@ public class CryptoUtilJ8Test { */ @Before public void setUp() throws Exception { -// CryptoStreamFactoryImpl factory = new CryptoStreamFactoryJ8Impl(CryptoParameters.SALT, CryptoParameters.COUNT_J8); -// -// CryptoStreamFactoryImpl.setInstance(factory); + cryptoUtilJ8 = CryptoUtilJ8.getInstance(); } /** @@ -77,7 +79,7 @@ public class CryptoUtilJ8Test { public void testTextEncryption() throws Exception { File sourceFile = new File(this.getTestDataDirectory(), "plain.txt"); File targetFile = new File(this.getTempDataDirectory(), "plain.j8.enc.txt"); - CryptoUtilJ8.getInstance().encrypt(sourceFile, targetFile, this.getPassword()); + cryptoUtilJ8.encrypt(sourceFile, targetFile, this.getPassword()); } /** Decrypt a text file @@ -88,7 +90,7 @@ public class CryptoUtilJ8Test { testTextEncryption(); File sourceFile = new File(this.getTempDataDirectory(), "plain.j8.enc.txt"); File targetFile = new File(this.getTempDataDirectory(), "plain.j8.dec.txt"); - CryptoUtilJ8.getInstance().decrypt(sourceFile, targetFile.getAbsolutePath(), this.getPassword()); + cryptoUtilJ8.decrypt(sourceFile, targetFile.getAbsolutePath(), this.getPassword()); } /** Encrypt a PDF file @@ -99,7 +101,7 @@ public class CryptoUtilJ8Test { public void testPdfEncryption() throws Exception { File sourceFile = new File(this.getTestDataDirectory(), "plain.pdf"); File targetFile = new File(this.getTempDataDirectory(), "plain.j8.enc.pdf"); - CryptoUtil.getInstance().encrypt(sourceFile, targetFile, this.getPassword()); + cryptoUtilJ8.encrypt(sourceFile, targetFile, this.getPassword()); } /** Decrypt a PDF file @@ -111,9 +113,37 @@ public class CryptoUtilJ8Test { testPdfEncryption(); File sourceFile = new File(this.getTempDataDirectory(), "plain.j8.enc.pdf"); File targetFile = new File(this.getTempDataDirectory(), "plain.j8.dec.pdf"); - CryptoUtil.getInstance().decrypt(sourceFile, targetFile, this.getPassword()); + cryptoUtilJ8.decrypt(sourceFile, targetFile, this.getPassword()); + } + + /** Test encryption and decryption of Strings + * + * @throws Exception Generic exception + */ + @Test + public void testStringEncryption() throws Exception { + char[] testVector = new char[513]; + + for (int i = 0; i < testVector.length; i++) { + testVector[i] = (char) i; + } + + String source = new String(testVector); + String cipherText = cryptoUtilJ8.encryptString(source, this.getPassword()); + String plainText = cryptoUtilJ8.decryptString(cipherText, this.getPassword()); + assertEquals(source, plainText); } + /** Test encryption and decryption of Strings + * @throws Exception Generic exception + */ + @Test + public void testStringHandling() throws Exception { + String source = "Nobody knows the toubles I have seen ..."; + String cipherText = cryptoUtilJ8.encryptString(source, this.getPassword()); + String plainText = cryptoUtilJ8.decryptString(cipherText, this.getPassword()); + assertEquals(source, plainText); + } /** Test creating a password * @throws Exception Generic exception @@ -128,7 +158,34 @@ public class CryptoUtilJ8Test { assertNotNull(result); return; } + + /** Test encryption and decryption of binary data + * @throws Exception Generic exception + */ + @Test + public void testBinaryHandling() throws Exception { + byte[] source = new byte[256]; + byte[] result = null; + for (int i = 0; i < source.length; i++) { + source[i] = (byte) i; + } + + ByteArrayOutputStream cipherText = new ByteArrayOutputStream(); + ByteArrayOutputStream plainText = new ByteArrayOutputStream(); + + cryptoUtilJ8.encrypt(source, cipherText, this.getPassword()); + cryptoUtilJ8.decrypt(cipherText, plainText, this.getPassword()); + + result = plainText.toByteArray(); + + for (int i = 0; i < source.length; i++) { + if (source[i] != result[i]) { + fail("Binary data are different at position " + i); + } + } + } + /** Test encryption and decryption of Strings * @throws Exception Generic exception */ @@ -136,11 +193,11 @@ public class CryptoUtilJ8Test { public void testStringWithPasswordEncryption() throws Exception { char[] password = "57cb-4a23-d838-45222".toCharArray(); String source = "e02c-3b76-ff1e-5d9a1"; - String cipherText = CryptoUtilJ8.getInstance().encryptString(source, password); + String cipherText = cryptoUtilJ8.encryptString(source, password); System.out.println(cipherText);// 128bit assertEquals(128, cipherText.length()); - //CryptoStreamFactoryJ8Impl.setInstance(null); - String plainText = CryptoUtilJ8.getInstance().decryptString(cipherText, password); + CryptoStreamFactoryJ8Impl.setInstance(null); + String plainText = cryptoUtilJ8.decryptString(cipherText, password); assertEquals(source, plainText); } Modified: 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=1849416&r1=1849415&r2=1849416&view=diff ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/Main.java (original) +++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/Main.java Thu Dec 20 16:34:05 2018 @@ -74,7 +74,7 @@ public class Main public static void printHelp() { System.out.println("Main file [enc|dec] passwd source [target]"); - System.out.println("Main string [enc|dec] passwd "); + System.out.println("Main string [enc|dec] passwd source"); } /**