日期:2014-05-18 浏览次数:20983 次
public class EncryptPasswordUtil {
private static LogService log = LogService
.getInstance(EncryptPasswordUtil.class);
private static final String ENCRPTY_FILE_NAME = "test.cer";
public static String generateTCSRSAPassword(String password) {
PublicKey tcsPublicKey = null;
try {
tcsPublicKey = getPublicKeyFromX509(ENCRPTY_FILE_NAME);
if (tcsPublicKey != null) {
log.debug("Load the file: " + ENCRPTY_FILE_NAME);
Cipher tcsCipher = Cipher.getInstance(tcsPublicKey
.getAlgorithm());
tcsCipher.init(Cipher.ENCRYPT_MODE, tcsPublicKey);
String tcsEncryptPassword = Base64.encodeBase64String(tcsCipher
.doFinal(Base64.encodeBase64(password.getBytes())));
return tcsEncryptPassword;
} else {
log.warn(ENCRPTY_FILE_NAME + " cannot be found!");
throw new Exception(ENCRPTY_FILE_NAME
+ " cannot be found!");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static PublicKey getPublicKeyFromX509(String filename)
throws Exception {
InputStream fin = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(ENCRPTY_FILE_NAME);
CertificateFactory f = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) f
.generateCertificate(fin);
PublicKey pk = certificate.getPublicKey();
return pk;
}
}
package com.gus.encipherment;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import org.apache.commons.codec.binary.Hex;
/**
* 数字证书应用
* @author gus
* @since 2011/02/29
*/
public class DC {
/**
* 由密钥库获得私钥
* @param keyStorePath 密钥库路径
* @param passwd
* @param alias