View Javadoc
1   package org.jastacry.layer;
2   
3   import java.security.NoSuchAlgorithmException;
4   import java.security.spec.InvalidKeySpecException;
5   import java.util.Arrays;
6   
7   import javax.crypto.SecretKeyFactory;
8   import javax.crypto.spec.PBEKeySpec;
9   import javax.crypto.spec.SecretKeySpec;
10  
11  import org.jastacry.JastacryException;
12  
13  /**
14   * AES Layer class.
15   *
16   * <p>SPDX-License-Identifier: MIT
17   *
18   * @author Kai Kretschmann
19   */
20  public class AesCbcLayer extends AbstractCipherLayer
21  {
22      /**
23       * static name of the layer.
24       */
25      public static final String LAYERNAME = "AES CBC Layer";
26  
27      /**
28       * Used algorithm name.
29       */
30      private static final String MYALG = "AES/CBC/PKCS5Padding";
31  
32      /**
33       * Used algorithm name for the key.
34       */
35      private static final String MYKEYALG = "AES";
36  
37      /**
38       * Used algorithm name for the key factory.
39       */
40      private static final String MYKEYFACT = "PBKDF2WithHmacSHA1";
41  
42      /**
43       * IV length.
44       */
45      private static final int IVLEN = 16;
46  
47      /**
48       * Salt length.
49       */
50      private static final int SALTLEN = 32;
51  
52      /**
53       * Iteration count.
54       */
55      private static final int COUNT = 10000;
56  
57      /**
58       * Size of key.
59       */
60      private static final int KEYSIZE = 128;
61  
62      /**
63       * Constructor of AesLayer.
64       */
65      public AesCbcLayer()
66      {
67          super(AesCbcLayer.class, LAYERNAME);
68      }
69  
70      /**
71       * Generate Keys from plain password.
72       *
73       * @throws JastacryException on error
74       */
75      @Override
76      protected final void setupPbe() throws JastacryException
77      {
78          try
79          {
80              keyFac = SecretKeyFactory.getInstance(MYKEYFACT);
81              pbeKeySpec = new PBEKeySpec(chPasswd, salt, iterCount, currentKeysize);
82              pbeKey = keyFac.generateSecret(pbeKeySpec);
83              pbeSecretKeySpec = new SecretKeySpec(pbeKey.getEncoded(), strKeyAlg);
84          }
85          catch (NoSuchAlgorithmException | InvalidKeySpecException e)
86          {
87              throw (JastacryExceptionl#JastacryException">JastacryException) new JastacryException("Setup PBE failed").initCause(e);
88          }
89      }
90  
91      /**
92       * init function. Overrides base init but uses it for setting base values.
93       *
94       * @param data to initialize the crypt value.
95       */
96      @Override
97      public final void init(final String data)
98      {
99          super.init();
100 
101         this.chPasswd = data.toCharArray();
102     }
103 
104     @Override
105     protected final String getMyAlg()
106     {
107         return MYALG;
108     }
109 
110     @Override
111     protected final String getMyKeyAlg()
112     {
113         return MYKEYALG;
114     }
115 
116     @Override
117     protected int getMySaltLen()
118     {
119         return SALTLEN;
120     }
121 
122     @Override
123     protected int getMyIvLen()
124     {
125         return IVLEN;
126     }
127 
128     @Override
129     protected int getMyCount()
130     {
131         return COUNT;
132     }
133 
134     @Override
135     protected int getMyKeysize()
136     {
137         return KEYSIZE;
138     }
139 
140     /**
141      * Override equals method from object class.
142      * @param o object to compare with
143      * @return true or false
144      */
145     @Override
146     public boolean equals(final Object o)
147     {
148         if (o == this)
149         {
150             return true;
151         }
152         if (!(o instanceof AesCbcLayer))
153         {
154             return false;
155         }
156 
157         final AesCbcLayer./../org/jastacry/layer/AesCbcLayer.html#AesCbcLayer">AesCbcLayer layer = (AesCbcLayer) o;
158         return Arrays.equals(layer.chPasswd, this.chPasswd);
159     }
160 
161     /**
162      * Override equals method from object class.
163      * @return hash of properties
164      */
165     @Override
166     public int hashCode()
167     {
168         return Arrays.hashCode(chPasswd);
169     }
170 }