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.ByteBuffer;
21  import java.nio.charset.StandardCharsets;
22  import java.util.Arrays;
23  import java.util.Properties;
24  
25  import javax.crypto.Cipher;
26  import javax.crypto.spec.IvParameterSpec;
27  import javax.crypto.spec.SecretKeySpec;
28  
29  import org.apache.commons.crypto.cipher.CryptoCipher;
30  import org.apache.commons.crypto.utils.AES;
31  import org.apache.commons.crypto.utils.Utils;
32  
33  
34  
35  
36  public class CipherByteBufferExample {
37  
38      
39  
40  
41  
42  
43  
44      private static String asString(final ByteBuffer buffer) {
45          final ByteBuffer copy = buffer.duplicate();
46          final byte[] bytes = new byte[copy.remaining()];
47          copy.get(bytes);
48          return new String(bytes, StandardCharsets.UTF_8);
49      }
50  
51      
52  
53  
54  
55  
56  
57      private static byte[] getUTF8Bytes(final String input) {
58          return input.getBytes(StandardCharsets.UTF_8);
59      }
60  
61      public static void main(final String[] args) throws Exception {
62          final SecretKeySpec key = AES.newSecretKeySpec(getUTF8Bytes("1234567890123456"));
63          final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));
64          final Properties properties = new Properties();
65          
66          final String transform = AES.CBC_PKCS5_PADDING;
67          final ByteBuffer outBuffer;
68          final int bufferSize = 1024;
69          final int updateBytes;
70          final int finalBytes;
71          try (CryptoCipher encipher = Utils.getCipherInstance(transform, properties)) {
72  
73              final ByteBuffer inBuffer = ByteBuffer.allocateDirect(bufferSize);
74              outBuffer = ByteBuffer.allocateDirect(bufferSize);
75              inBuffer.put(getUTF8Bytes("hello world!"));
76  
77              inBuffer.flip(); 
78              
79              System.out.println("inBuffer=" + asString(inBuffer));
80  
81              
82              encipher.init(Cipher.ENCRYPT_MODE, key, iv);
83              
84              updateBytes = encipher.update(inBuffer, outBuffer);
85              System.out.println(updateBytes);
86  
87              
88              finalBytes = encipher.doFinal(inBuffer, outBuffer);
89              System.out.println(finalBytes);
90          }
91  
92          outBuffer.flip(); 
93          final byte [] encoded = new byte[updateBytes + finalBytes];
94          outBuffer.duplicate().get(encoded);
95          System.out.println(Arrays.toString(encoded));
96  
97          
98          try (CryptoCipher decipher = Utils.getCipherInstance(transform, properties)) {
99              decipher.init(Cipher.DECRYPT_MODE, key, iv);
100             final ByteBuffer decoded = ByteBuffer.allocateDirect(bufferSize);
101             decipher.update(outBuffer, decoded);
102             decipher.doFinal(outBuffer, decoded);
103             decoded.flip(); 
104             System.out.println("decoded="+asString(decoded));
105         }
106     }
107 
108 }