1    
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
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  
36  
37  public class CipherByteArrayExample {
38  
39      
40  
41  
42  
43  
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          
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              
72              encipher.init(Cipher.ENCRYPT_MODE, key, iv);
73              
74              updateBytes = encipher.update(input, 0, input.length, output, 0);
75              System.out.println(updateBytes);
76              
77              finalBytes = encipher.doFinal(input, 0, 0, output, updateBytes);
78              System.out.println(finalBytes);
79              encipherClass = encipher.getClass();
80              
81          }
82  
83          System.out.println(Arrays.toString(Arrays.copyOf(output, updateBytes + finalBytes)));
84  
85          
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  }