1 /** 2 * Copyright 2014 Internet2 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package edu.internet2.middleware.grouperClientExt.edu.internet2.middleware.morphString; 17 18 import java.io.InputStream; 19 import java.io.OutputStream; 20 21 import javax.crypto.Cipher; 22 23 /** 24 * The purpose of this class is to provide encryption 25 * and decryption using standard Java libraries, for potentially 26 * large amounts of data. 27 * <p> 28 * This class provides default encryption using AES with a constant 29 * 128 bit key. If you want something more secure feel free to 30 * override the defaults however you please. 31 * <p> 32 * This class works in one of two ways, (1) in memory using Strings, or (2) via 33 * I/O streams (preferred for large amounts of data). 34 * <p> 35 * Crypo objects, or more specifically the default ciphers they create, are not 36 * threadsafe and are not computationally cheap, so a threadlocal factory 37 * method is provided for convenience. This is the preferred means of usage, 38 * but feel free to create these objects however you please. 39 * <p> 40 * Note that you can encrypt BLOB fields by specifying encryption in the 41 * configurator (Crypto is the default encryption mechanism for that). 42 * <p> 43 * @deprecated use edu.internet2.middleware.morphString.Crypto instead! 44 */ 45 @Deprecated 46 public class Crypto { 47 48 private edu.internet2.middleware.morphString.Crypto crypto = null; 49 50 /** @return a non-null thread-safe crypto object from a ThreadLocal */ 51 public static Crypto getThreadLocalCrypto() { 52 return new Crypto(edu.internet2.middleware.morphString.Crypto.getThreadLocalCrypto()); 53 } 54 55 public Crypto(edu.internet2.middleware.morphString.Crypto theCrypto) { 56 this.crypto = theCrypto; 57 } 58 59 /** 60 * Generate a key. 61 * @param cipherName the name of the cipher, if null will default to "AES" 62 * @param keybits the number of bits in the key, if null will default to 128 63 * @return the bytes comprising the key 64 */ 65 public static byte[] generateKeyBytes(String cipherName, Integer keybits) { 66 return edu.internet2.middleware.morphString.Crypto.generateKeyBytes(cipherName, keybits); 67 } 68 69 70 /** 71 * Create the default cipher 72 * @return the default cipher 73 */ 74 public Cipher createDefaultCipher() { 75 return this.crypto.createDefaultCipher(); 76 } 77 78 /** Default crypto object */ 79 public Crypto() { 80 this(Morph.key()); 81 } 82 83 /** Default crypto object 84 * @param theKey used to encrypt/decrypt 85 */ 86 public Crypto(String theKey) { 87 this.crypto = new edu.internet2.middleware.morphString.Crypto(theKey); 88 } 89 90 /** 91 * Encrypt the string 92 * @param clearText 93 * @return the encrypted String 94 */ 95 public String encrypt(String clearText) { 96 return this.crypto.encrypt(clearText); 97 } 98 99 /** 100 * Decrypt the string 101 * @param cipherText 102 * @return the decrypted string 103 */ 104 public String decrypt(String cipherText) { 105 return this.crypto.decrypt(cipherText); 106 } 107 108 /** 109 * Get the encrypted input stream 110 * @param in 111 * @return the encrypted input stream 112 */ 113 public InputStream encrypt(InputStream in) { 114 return this.crypto.encrypt(in); 115 } 116 117 /** 118 * the decrypted input stream 119 * @param in 120 * @return the decrypted input stream 121 */ 122 public InputStream decrypt(InputStream in) { 123 return this.crypto.decrypt(in); 124 } 125 126 /** 127 * the encrypted output stream 128 * @param out 129 * @return the encrypted output stream 130 */ 131 public OutputStream encrypt(OutputStream out) { 132 return this.crypto.encrypt(out); 133 } 134 135 /** 136 * the decrypted output stream 137 * @param out 138 * @return the decrypted output stream 139 */ 140 public OutputStream decrypt(OutputStream out) { 141 return this.crypto.decrypt(out); 142 } 143 }