1/* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.io.PrintStream; 25import java.util.Random; 26import javax.crypto.Cipher; 27import javax.crypto.SecretKey; 28 29/** 30 * PBEWrapper is the abstract class for all concrete PBE Cipher wrappers. A 31 * PBEWrapper object encapsulates the information and behavior needed to test if 32 * the multiple-part encryption/decryption is performing by expected way on the 33 * same byte buffer. 34 * 35 * @author Alexandr Fomin 36 * @author rhalade 37 */ 38public abstract class PBEWrapper { 39 40 protected final static int ITERATION_COUNT = 1000; 41 42 protected final SecretKey key; 43 protected final Cipher ci; 44 protected final String algo; 45 protected final PrintStream out; 46 47 public PBEWrapper(String pAlgo, SecretKey pKey, Cipher pCi, 48 PrintStream pOut ){ 49 this.algo = pAlgo; 50 this.key = pKey; 51 this.ci = pCi; 52 this.out = pOut; 53 } 54 55 /** 56 * Abstract method need to be implemented in the subclasses. 57 * 58 * @param edMode Cipher mode - encrypt/decrypt 59 * @param inputText byte buffer to process 60 * @param offset offset in byte the inputText 61 * @param len length of byte to process in inputText 62 * @return true if cipher operation is successful, false otherwise 63 */ 64 public abstract boolean execute(int edMode, byte[] inputText, int offset, 65 int len); 66 67 /** 68 * An utility method to prepare "salt" for following Secret Key generation. 69 * 70 * @param numberOfBytes number of bytes in salt 71 * @return randomly generated byte array 72 */ 73 protected static byte[] generateSalt(int numberOfBytes) { 74 byte[] salt = new byte[numberOfBytes]; 75 new Random().nextBytes(salt); 76 return salt; 77 } 78 79 /** 80 * An utility method to check if two byte arrays are equal 81 * 82 * @param b1 first byte array 83 * @param off1 offset to compare from in b1 84 * @param b2 second byte array 85 * @param off2 offset to compare from in b2 86 * @param len length to compare 87 * @return true of arrays are equal, false otherwise 88 */ 89 protected boolean equalsBlock(byte[] b1, int off1, 90 byte[] b2, int off2, int len) { 91 for (int i = off1, j = off2, k = 0; k < len; i++, j++, k++) { 92 if (b1[i] != b2[j]) { 93 return false; 94 } 95 } 96 return true; 97 } 98} 99