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.Utils;
32  
33  /**
34   * Example showing use of the CryptoCipher API using a byte array
35   */
36  public class CipherByteArrayExample {
37  
38      public static void main(String[] args) throws Exception {
39  
40          final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"),"AES");
41          final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));
42  
43          Properties properties = new Properties();
44          properties.setProperty(CryptoCipherFactory.CLASSES_KEY, CipherProvider.OPENSSL.getClassName());
45          //Creates a CryptoCipher instance with the transformation and properties.
46          final String transform = "AES/CBC/PKCS5Padding";
47          CryptoCipher encipher = Utils.getCipherInstance(transform, properties);
48          System.out.println("Cipher:  " + encipher.getClass().getCanonicalName());
49  
50          final String sampleInput = "hello world!";
51          System.out.println("input:  " + sampleInput);
52  
53          byte[] input = getUTF8Bytes(sampleInput);
54          byte[] output = new byte[32];
55  
56          //Initializes the cipher with ENCRYPT_MODE, key and iv.
57          encipher.init(Cipher.ENCRYPT_MODE, key, iv);
58          //Continues a multiple-part encryption/decryption operation for byte array.
59          int updateBytes = encipher.update(input, 0, input.length, output, 0);
60          System.out.println(updateBytes);
61          //We must call doFinal at the end of encryption/decryption.
62          int finalBytes = encipher.doFinal(input, 0, 0, output, updateBytes);
63          System.out.println(finalBytes);
64          //Closes the cipher.
65          encipher.close();
66  
67          System.out.println(Arrays.toString(Arrays.copyOf(output, updateBytes+finalBytes)));
68  
69          // Now reverse the process using a different implementation with the same settings
70          properties.setProperty(CryptoCipherFactory.CLASSES_KEY, CipherProvider.JCE.getClassName());
71          CryptoCipher decipher = Utils.getCipherInstance(transform, properties);
72          System.out.println("Cipher:  " + encipher.getClass().getCanonicalName());
73  
74          decipher.init(Cipher.DECRYPT_MODE, key, iv);
75          byte [] decoded = new byte[32];
76          decipher.doFinal(output, 0, updateBytes + finalBytes, decoded, 0);
77  
78          System.out.println("output: " + new String(decoded, StandardCharsets.UTF_8));
79      }
80  
81      /**
82       * Converts String to UTF8 bytes
83       *
84       * @param input the input string
85       * @return UTF8 bytes
86       */
87      private static byte[] getUTF8Bytes(String input) {
88          return input.getBytes(StandardCharsets.UTF_8);
89      }
90  
91  }