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.io.ByteArrayInputStream;
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.nio.charset.StandardCharsets;
25  import java.util.Arrays;
26  import java.util.Properties;
27  
28  import javax.crypto.spec.IvParameterSpec;
29  import javax.crypto.spec.SecretKeySpec;
30  
31  import org.apache.commons.crypto.stream.CryptoInputStream;
32  import org.apache.commons.crypto.stream.CryptoOutputStream;
33  import org.apache.commons.crypto.utils.AES;
34  
35  /**
36   * Example showing how to use stream encryption and decryption.
37   */
38  public class StreamExample {
39  
40      /**
41       * Converts String to UTF8 bytes
42       *
43       * @param input the input string
44       * @return UTF8 bytes
45       */
46      private static byte[] getUTF8Bytes(final String input) {
47          return input.getBytes(StandardCharsets.UTF_8);
48      }
49  
50      public static void main(final String []args) throws IOException {
51          final SecretKeySpec key = AES.newSecretKeySpec(getUTF8Bytes("1234567890123456"));
52          final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));
53          final Properties properties = new Properties();
54          final String transform = AES.CBC_PKCS5_PADDING;
55  
56          final String input = "hello world!";
57          //Encryption with CryptoOutputStream.
58  
59          final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
60  
61          try (CryptoOutputStream cos = new CryptoOutputStream(transform, properties, outputStream, key, iv)) {
62              cos.write(getUTF8Bytes(input));
63              cos.flush();
64          }
65  
66          // The encrypted data:
67          System.out.println("Encrypted: "+Arrays.toString(outputStream.toByteArray()));
68  
69          // Decryption with CryptoInputStream.
70          final InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
71  
72          try (CryptoInputStream cis = new CryptoInputStream(transform, properties, inputStream, key, iv)) {
73              final byte[] decryptedData = new byte[1024];
74              int decryptedLen = 0;
75              int i;
76              while ((i = cis.read(decryptedData, decryptedLen, decryptedData.length - decryptedLen)) > -1) {
77                  decryptedLen += i;
78              }
79              System.out.println("Decrypted: "+new String(decryptedData, 0, decryptedLen, StandardCharsets.UTF_8));
80          }
81      }
82  
83  }