Path: blob/master/test/jdk/com/sun/crypto/provider/CICO/PBEFunc/DefaultPBEWrapper.java
41162 views
/*1* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.security.GeneralSecurityException;23import java.security.Provider;24import java.security.Security;25import javax.crypto.SecretKey;26import javax.crypto.Cipher;27import javax.crypto.SecretKeyFactory;28import javax.crypto.spec.PBEKeySpec;29import javax.crypto.spec.PBEParameterSpec;3031/**32* Default wrapper for a password based encryption Cipher.33*/3435public class DefaultPBEWrapper extends AbstractPBEWrapper {36/**37* Define default SALT size as 8.38*/39private static final int PBE_SALT_SIZE = 8;4041/**42* Default PBE wrapper constructor.43*44* @param algo PGE algorithm to wrap.45* @param passwd password phrase46*/47public DefaultPBEWrapper(PBEAlgorithm algo, String passwd) {48super(algo, passwd, PBE_SALT_SIZE);49}5051/**52* Instantiate Cipher for the PBE algorithm.53*54* @param mode Cipher mode: encrypt or decrypt.55* @return Cipher in accordance to the PBE algorithm56* @throws java.security.GeneralSecurityException57*/58@Override59protected Cipher initCipher(int mode) throws GeneralSecurityException {60Provider provider = Security.getProvider("SunJCE");61if (provider == null) {62throw new RuntimeException("SunJCE provider does not exist.");63}64SecretKey key = SecretKeyFactory.getInstance(baseAlgo)65.generateSecret(new PBEKeySpec(password.toCharArray()));66Cipher ci = Cipher.getInstance(transformation, provider);67ci.init(mode, key, new PBEParameterSpec(salt, DEFAULT_ITERATION));68return ci;69}70}717273