X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2FPasswordEncryption.java;h=c95c7879ef3f1225b438f11e5c8f8c718c58c908;hb=3c1cdc594d954520b14646102b366290bdad58c7;hp=7269689bcdc6719f5aede40100f6352207b438f4;hpb=6d206b9052689ffa880cd4593bfefa704dc0dd46;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/PasswordEncryption.java b/org.argeo.util/src/org/argeo/util/PasswordEncryption.java index 7269689bc..c95c7879e 100644 --- a/org.argeo.util/src/org/argeo/util/PasswordEncryption.java +++ b/org.argeo.util/src/org/argeo/util/PasswordEncryption.java @@ -1,18 +1,3 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * Licensed 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.argeo.util; import java.io.ByteArrayInputStream; @@ -20,6 +5,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.security.InvalidKeyException; import java.security.Key; @@ -41,7 +28,8 @@ public class PasswordEncryption { public final static String DEFAULT_SECRETE_KEY_FACTORY = "PBKDF2WithHmacSHA1"; public final static String DEFAULT_SECRETE_KEY_ENCRYPTION = "AES"; public final static String DEFAULT_CIPHER_NAME = "AES/CBC/PKCS5Padding"; - public final static String DEFAULT_CHARSET = "UTF-8"; +// public final static String DEFAULT_CHARSET = "UTF-8"; + public final static Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; private Integer iterationCount = DEFAULT_ITERATION_COUNT; private Integer secreteKeyLength = DEFAULT_SECRETE_KEY_LENGTH; @@ -83,11 +71,11 @@ public class PasswordEncryption { + " secrete key length instead of " + previousSecreteKeyLength); try { initKeyAndCiphers(password, passwordSalt, initializationVector); - } catch (Exception e1) { - throw new UtilsException("Cannot get secret key (with restricted length)", e1); + } catch (GeneralSecurityException e1) { + throw new IllegalStateException("Cannot get secret key (with restricted length)", e1); } - } catch (Exception e) { - throw new UtilsException("Cannot get secret key", e); + } catch (GeneralSecurityException e) { + throw new IllegalStateException("Cannot get secret key", e); } } @@ -125,8 +113,6 @@ public class PasswordEncryption { StreamUtils.closeQuietly(out); } catch (IOException e) { throw e; - } catch (Exception e) { - throw new UtilsException("Cannot encrypt", e); } finally { StreamUtils.closeQuietly(decryptedIn); } @@ -138,8 +124,6 @@ public class PasswordEncryption { StreamUtils.copy(decryptedIn, decryptedOut); } catch (IOException e) { throw e; - } catch (Exception e) { - throw new UtilsException("Cannot decrypt", e); } finally { StreamUtils.closeQuietly(encryptedIn); } @@ -153,8 +137,8 @@ public class PasswordEncryption { in = new ByteArrayInputStream(str.getBytes(DEFAULT_CHARSET)); encrypt(in, out); return out.toByteArray(); - } catch (Exception e) { - throw new UtilsException("Cannot encrypt", e); + } catch (IOException e) { + throw new RuntimeException(e); } finally { StreamUtils.closeQuietly(out); } @@ -167,8 +151,8 @@ public class PasswordEncryption { out = new ByteArrayOutputStream(); decrypt(in, out); return new String(out.toByteArray(), DEFAULT_CHARSET); - } catch (Exception e) { - throw new UtilsException("Cannot decrypt", e); + } catch (IOException e) { + throw new RuntimeException(e); } finally { StreamUtils.closeQuietly(out); }