1 34 35 36 package com.micronova.util.codec; 37 38 import java.text.*; 39 import java.security.*; 40 import javax.crypto.*; 41 import javax.crypto.spec.*; 42 43 import com.micronova.util.*; 44 45 46 47 public class CodecSecurity extends Codec 48 { 49 50 51 public static final Object digest(Object object, Object algorithm) throws Exception 52 { 53 if (object != null) 54 { 55 MessageDigest messageDigest = MessageDigest.getInstance(algorithm.toString()); 56 57 byte[] input; 58 59 if (object instanceof byte[]) 60 { 61 input = (byte[])object; 62 } 63 else 64 { 65 input = StringUtil.fromBinaryString(object.toString()); 66 } 67 68 return StringUtil.toBinaryString(messageDigest.digest(input)); 69 } 70 71 return object; 72 } 73 74 75 76 public static final Object secureRandom(Object object, Object algorithm) throws Exception 77 { 78 if (object != null) 79 { 80 SecureRandom secureRandom = SecureRandom.getInstance(algorithm.toString()); 81 String objectString = object.toString(); 82 83 int size = 0; 84 85 try 86 { 87 size = Integer.parseInt(objectString); 88 } 89 catch (Exception e) 90 { 91 } 92 93 if (size <= 0) 94 { 95 size = StringUtil.fromBinaryString(objectString).length; 96 } 97 98 byte[] b = new byte[size]; 99 100 secureRandom.nextBytes(b); 101 102 object = StringUtil.toBinaryString(b); 103 } 104 105 return object; 106 } 107 108 109 110 public static Object generateSecretKey(Object object) throws Exception 111 { 112 if (object != null) 113 { 114 String algorithm = object.toString(); 115 116 KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm); 117 118 SecretKey secretKey = keyGenerator.generateKey(); 119 120 object = StringUtil.toBinaryString(secretKey.getEncoded()); 121 } 122 123 return object; 124 } 125 126 private static Object crypt(Object object, Object keySpec, Object algorithmSpec, Object ivSpec, int mode) throws Exception 127 { 128 if (object != null) 129 { 130 String algorithm = algorithmSpec.toString(); 131 132 byte[] source = StringUtil.fromBinaryString(object.toString()); 133 134 Cipher cipher = Cipher.getInstance(algorithm); 135 136 byte[] keyBytes = StringUtil.fromBinaryString(keySpec.toString()); 137 138 SecretKey secretKey = new SecretKeySpec(keyBytes, algorithm.replaceFirst("/.*$", "")); 139 140 if (ivSpec != null) 141 { 142 cipher.init(mode, secretKey, new IvParameterSpec(ivSpec.toString().getBytes("iso-8859-1"))); 143 } 144 else 145 { 146 cipher.init(mode, secretKey); 147 } 148 149 object = StringUtil.toBinaryString(cipher.doFinal(source)); 150 } 151 152 return object; 153 } 154 155 public static Object encrypt(Object object, Object keySpec, Object algorithmSpec, Object ivSpec) throws Exception 156 { 157 return crypt(object, keySpec, algorithmSpec, ivSpec, Cipher.ENCRYPT_MODE); 158 } 159 160 public static Object encrypt(Object object, Object keySpec, Object algorithmSpec) throws Exception 161 { 162 return encrypt(object, keySpec, algorithmSpec, null); 163 } 164 165 public static Object decrypt(Object object, Object keySpec, Object algorithmSpec, Object ivSpec) throws Exception 166 { 167 return crypt(object, keySpec, algorithmSpec, ivSpec, Cipher.DECRYPT_MODE); 168 } 169 170 public static Object decrypt(Object object, Object keySpec, Object algorithmSpec) throws Exception 171 { 172 return decrypt(object, keySpec, algorithmSpec, null); 173 } 174 } 175 176 | Popular Tags |