papegaaij commented on a change in pull request #462:
URL: https://github.com/apache/wicket/pull/462#discussion_r593866073



##########
File path: wicket-util/src/main/java/org/apache/wicket/util/crypt/AESCrypt.java
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.util.crypt;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.IvParameterSpec;
+
+/**
+ * AES based {@link ICrypt} encrypt and decrypt strings such as passwords or 
URL segments.
+ * Based on http://stackoverflow.com/a/992413
+ * 
+ * @see ICrypt
+ */
+class AESCrypt extends AbstractJceCrypt
+{
+
+       private final SecretKey secretKey;
+       private final String algorithm;
+       private final int ivSize;
+
+       
+       /**
+        * Constructor
+        * 
+        * @param secretKey
+        *              The {@link SecretKey} to use to initialize the {@link 
Cipher}.
+        */
+       public AESCrypt(SecretKey secretKey)
+       {
+               this(secretKey, "AES/CBC/PKCS5Padding", 16);
+       }
+
+       /**
+        * Constructor
+        * 
+        * @param secretKey
+        *              The {@link SecretKey} to use to initialize the {@link 
Cipher}.
+        * @param algorithm
+        *              The cipher algorithm to use, for example 
"AES/CBC/PKCS5Padding".
+        * @param ivSize
+        *              The size of the Initialization Vector to use with the 
cipher.

Review comment:
       You get the Cipher with just `Cipher.getInstance(alg)`. There you can 
get the block size from. You only need the iv for the call to `init`. I'm 
typing this on my phone, so posting code is a bit difficult. What about this?
   
   ```
   public byte[] decrypt(SecureRandom rnd, SecretKey key, byte[] cipherInput) 
throws GeneralSecurityException {
       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
       int blocksize = cipher.getBlockSize();
       byte[] iv = new byte[blocksize];
       byte[] ciphertext = new byte[cipherInput.length - blocksize];
       System.arraycopy(cipherInput, 0, iv, 0, iv.length);
       System.arraycopy(cipherInput, blocksize, ciphertext, 0, 
ciphertext.length);
   
       
       cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv), rnd);
       return cipher.doFinal(ciphertext);
   }```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to