1 21 22 package net.sourceforge.jcetaglib.jsp20; 23 24 import net.sourceforge.jcetaglib.exceptions.CryptoException; 25 import net.sourceforge.jcetaglib.exceptions.KeystoreException; 26 import net.sourceforge.jcetaglib.lib.Crypt; 27 import net.sourceforge.jcetaglib.lib.Digesters; 28 import net.sourceforge.jcetaglib.lib.Macs; 29 import net.sourceforge.jcetaglib.lib.PBECrypt; 30 31 import java.io.IOException ; 32 33 41 public class ELFunctions { 42 43 51 public static String digest(String text, 52 String algorithm) throws CryptoException { 53 54 if (algorithm == null || algorithm.equalsIgnoreCase("")) { 55 return new String (Digesters.hash(new StringBuffer (text), "SHA-1")); 56 } else { 57 return new String (Digesters.hash(new StringBuffer (text), algorithm)); 58 } 59 } 60 61 73 public static String formdigest(String text, 74 String digest, 75 String keyfile, 76 String passphrase, 77 String algorithm) throws CryptoException, IOException { 78 if (digest == null || digest.equalsIgnoreCase("")) { 79 return new String (Digesters.formDigest(new StringBuffer (text), "SHA-1", keyfile, new StringBuffer (passphrase), algorithm)); 80 } else { 81 return new String (Digesters.formDigest(new StringBuffer (text), digest, keyfile, new StringBuffer (passphrase), algorithm)); 82 } 83 } 84 85 95 public static String pbeencrypt(String text, 96 String passphrase, 97 String algorithm) throws CryptoException { 98 if (algorithm == null || algorithm.equalsIgnoreCase("")) { 99 return new String (PBECrypt.encrypt(new StringBuffer (text), new StringBuffer (passphrase), "PBEWithSHAAndIDEA-CBC")); 100 } else { 101 return new String (PBECrypt.encrypt(new StringBuffer (text), new StringBuffer (passphrase), algorithm)); 102 } 103 } 104 105 114 public static String pbedecrypt(String ciphertext, 115 String passphrase, 116 String algorithm) throws CryptoException { 117 if (algorithm == null || algorithm.equalsIgnoreCase("")) { 118 return new String (PBECrypt.decrypt(new StringBuffer (ciphertext), new StringBuffer (passphrase), "PBEWithSHAAndIDEA-CBC")); 119 } else { 120 return new String (PBECrypt.decrypt(new StringBuffer (ciphertext), new StringBuffer (passphrase), algorithm)); 121 } 122 } 123 124 138 public static String encrypt(String text, 139 String keyfile, 140 String passphrase, 141 String algorithm, 142 String mode, 143 String padding) throws CryptoException, KeystoreException { 144 145 String t_algorithm = algorithm; 146 String t_mode = mode; 147 String t_padding = padding; 148 149 if (algorithm == null || algorithm.equalsIgnoreCase("")) { 150 t_algorithm = "AES"; 151 } 152 153 if (mode == null || mode.equalsIgnoreCase("")) { 154 t_mode = "CBC"; 155 } 156 157 if (padding == null || padding.equalsIgnoreCase("")) { 158 t_padding = "PKCS7Padding"; 159 } 160 161 return new String (Crypt.encrypt(new StringBuffer (text), keyfile, new StringBuffer (passphrase), t_algorithm, t_mode, t_padding, null)); 162 } 163 164 177 public static String decrypt(String ciphertext, 178 String keyfile, 179 String passphrase, 180 String algorithm, 181 String mode, 182 String padding) throws CryptoException, KeystoreException { 183 184 String t_algorithm = algorithm; 185 String t_mode = mode; 186 String t_padding = padding; 187 188 if (algorithm == null || algorithm.equalsIgnoreCase("")) { 189 t_algorithm = "AES"; 190 } 191 192 if (mode == null || mode.equalsIgnoreCase("")) { 193 t_mode = "CBC"; 194 } 195 196 if (padding == null || padding.equalsIgnoreCase("")) { 197 t_padding = "PKCS7Padding"; 198 } 199 200 return new String (Crypt.decrypt(new StringBuffer (ciphertext), keyfile, new StringBuffer (passphrase), t_algorithm, t_mode, t_padding)); 201 } 202 203 214 public static String mac(String text, 215 String keyfile, 216 String passphrase, 217 String algorithm, 218 String macname) throws CryptoException { 219 if (macname == null || macname.equalsIgnoreCase("")) { 220 return new String (Macs.generateMAC(new StringBuffer (text), keyfile, new StringBuffer (passphrase), algorithm, "HMac-SHA512")); 221 } else { 222 return new String (Macs.generateMAC(new StringBuffer (text), keyfile, new StringBuffer (passphrase), algorithm, macname)); 223 } 224 } 225 } | Popular Tags |