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