Path: blob/master/test/jdk/com/sun/crypto/provider/CICO/PBEFunc/AESPBEWrapper.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*/2223import java.security.AlgorithmParameters;24import java.security.GeneralSecurityException;25import java.security.Provider;26import java.security.Security;27import javax.crypto.SecretKey;28import javax.crypto.Cipher;29import javax.crypto.SecretKeyFactory;30import javax.crypto.spec.PBEKeySpec;3132/**33* Wrapper class to test a given AES-based PBE algorithm.34*/35public class AESPBEWrapper extends AbstractPBEWrapper {36/**37* the algorithm parameters.38*/39private AlgorithmParameters pbeParams;4041/**42* the encryption key.43*/44private final SecretKey key;4546/**47* The Wrapper constructor. Instantiate Cipher using the given AES-based PBE48* algorithm.49*50* @param algo AES-based PBE algorithm.51* @param passwd password phrase.52* @throws GeneralSecurityException all security exceptions are thrown.53*/54public AESPBEWrapper(PBEAlgorithm algo, String passwd)55throws GeneralSecurityException {56// salt and iteration count will be generated during encryption57super(algo, passwd, 0);5859// Generate secret key. We expect no mode and padding specified.60SecretKeyFactory skf = SecretKeyFactory.getInstance(algo.baseAlgo);61key = skf.generateSecret(new PBEKeySpec(passwd.toCharArray()));62}6364/**65* Initiate the Cipher object using given "mode".66* @return a cipher object.67* @throws GeneralSecurityException all security exceptions are thrown.68*/69@Override70protected Cipher initCipher(int mode) throws GeneralSecurityException {71Provider provider = Security.getProvider("SunJCE");72if (provider == null) {73throw new RuntimeException("SunJCE provider does not exist.");74}75// get Cipher instance76Cipher ci = Cipher.getInstance(transformation, provider);77if (Cipher.ENCRYPT_MODE == mode) {78ci.init(Cipher.ENCRYPT_MODE, key);79pbeParams = ci.getParameters();80} else {81ci.init(Cipher.DECRYPT_MODE, key, pbeParams);82}83return ci;84}85}868788