Repository: commons-crypto Updated Branches: refs/heads/master b380b8c87 -> 3e7836f2a
Move examples to test tree Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/3e7836f2 Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/3e7836f2 Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/3e7836f2 Branch: refs/heads/master Commit: 3e7836f2a9e4315e7f1d0429ef0558b1722eb023 Parents: b380b8c Author: Sebb <[email protected]> Authored: Fri Jul 1 10:25:38 2016 +0100 Committer: Sebb <[email protected]> Committed: Fri Jul 1 10:25:38 2016 +0100 ---------------------------------------------------------------------- .../crypto/examples/CipherByteArrayExample.java | 91 ---------------- .../examples/CipherByteBufferExample.java | 107 ------------------- .../commons/crypto/examples/RandomExample.java | 60 ----------- .../commons/crypto/examples/StreamExample.java | 82 -------------- .../commons/crypto/examples/package-info.java | 22 ---- src/site/xdoc/userguide.xml | 8 +- .../crypto/examples/CipherByteArrayExample.java | 91 ++++++++++++++++ .../examples/CipherByteBufferExample.java | 107 +++++++++++++++++++ .../commons/crypto/examples/RandomExample.java | 60 +++++++++++ .../commons/crypto/examples/StreamExample.java | 82 ++++++++++++++ .../commons/crypto/examples/package-info.java | 22 ++++ 11 files changed, 366 insertions(+), 366 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/3e7836f2/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java b/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java deleted file mode 100644 index 784b722..0000000 --- a/src/main/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java +++ /dev/null @@ -1,91 +0,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. - */ -package org.apache.commons.crypto.examples; - -import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Properties; - -import javax.crypto.Cipher; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; - -import org.apache.commons.crypto.cipher.CryptoCipher; -import org.apache.commons.crypto.cipher.CryptoCipherFactory.CipherProvider; -import org.apache.commons.crypto.utils.Utils; -import static org.apache.commons.crypto.conf.ConfigurationKeys.CIPHER_CLASSES_KEY; - -/** - * Example showing use of the CryptoCipher API using a byte array - */ -public class CipherByteArrayExample { - - public static void main(String[] args) throws Exception { - - final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"),"AES"); - final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456")); - - Properties properties = new Properties(); - properties.setProperty(CIPHER_CLASSES_KEY, CipherProvider.OPENSSL.getClassName()); - //Creates a CryptoCipher instance with the transformation and properties. - final String transform = "AES/CBC/PKCS5Padding"; - CryptoCipher encipher = Utils.getCipherInstance(transform, properties); - System.out.println("Cipher: " + encipher.getClass().getCanonicalName()); - - final String sampleInput = "hello world!"; - System.out.println("input: " + sampleInput); - - byte[] input = getUTF8Bytes(sampleInput); - byte[] output = new byte[32]; - - //Initializes the cipher with ENCRYPT_MODE, key and iv. - encipher.init(Cipher.ENCRYPT_MODE, key, iv); - //Continues a multiple-part encryption/decryption operation for byte array. - int updateBytes = encipher.update(input, 0, input.length, output, 0); - System.out.println(updateBytes); - //We must call doFinal at the end of encryption/decryption. - int finalBytes = encipher.doFinal(input, 0, 0, output, updateBytes); - System.out.println(finalBytes); - //Closes the cipher. - encipher.close(); - - System.out.println(Arrays.toString(Arrays.copyOf(output, updateBytes+finalBytes))); - - // Now reverse the process using a different implementation with the same settings - properties.setProperty(CIPHER_CLASSES_KEY, CipherProvider.JCE.getClassName()); - CryptoCipher decipher = Utils.getCipherInstance(transform, properties); - System.out.println("Cipher: " + encipher.getClass().getCanonicalName()); - - decipher.init(Cipher.DECRYPT_MODE, key, iv); - byte [] decoded = new byte[32]; - decipher.doFinal(output, 0, updateBytes + finalBytes, decoded, 0); - - System.out.println("output: " + new String(decoded, StandardCharsets.UTF_8)); - } - - /** - * Converts String to UTF8 bytes - * - * @param input the input string - * @return UTF8 bytes - */ - private static byte[] getUTF8Bytes(String input) { - return input.getBytes(StandardCharsets.UTF_8); - } - -} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/3e7836f2/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java b/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java deleted file mode 100644 index c515461..0000000 --- a/src/main/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java +++ /dev/null @@ -1,107 +0,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. - */ -package org.apache.commons.crypto.examples; - -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Properties; - -import javax.crypto.Cipher; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; - -import org.apache.commons.crypto.cipher.CryptoCipher; -import org.apache.commons.crypto.utils.Utils; - -/** - * Example showing the CryptoCipher API using a ByteBuffer - */ -public class CipherByteBufferExample { - - public static void main(String[] args) throws Exception { - final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"), "AES"); - final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456")); - Properties properties = new Properties(); - //Creates a CryptoCipher instance with the transformation and properties. - final String transform = "AES/CBC/PKCS5Padding"; - final ByteBuffer outBuffer; - final int bufferSize = 1024; - final int updateBytes; - final int finalBytes; - try (CryptoCipher encipher = Utils.getCipherInstance(transform, properties)) { - - ByteBuffer inBuffer = ByteBuffer.allocateDirect(bufferSize); - outBuffer = ByteBuffer.allocateDirect(bufferSize); - inBuffer.put(getUTF8Bytes("hello world!")); - - inBuffer.flip(); // ready for the cipher to read it - // Show the data is there - System.out.println("inBuffer=" + asString(inBuffer)); - - // Initializes the cipher with ENCRYPT_MODE,key and iv. - encipher.init(Cipher.ENCRYPT_MODE, key, iv); - // Continues a multiple-part encryption/decryption operation for byte buffer. - updateBytes = encipher.update(inBuffer, outBuffer); - System.out.println(updateBytes); - - // We should call do final at the end of encryption/decryption. - finalBytes = encipher.doFinal(inBuffer, outBuffer); - System.out.println(finalBytes); - } - - outBuffer.flip(); // ready for use as decrypt - byte [] encoded = new byte[updateBytes + finalBytes]; - outBuffer.duplicate().get(encoded); - System.out.println(Arrays.toString(encoded)); - - // Now reverse the process - try (CryptoCipher decipher = Utils.getCipherInstance(transform, properties)) { - decipher.init(Cipher.DECRYPT_MODE, key, iv); - ByteBuffer decoded = ByteBuffer.allocateDirect(bufferSize); - decipher.update(outBuffer, decoded); - decipher.doFinal(outBuffer, decoded); - decoded.flip(); // ready for use - System.out.println("decoded="+asString(decoded)); - } - } - - /** - * Converts String to UTF8 bytes - * - * @param input the input string - * @return UTF8 bytes - */ - private static byte[] getUTF8Bytes(String input) { - return input.getBytes(StandardCharsets.UTF_8); - } - - /** - * Converts ByteBuffer to String - * - * @param buffer input byte buffer - * @return the converted string - */ - private static String asString(ByteBuffer buffer) { - final ByteBuffer copy = buffer.duplicate(); - final byte[] bytes = new byte[Math.min(copy.remaining(),50)]; - copy.get(bytes); - return new String(bytes, StandardCharsets.UTF_8); - } - -} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/3e7836f2/src/main/java/org/apache/commons/crypto/examples/RandomExample.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/examples/RandomExample.java b/src/main/java/org/apache/commons/crypto/examples/RandomExample.java deleted file mode 100644 index 2a9ff41..0000000 --- a/src/main/java/org/apache/commons/crypto/examples/RandomExample.java +++ /dev/null @@ -1,60 +0,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. - */ -package org.apache.commons.crypto.examples; - -import java.io.IOException; -import java.security.GeneralSecurityException; -import java.util.Arrays; -import java.util.Properties; - -import org.apache.commons.crypto.conf.ConfigurationKeys; -import org.apache.commons.crypto.random.CryptoRandom; -import org.apache.commons.crypto.random.CryptoRandomFactory; - -/** - * Example showing use of the CryptoRandom API - */ -public class RandomExample { - - public static void main(String []args) throws GeneralSecurityException, IOException { - //Constructs a byte array to store random data. - byte[] key = new byte[16]; - byte[] iv = new byte[32]; - - Properties properties = new Properties(); - properties.put(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, - CryptoRandomFactory.RandomProvider.OPENSSL.getClassName()); - - //Gets the 'CryptoRandom' instance. - CryptoRandom random = CryptoRandomFactory.getCryptoRandom(properties); - - // Show the actual class (may be different from the one requested) - System.out.println(random.getClass().getCanonicalName()); - - // Generate random bytes and places them into the byte arrays. - random.nextBytes(key); - random.nextBytes(iv); - - //Closes the CryptoRandom. - random.close(); - - // Show the generated output - System.out.println(Arrays.toString(key)); - System.out.println(Arrays.toString(iv)); - } -} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/3e7836f2/src/main/java/org/apache/commons/crypto/examples/StreamExample.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/examples/StreamExample.java b/src/main/java/org/apache/commons/crypto/examples/StreamExample.java deleted file mode 100644 index 372475a..0000000 --- a/src/main/java/org/apache/commons/crypto/examples/StreamExample.java +++ /dev/null @@ -1,82 +0,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. - */ -package org.apache.commons.crypto.examples; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Properties; - -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; - -import org.apache.commons.crypto.stream.CryptoInputStream; -import org.apache.commons.crypto.stream.CryptoOutputStream; - -/** - * Example showing how to use stream encryption and decryption. - */ -public class StreamExample { - - public static void main(String []args) throws IOException { - final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"),"AES"); - final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456")); - Properties properties = new Properties(); - final String transform = "AES/CBC/PKCS5Padding"; - - String input = "hello world!"; - //Encryption with CryptoOutputStream. - - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - - try (CryptoOutputStream cos = new CryptoOutputStream(transform, properties, outputStream, key, iv)) { - cos.write(getUTF8Bytes(input)); - cos.flush(); - } - - // The encrypted data: - System.out.println("Encrypted: "+Arrays.toString(outputStream.toByteArray())); - - // Decryption with CryptoInputStream. - InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); - - try (CryptoInputStream cis = new CryptoInputStream(transform, properties, inputStream, key, iv)) { - byte[] decryptedData = new byte[1024]; - int decryptedLen = 0; - int i; - while ((i = cis.read(decryptedData, decryptedLen, decryptedData.length - decryptedLen)) > -1) { - decryptedLen += i; - } - System.out.println("Decrypted: "+new String(decryptedData, 0, decryptedLen, StandardCharsets.UTF_8)); - } - } - - /** - * Converts String to UTF8 bytes - * - * @param input the input string - * @return UTF8 bytes - */ - private static byte[] getUTF8Bytes(String input) { - return input.getBytes(StandardCharsets.UTF_8); - } - -} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/3e7836f2/src/main/java/org/apache/commons/crypto/examples/package-info.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/examples/package-info.java b/src/main/java/org/apache/commons/crypto/examples/package-info.java deleted file mode 100644 index 6f9c541..0000000 --- a/src/main/java/org/apache/commons/crypto/examples/package-info.java +++ /dev/null @@ -1,22 +0,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. - */ - -/** - * Example classes - */ -package org.apache.commons.crypto.examples; http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/3e7836f2/src/site/xdoc/userguide.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/userguide.xml b/src/site/xdoc/userguide.xml index b4715c3..41b1d7d 100644 --- a/src/site/xdoc/userguide.xml +++ b/src/site/xdoc/userguide.xml @@ -82,7 +82,7 @@ for accelerating the random generation. </p> - <a href="xref/org/apache/commons/crypto/examples/RandomExample.html">RandomExample.java</a> + <a href="xref-test/org/apache/commons/crypto/examples/RandomExample.html">RandomExample.java</a> <h4>Usage of Cipher API</h4> <p> @@ -93,11 +93,11 @@ </p> <h5>Usage of Byte Array Encryption/Decryption</h5> - <a href="xref/org/apache/commons/crypto/examples/CipherByteArrayExample.html">CipherByteArrayExample.java</a> + <a href="xref-test/org/apache/commons/crypto/examples/CipherByteArrayExample.html">CipherByteArrayExample.java</a> <h5>Usage of ByteBuffer Encryption/Decryption</h5> - <a href="xref/org/apache/commons/crypto/examples/CipherByteBufferExample.html">CipherByteBufferExample.java</a> + <a href="xref-test/org/apache/commons/crypto/examples/CipherByteBufferExample.html">CipherByteBufferExample.java</a> <h4>Usage of Stream API</h4> @@ -108,7 +108,7 @@ </p> <h5>Usage of stream encryption/decryption</h5> - <a href="xref/org/apache/commons/crypto/examples/StreamExample.html">StreamExample.java</a> + <a href="xref-test/org/apache/commons/crypto/examples/StreamExample.html">StreamExample.java</a> </subsection> </section> http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/3e7836f2/src/test/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java b/src/test/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java new file mode 100644 index 0000000..784b722 --- /dev/null +++ b/src/test/java/org/apache/commons/crypto/examples/CipherByteArrayExample.java @@ -0,0 +1,91 @@ +/** + * 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. + */ +package org.apache.commons.crypto.examples; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Properties; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +import org.apache.commons.crypto.cipher.CryptoCipher; +import org.apache.commons.crypto.cipher.CryptoCipherFactory.CipherProvider; +import org.apache.commons.crypto.utils.Utils; +import static org.apache.commons.crypto.conf.ConfigurationKeys.CIPHER_CLASSES_KEY; + +/** + * Example showing use of the CryptoCipher API using a byte array + */ +public class CipherByteArrayExample { + + public static void main(String[] args) throws Exception { + + final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"),"AES"); + final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456")); + + Properties properties = new Properties(); + properties.setProperty(CIPHER_CLASSES_KEY, CipherProvider.OPENSSL.getClassName()); + //Creates a CryptoCipher instance with the transformation and properties. + final String transform = "AES/CBC/PKCS5Padding"; + CryptoCipher encipher = Utils.getCipherInstance(transform, properties); + System.out.println("Cipher: " + encipher.getClass().getCanonicalName()); + + final String sampleInput = "hello world!"; + System.out.println("input: " + sampleInput); + + byte[] input = getUTF8Bytes(sampleInput); + byte[] output = new byte[32]; + + //Initializes the cipher with ENCRYPT_MODE, key and iv. + encipher.init(Cipher.ENCRYPT_MODE, key, iv); + //Continues a multiple-part encryption/decryption operation for byte array. + int updateBytes = encipher.update(input, 0, input.length, output, 0); + System.out.println(updateBytes); + //We must call doFinal at the end of encryption/decryption. + int finalBytes = encipher.doFinal(input, 0, 0, output, updateBytes); + System.out.println(finalBytes); + //Closes the cipher. + encipher.close(); + + System.out.println(Arrays.toString(Arrays.copyOf(output, updateBytes+finalBytes))); + + // Now reverse the process using a different implementation with the same settings + properties.setProperty(CIPHER_CLASSES_KEY, CipherProvider.JCE.getClassName()); + CryptoCipher decipher = Utils.getCipherInstance(transform, properties); + System.out.println("Cipher: " + encipher.getClass().getCanonicalName()); + + decipher.init(Cipher.DECRYPT_MODE, key, iv); + byte [] decoded = new byte[32]; + decipher.doFinal(output, 0, updateBytes + finalBytes, decoded, 0); + + System.out.println("output: " + new String(decoded, StandardCharsets.UTF_8)); + } + + /** + * Converts String to UTF8 bytes + * + * @param input the input string + * @return UTF8 bytes + */ + private static byte[] getUTF8Bytes(String input) { + return input.getBytes(StandardCharsets.UTF_8); + } + +} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/3e7836f2/src/test/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java b/src/test/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java new file mode 100644 index 0000000..c515461 --- /dev/null +++ b/src/test/java/org/apache/commons/crypto/examples/CipherByteBufferExample.java @@ -0,0 +1,107 @@ +/** + * 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. + */ +package org.apache.commons.crypto.examples; + +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Properties; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +import org.apache.commons.crypto.cipher.CryptoCipher; +import org.apache.commons.crypto.utils.Utils; + +/** + * Example showing the CryptoCipher API using a ByteBuffer + */ +public class CipherByteBufferExample { + + public static void main(String[] args) throws Exception { + final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"), "AES"); + final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456")); + Properties properties = new Properties(); + //Creates a CryptoCipher instance with the transformation and properties. + final String transform = "AES/CBC/PKCS5Padding"; + final ByteBuffer outBuffer; + final int bufferSize = 1024; + final int updateBytes; + final int finalBytes; + try (CryptoCipher encipher = Utils.getCipherInstance(transform, properties)) { + + ByteBuffer inBuffer = ByteBuffer.allocateDirect(bufferSize); + outBuffer = ByteBuffer.allocateDirect(bufferSize); + inBuffer.put(getUTF8Bytes("hello world!")); + + inBuffer.flip(); // ready for the cipher to read it + // Show the data is there + System.out.println("inBuffer=" + asString(inBuffer)); + + // Initializes the cipher with ENCRYPT_MODE,key and iv. + encipher.init(Cipher.ENCRYPT_MODE, key, iv); + // Continues a multiple-part encryption/decryption operation for byte buffer. + updateBytes = encipher.update(inBuffer, outBuffer); + System.out.println(updateBytes); + + // We should call do final at the end of encryption/decryption. + finalBytes = encipher.doFinal(inBuffer, outBuffer); + System.out.println(finalBytes); + } + + outBuffer.flip(); // ready for use as decrypt + byte [] encoded = new byte[updateBytes + finalBytes]; + outBuffer.duplicate().get(encoded); + System.out.println(Arrays.toString(encoded)); + + // Now reverse the process + try (CryptoCipher decipher = Utils.getCipherInstance(transform, properties)) { + decipher.init(Cipher.DECRYPT_MODE, key, iv); + ByteBuffer decoded = ByteBuffer.allocateDirect(bufferSize); + decipher.update(outBuffer, decoded); + decipher.doFinal(outBuffer, decoded); + decoded.flip(); // ready for use + System.out.println("decoded="+asString(decoded)); + } + } + + /** + * Converts String to UTF8 bytes + * + * @param input the input string + * @return UTF8 bytes + */ + private static byte[] getUTF8Bytes(String input) { + return input.getBytes(StandardCharsets.UTF_8); + } + + /** + * Converts ByteBuffer to String + * + * @param buffer input byte buffer + * @return the converted string + */ + private static String asString(ByteBuffer buffer) { + final ByteBuffer copy = buffer.duplicate(); + final byte[] bytes = new byte[Math.min(copy.remaining(),50)]; + copy.get(bytes); + return new String(bytes, StandardCharsets.UTF_8); + } + +} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/3e7836f2/src/test/java/org/apache/commons/crypto/examples/RandomExample.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/examples/RandomExample.java b/src/test/java/org/apache/commons/crypto/examples/RandomExample.java new file mode 100644 index 0000000..2a9ff41 --- /dev/null +++ b/src/test/java/org/apache/commons/crypto/examples/RandomExample.java @@ -0,0 +1,60 @@ +/** + * 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. + */ +package org.apache.commons.crypto.examples; + +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.Arrays; +import java.util.Properties; + +import org.apache.commons.crypto.conf.ConfigurationKeys; +import org.apache.commons.crypto.random.CryptoRandom; +import org.apache.commons.crypto.random.CryptoRandomFactory; + +/** + * Example showing use of the CryptoRandom API + */ +public class RandomExample { + + public static void main(String []args) throws GeneralSecurityException, IOException { + //Constructs a byte array to store random data. + byte[] key = new byte[16]; + byte[] iv = new byte[32]; + + Properties properties = new Properties(); + properties.put(ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, + CryptoRandomFactory.RandomProvider.OPENSSL.getClassName()); + + //Gets the 'CryptoRandom' instance. + CryptoRandom random = CryptoRandomFactory.getCryptoRandom(properties); + + // Show the actual class (may be different from the one requested) + System.out.println(random.getClass().getCanonicalName()); + + // Generate random bytes and places them into the byte arrays. + random.nextBytes(key); + random.nextBytes(iv); + + //Closes the CryptoRandom. + random.close(); + + // Show the generated output + System.out.println(Arrays.toString(key)); + System.out.println(Arrays.toString(iv)); + } +} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/3e7836f2/src/test/java/org/apache/commons/crypto/examples/StreamExample.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/examples/StreamExample.java b/src/test/java/org/apache/commons/crypto/examples/StreamExample.java new file mode 100644 index 0000000..372475a --- /dev/null +++ b/src/test/java/org/apache/commons/crypto/examples/StreamExample.java @@ -0,0 +1,82 @@ +/** + * 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. + */ +package org.apache.commons.crypto.examples; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Properties; + +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +import org.apache.commons.crypto.stream.CryptoInputStream; +import org.apache.commons.crypto.stream.CryptoOutputStream; + +/** + * Example showing how to use stream encryption and decryption. + */ +public class StreamExample { + + public static void main(String []args) throws IOException { + final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"),"AES"); + final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456")); + Properties properties = new Properties(); + final String transform = "AES/CBC/PKCS5Padding"; + + String input = "hello world!"; + //Encryption with CryptoOutputStream. + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + try (CryptoOutputStream cos = new CryptoOutputStream(transform, properties, outputStream, key, iv)) { + cos.write(getUTF8Bytes(input)); + cos.flush(); + } + + // The encrypted data: + System.out.println("Encrypted: "+Arrays.toString(outputStream.toByteArray())); + + // Decryption with CryptoInputStream. + InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); + + try (CryptoInputStream cis = new CryptoInputStream(transform, properties, inputStream, key, iv)) { + byte[] decryptedData = new byte[1024]; + int decryptedLen = 0; + int i; + while ((i = cis.read(decryptedData, decryptedLen, decryptedData.length - decryptedLen)) > -1) { + decryptedLen += i; + } + System.out.println("Decrypted: "+new String(decryptedData, 0, decryptedLen, StandardCharsets.UTF_8)); + } + } + + /** + * Converts String to UTF8 bytes + * + * @param input the input string + * @return UTF8 bytes + */ + private static byte[] getUTF8Bytes(String input) { + return input.getBytes(StandardCharsets.UTF_8); + } + +} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/3e7836f2/src/test/java/org/apache/commons/crypto/examples/package-info.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/examples/package-info.java b/src/test/java/org/apache/commons/crypto/examples/package-info.java new file mode 100644 index 0000000..6f9c541 --- /dev/null +++ b/src/test/java/org/apache/commons/crypto/examples/package-info.java @@ -0,0 +1,22 @@ +/** + * 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. + */ + +/** + * Example classes + */ +package org.apache.commons.crypto.examples;
