View Javadoc
1    /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.crypto.examples;
19  
20  import java.nio.charset.StandardCharsets;
21  import java.util.Arrays;
22  import java.util.Properties;
23  
24  import javax.crypto.Cipher;
25  import javax.crypto.spec.IvParameterSpec;
26  import javax.crypto.spec.SecretKeySpec;
27  
28  import org.apache.commons.crypto.cipher.CryptoCipher;
29  import org.apache.commons.crypto.cipher.CryptoCipherFactory;
30  import org.apache.commons.crypto.cipher.CryptoCipherFactory.CipherProvider;
31  import org.apache.commons.crypto.utils.AES;
32  import org.apache.commons.crypto.utils.Utils;
33  
34  /**
35   * Example showing use of the CryptoCipher API using a byte array
36   */
37  public class CipherByteArrayExample {
38  
39      /**
40       * Converts String to UTF8 bytes
41       *
42       * @param input the input string
43       * @return UTF8 bytes
44       */
45      private static byte[] getUTF8Bytes(final String input) {
46          return input.getBytes(StandardCharsets.UTF_8);
47      }
48  
49      public static void main(final String[] args) throws Exception {
50  
51          final SecretKeySpec key = AES.newSecretKeySpec(getUTF8Bytes("1234567890123456"));
52          final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));
53  
54          final Properties properties = new Properties();
55          properties.setProperty(CryptoCipherFactory.CLASSES_KEY, CipherProvider.OPENSSL.getClassName());
56          // Creates a CryptoCipher instance with the transformation and properties.
57          final String transform = AES.CBC_PKCS5_PADDING;
58          byte[] output;
59          int updateBytes;
60          int finalBytes;
61          Class<?> encipherClass;
62          try (final CryptoCipher encipher = Utils.getCipherInstance(transform, properties)) {
63              System.out.println("Cipher:  " + encipher.getClass().getCanonicalName());
64  
65              final String sampleInput = "hello world!";
66              System.out.println("input:  " + sampleInput);
67  
68              final byte[] input = getUTF8Bytes(sampleInput);
69              output = new byte[32];
70  
71              // Initializes the cipher with ENCRYPT_MODE, key and iv.
72              encipher.init(Cipher.ENCRYPT_MODE, key, iv);
73              // Continues a multiple-part encryption/decryption operation for byte array.
74              updateBytes = encipher.update(input, 0, input.length, output, 0);
75              System.out.println(updateBytes);
76              // We must call doFinal at the end of encryption/decryption.
77              finalBytes = encipher.doFinal(input, 0, 0, output, updateBytes);
78              System.out.println(finalBytes);
79              encipherClass = encipher.getClass();
80              // Closes the cipher.
81          }
82  
83          System.out.println(Arrays.toString(Arrays.copyOf(output, updateBytes + finalBytes)));
84  
85          // Now reverse the process using a different implementation with the same settings
86          properties.setProperty(CryptoCipherFactory.CLASSES_KEY, CipherProvider.JCE.getClassName());
87          try (final CryptoCipher decipher = Utils.getCipherInstance(transform, properties)) {
88              System.out.println("Cipher:  " + encipherClass.getCanonicalName());
89  
90              decipher.init(Cipher.DECRYPT_MODE, key, iv);
91              final byte[] decoded = new byte[32];
92              decipher.doFinal(output, 0, updateBytes + finalBytes, decoded, 0);
93  
94              System.out.println("output: " + new String(decoded, StandardCharsets.UTF_8));
95          }
96      }
97  
98  }