* Licensed under the GNU General Public License, Version 3 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package cn.com.lzt.common.util.security;
import com.daju.base.exception.BusinessException;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
/**
* RSA秘钥文件缓存类
* @author xiaosf
* @date 2017年6月13日
*/
public class RSAKeys {
/**
* 日志对象
*/
private static Logger logger=Logger.getLogger(RSAKeys.class);
/**
* 公钥
*/
private static Map pubKey=new HashMap();
//初始化公钥
static{
fresh();
}
/**
* 根据系统id获取公钥
* @param sysId 系统id
* @return 公钥
* @throws Exception 找不到对应的公钥抛出此异常
*/
public static PublicKey getPubKeyBySys(String sysId) throws Exception{
PublicKey key=pubKey.get(sysId);
if(key==null){
throw new Exception("系统["+sysId+"]没有对应的公钥配置");
}
return key;
}
/**
* 刷新
*/
private static void fresh(){
InputStream is=RSAKeys.class.getClassLoader().getResourceAsStream("property/keys.properties");
try {
Properties prop=new Properties();
prop.load(is);
is.close();
String keyFile=null;
String[] key=null;
RSASignatureTools.KeyStoreFileType keyFileType= RSASignatureTools.KeyStoreFileType.BINARY;
for(Entry