- 浏览: 1356661 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (453)
- Struts2 (4)
- Struts1 (16)
- Spring (9)
- Hibernate (5)
- AJAX (16)
- MS Server (9)
- Oracle (35)
- 测试技术 (5)
- 其他 (37)
- JSF (1)
- EJB (5)
- ASP.NET (9)
- iBATIS (5)
- Struts Spring Hibernate (2)
- HTML (12)
- Eclipse使用 (5)
- 设计模式 (5)
- JSP (32)
- 正则表达式 (2)
- 服务器 (12)
- Java基础 (48)
- MySQL (4)
- 面试 (22)
- UML (1)
- 数据结构 (3)
- Ant (2)
- 规范 (4)
- JavaScript (29)
- 软件使用及技巧 (25)
- PHP (1)
- DWR (3)
- FreeMarker (1)
- ExtJs (17)
- JFreeChart (9)
- Reports (2)
- JavaException (1)
- Java Mail (1)
- Flex (9)
- 异常 (12)
- Log4j (2)
- WebService (1)
- jQuery (1)
- Sybase (2)
- myeclipse_plugin (2)
- Linux (5)
- jbpm (2)
- SCM (1)
- mongoDB (1)
最新评论
-
lihua2008love:
<typeAlias alias="User& ...
IBATIS简单操作 -
lihua2008love:
...
关于项目框架设计的一点学习 -
c_jinglin:
我使用<OBJECT CLASSID="cls ...
这就是个静态页面通过js控制mscomm对某com设备进行访问的例子. -
zlbjava:
赞,用了还不错
java获取请求的ip地址,并解析ip所属地区 -
完美天龙:
cs842813290 写道楼主,我明白的地方:在链表已有N个 ...
JAVA实现双向链表
首先 需要下载jar包 bcprov-jdk15-145.jar
实体类
package com.hongan.lh.cert;
import java.security.KeyPair;
import java.security.cert.X509Certificate;
public class CertInfo {
private KeyPair key;
private X509Certificate cert;
public KeyPair getKey() {
return key;
}
public void setKey(KeyPair key) {
this.key = key;
}
public X509Certificate getCert() {
return cert;
}
public void setCert(X509Certificate cert) {
this.cert = cert;
}
}
帮助类
package com.hongan.lh.cert;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.bouncycastle.openssl.PEMReader;
import org.bouncycastle.openssl.PEMWriter;
import org.bouncycastle.openssl.PasswordFinder;
public class HongAnUtils {
public static KeyPair getPrivateKey(String rootMiyuePath,
final String rootMiyuePwd) throws Exception {
PEMReader reader = new PEMReader(new InputStreamReader(
new FileInputStream(rootMiyuePath)), new PasswordFinder() {
public char[] getPassword() {
// TODO Auto-generated method stub
return rootMiyuePwd.toCharArray();
}
});
KeyPair key = (KeyPair) reader.readObject();
return key;
}
public static void saveX509Certificate(X509Certificate certificate,
String rootcertPath) throws Exception {
FileOutputStream stream = new FileOutputStream(rootcertPath);
stream.write(certificate.getEncoded());
stream.close();
}
public static void savePEM(PrivateKey key, String rootMiyuePwd,
String rootMiyuePath) throws Exception {
PEMWriter writer = new PEMWriter(new FileWriter(rootMiyuePath));
writer.writeObject(key, "DESEDE", rootMiyuePwd.toCharArray(),
new SecureRandom());
writer.close();
}
public static KeyPair generateRSAKeyPair() throws Exception {
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA");
kpGen.initialize(1024, new SecureRandom());
return kpGen.generateKeyPair();
}
public static byte[] RSASign(PrivateKey key, byte[] src) throws Exception {
java.security.Signature sign = java.security.Signature.getInstance(
"SHA1withRSA", "BC");
sign.initSign(key);
sign.update(src);
return sign.sign();
}
public static boolean RSAVerifySign(PublicKey key, byte[] crypt, byte[] src)
throws Exception {
java.security.Signature sign = java.security.Signature.getInstance(
"SHA1withRSA", "BC");
sign.initVerify(key);
sign.update(src);
return sign.verify(crypt);
}
public static X509Certificate getCertificate(String rootCertPath)
throws Exception {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
FileInputStream inputStream = new FileInputStream(rootCertPath);
X509Certificate certificate = (X509Certificate) factory
.generateCertificate(inputStream);
return certificate;
}
public static void write(String file, byte[] data) throws Exception {
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(data);
outputStream.close();
}
public static byte[] read(String file) throws Exception {
FileInputStream stream = new FileInputStream(file);
byte[] by = new byte[stream.available()];
stream.read(by);
stream.close();
return by;
}
public static byte[] encrypt(Key key, byte[] b, String suan)
throws Exception {
Cipher newcipher = Cipher.getInstance(suan);
newcipher.init(Cipher.ENCRYPT_MODE, key);
return newcipher.doFinal(b);
}
public static byte[] encryptByKouling(byte[] s, String suan, String kou)
throws Exception {
PBEKeySpec spec = new PBEKeySpec(kou.toCharArray());
SecretKeyFactory factory = SecretKeyFactory.getInstance(suan);
SecretKey key = factory.generateSecret(spec);
Cipher cipher = Cipher.getInstance(suan);
PBEParameterSpec pps = new PBEParameterSpec(new byte[] { 0xF, 0x0, 0x1,
0x2, 0x3, 0x4, 0x5, 0x6 }, 1);
cipher.init(Cipher.ENCRYPT_MODE, key, pps);
byte[] debyte = cipher.doFinal(s);
return debyte;
}
public static byte[] decryptByKouling(byte[] s, String suan, String kou)
throws Exception {
PBEKeySpec spec = new PBEKeySpec(kou.toCharArray());
SecretKeyFactory factory = SecretKeyFactory.getInstance(suan);
SecretKey key = factory.generateSecret(spec);
Cipher cipher = Cipher.getInstance(suan);
PBEParameterSpec pps = new PBEParameterSpec(new byte[] { 0xF, 0x0, 0x1,
0x2, 0x3, 0x4, 0x5, 0x6 }, 1);
cipher.init(Cipher.DECRYPT_MODE, key, pps);
byte[] debyte = cipher.doFinal(s);
return debyte;
}
}
证书操作类
package com.hongan.lh.cert;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Date;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.security.auth.x500.X500Principal;
import org.bouncycastle.asn1.DEROutputStream;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.x509.X509V1CertificateGenerator;
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CertInfo;
public class HongAnCA {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// CreateRootCert("中国|广东省|深圳市|公司A|3650|1000000000000001|HARootPwd|c://ca//HARoot.pem|c://ca//HARoot.cer");
// BackupRootCert("c://ca//honganbackup.p12|p12pass|c://ca//HARoot.pem|HARootPwd|c://ca//HARoot.cer");
// RestoreRootCert("c://ca//honganbackup.p12|p12pass|c://ca//honganroot_restore.pem|privkeypass|c://ca//honganroot_restore.cer");
// CreateUserCert("中国|广东省|深圳市|一家|廖敏|111@163.com|3333333|365|1000000000000002|c://ca//廖敏.pem|user1pass|c://ca//廖敏.cer|c://ca//HARoot.pem|HARootPwd|c://ca//HARoot.cer");
// ExportCompanyKeyByUserCert("c://ca//companykey.dat|keypassword|c://ca//廖敏.cer|c://ca//廖敏.dat");
// CreateCompanyKey("ABCDEFGHIJKLMNOPQRSTUVWXYZ123456|c://ca//companykey.dat|keypassword");
// ExportCompanyKeyByUserCert("c://ca//companykey.dat|keypassword|c://ca//廖敏.cer|c://ca//廖敏.dat");
}
public static byte[] CreateRootCert(String certInfo) throws Exception {
Security.addProvider(new BouncyCastleProvider());
String[] splt = certInfo.split("//|");
String country = splt[0];
String provice = splt[1];
String shi = splt[2];
String company = splt[3];
String validateTime = splt[4];
String seriszeString = splt[5];
String rootMiyuePwd = splt[6];
String rootMiyuePath = splt[7];
String rootcertPath = splt[8];
String certSubject = "C=" + country + ",S=" + provice + ",L=" + shi
+ ",O=" + company + ",CN=" + company;
KeyPair pair = HongAnUtils.generateRSAKeyPair();
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
certGen.setSerialNumber(new BigInteger(seriszeString));
certGen.setNotBefore(new Date(System.currentTimeMillis()));
certGen.setNotAfter(new Date(System.currentTimeMillis()
+ Long.parseLong(validateTime) * 24 * 60 * 60 * 1000L));
certGen.setSubjectDN(new X500Principal(certSubject));
certGen.setPublicKey(pair.getPublic());
certGen.setIssuerDN(new X500Principal(certSubject));
certGen.setSignatureAlgorithm("SHA1WithRSA");
X509Certificate certificate = certGen.generate(pair.getPrivate());
HongAnUtils.saveX509Certificate(certificate, rootcertPath);
HongAnUtils.savePEM(pair.getPrivate(), rootMiyuePwd, rootMiyuePath);
return certificate.getEncoded();
}
public static void BackupRootCert(String certInfo) throws Exception {
Security.addProvider(new BouncyCastleProvider());
String[] splt = certInfo.split("//|");
String backPath = splt[0];
String backPwd = splt[1];
String rootMiyuePath = splt[2];
final String rootMiyuePwd = splt[3];
String rootCertPath = splt[4];
CertificateFactory factory = CertificateFactory.getInstance("X.509");
FileInputStream inputStream = new FileInputStream(rootCertPath);
X509Certificate certificate = (X509Certificate) factory
.generateCertificate(inputStream);
KeyPair key = HongAnUtils.getPrivateKey(rootMiyuePath, rootMiyuePwd);
Certificate[] chain = new Certificate[1];
chain[0] = certificate;
KeyStore store = KeyStore.getInstance("PKCS12", "BC");
store.load(null, null);
store.setCertificateEntry("root", certificate);
store.setKeyEntry("rootPriKey", key.getPrivate(), null, chain);
store.store(new FileOutputStream(backPath), backPwd.toCharArray());
}
public static void RestoreRootCert(String certInfo) throws Exception {
Security.addProvider(new BouncyCastleProvider());
String[] splt = certInfo.split("//|");
String bakpath = splt[0];
String bakpwd = splt[1];
String yuanpem = splt[2];
String pempwd = splt[3];
String certPath = splt[4];
InputStream stream = new FileInputStream(bakpath);
KeyStore store = KeyStore.getInstance("PKCS12", "BC");
store.load(stream, bakpwd.toCharArray());
X509Certificate certificate = (X509Certificate) store
.getCertificate("root");
PrivateKey key = (PrivateKey) store.getKey("rootPriKey", null);
HongAnUtils.saveX509Certificate(certificate, certPath);
HongAnUtils.savePEM(key, pempwd, yuanpem);
}
// ca.CreateUserCert("中国|广东省|深圳市|一家|廖敏|111@163.com|3333333|365|1000000000000002|c://ca//廖敏.pem|user1pass|c://ca//于泳涛.cer|c://ca//HARoot.pem|HARootPwd|c://ca//HARoot.cer");
public static CertInfo CreateUserCert(String certInfo) throws Exception {
Security.addProvider(new BouncyCastleProvider());
String[] splt = certInfo.split("//|");
String country = splt[0];
String provice = splt[1];
String shi = splt[2];
String company = splt[3];
String userName = splt[4];
String email = splt[5];
String cellphone = splt[6];
String validateTime = splt[7];
String seriszeString = splt[8];
String userpemPath = splt[9];
String userpemPwd = splt[10];
String usercertpath = splt[11];
String rootpempath = splt[12];
String rootpempwd = splt[13];
String rootcertpath = splt[14];
X509Certificate CA = HongAnUtils.getCertificate(rootcertpath);
String certSubject = "C=" + country + ",S=" + provice + ",L=" + shi
+ ",O=" + company + ",Email=" + email
+ ",0.9.2342.19200300.100.1.41 = " + cellphone + ",CN="
+ userName;
KeyPair pair = HongAnUtils.generateRSAKeyPair();
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
certGen.setSerialNumber(new BigInteger(seriszeString));
certGen.setNotBefore(new Date(System.currentTimeMillis()));
certGen.setNotAfter(new Date(System.currentTimeMillis()
+ Long.parseLong(validateTime) * 24 * 60 * 60 * 1000L));
certGen.setSubjectDN(new X500Principal(certSubject));
certGen.setPublicKey(pair.getPublic());
certGen.setIssuerDN(CA.getIssuerX500Principal());
certGen.setSignatureAlgorithm("SHA1WithRSA");
X509Certificate certificate = certGen.generate(pair.getPrivate());
byte[] src = certificate.getEncoded();
KeyPair key = HongAnUtils.getPrivateKey(rootpempath, rootpempwd);
// byte[] b = HongAnUtils.RSASign(key.getPrivate(), src);
X509CertImpl newcert = new X509CertImpl(src);
X509CertInfo info = (X509CertInfo) newcert.get(newcert.getName() + "."
+ newcert.INFO);
X509CertImpl export = new X509CertImpl(info);
export.sign(key.getPrivate(), "SHA1WithRSA");
HongAnUtils.savePEM(pair.getPrivate(), userpemPwd, userpemPath);
DEROutputStream stream = new DEROutputStream(new FileOutputStream(
usercertpath));
stream.write(export.getEncoded());
stream.close();
CertInfo cert = new CertInfo();
cert.setCert(certificate);
cert.setKey(pair);
return cert;
}
// 将字符串生成密钥文件利用密码保护
// ("ABCDEFGHIJKLMNOPQRSTUVWXYZ123456|c://ca//companykey.dat|keypassword");
public static void CreateCompanyKey(String strArgs) throws Exception {
Security.addProvider(new BouncyCastleProvider());
String pwd = strArgs.split("//|")[0];
String savePathString = strArgs.split("//|")[1];
String baoPwd = strArgs.split("//|")[2];
char[] kouling = baoPwd.toCharArray();
PBEKeySpec spec = new PBEKeySpec(kouling);
SecretKeyFactory factory = SecretKeyFactory
.getInstance("PBEWithMD5AndDES");
SecretKey seKey = factory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
PBEParameterSpec pps = new PBEParameterSpec(new byte[] { 0xF, 0x0, 0x1,
0x2, 0x3, 0x4, 0x5, 0x6 }, 1);
cipher.init(Cipher.ENCRYPT_MODE, seKey, pps);
byte[] enbyte = cipher.doFinal(pwd.getBytes("utf-8"));
FileOutputStream stream = new FileOutputStream(savePathString);
stream.write(enbyte);
stream.close();
}
// 利用用户证书的公钥 给公司密钥加密生成用户密钥
// c://ca//companykey.dat|keypassword|c://ca//廖敏.cer|c://ca//廖敏.dat");
public static void ExportCompanyKeyByUserCert(String strArgs)
throws Exception {
Security.addProvider(new BouncyCastleProvider());
String[] splt = strArgs.split("//|");
String mimaPath = splt[0];
String mima = splt[1];
String rootCertPath = splt[2];
String usermimapath = splt[3];
FileInputStream input = new FileInputStream(mimaPath);
byte[] enbyte = new byte[input.available()];
input.read(enbyte);
input.close();
PBEKeySpec spec = new PBEKeySpec(mima.toCharArray());
SecretKeyFactory factory = SecretKeyFactory
.getInstance("PBEWithMD5AndDES");
SecretKey key = factory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
PBEParameterSpec pps = new PBEParameterSpec(new byte[] { 0xF, 0x0, 0x1,
0x2, 0x3, 0x4, 0x5, 0x6 }, 1);
cipher.init(Cipher.DECRYPT_MODE, key, pps);
byte[] debyte = cipher.doFinal(enbyte);
PublicKey pk = (PublicKey) HongAnUtils.getCertificate(rootCertPath)
.getPublicKey();
byte[] enbyteagain = HongAnUtils.encrypt(pk, debyte, "RSA");
HongAnUtils.write(usermimapath, enbyteagain);
}
}
实体类
package com.hongan.lh.cert;
import java.security.KeyPair;
import java.security.cert.X509Certificate;
public class CertInfo {
private KeyPair key;
private X509Certificate cert;
public KeyPair getKey() {
return key;
}
public void setKey(KeyPair key) {
this.key = key;
}
public X509Certificate getCert() {
return cert;
}
public void setCert(X509Certificate cert) {
this.cert = cert;
}
}
帮助类
package com.hongan.lh.cert;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.bouncycastle.openssl.PEMReader;
import org.bouncycastle.openssl.PEMWriter;
import org.bouncycastle.openssl.PasswordFinder;
public class HongAnUtils {
public static KeyPair getPrivateKey(String rootMiyuePath,
final String rootMiyuePwd) throws Exception {
PEMReader reader = new PEMReader(new InputStreamReader(
new FileInputStream(rootMiyuePath)), new PasswordFinder() {
public char[] getPassword() {
// TODO Auto-generated method stub
return rootMiyuePwd.toCharArray();
}
});
KeyPair key = (KeyPair) reader.readObject();
return key;
}
public static void saveX509Certificate(X509Certificate certificate,
String rootcertPath) throws Exception {
FileOutputStream stream = new FileOutputStream(rootcertPath);
stream.write(certificate.getEncoded());
stream.close();
}
public static void savePEM(PrivateKey key, String rootMiyuePwd,
String rootMiyuePath) throws Exception {
PEMWriter writer = new PEMWriter(new FileWriter(rootMiyuePath));
writer.writeObject(key, "DESEDE", rootMiyuePwd.toCharArray(),
new SecureRandom());
writer.close();
}
public static KeyPair generateRSAKeyPair() throws Exception {
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA");
kpGen.initialize(1024, new SecureRandom());
return kpGen.generateKeyPair();
}
public static byte[] RSASign(PrivateKey key, byte[] src) throws Exception {
java.security.Signature sign = java.security.Signature.getInstance(
"SHA1withRSA", "BC");
sign.initSign(key);
sign.update(src);
return sign.sign();
}
public static boolean RSAVerifySign(PublicKey key, byte[] crypt, byte[] src)
throws Exception {
java.security.Signature sign = java.security.Signature.getInstance(
"SHA1withRSA", "BC");
sign.initVerify(key);
sign.update(src);
return sign.verify(crypt);
}
public static X509Certificate getCertificate(String rootCertPath)
throws Exception {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
FileInputStream inputStream = new FileInputStream(rootCertPath);
X509Certificate certificate = (X509Certificate) factory
.generateCertificate(inputStream);
return certificate;
}
public static void write(String file, byte[] data) throws Exception {
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(data);
outputStream.close();
}
public static byte[] read(String file) throws Exception {
FileInputStream stream = new FileInputStream(file);
byte[] by = new byte[stream.available()];
stream.read(by);
stream.close();
return by;
}
public static byte[] encrypt(Key key, byte[] b, String suan)
throws Exception {
Cipher newcipher = Cipher.getInstance(suan);
newcipher.init(Cipher.ENCRYPT_MODE, key);
return newcipher.doFinal(b);
}
public static byte[] encryptByKouling(byte[] s, String suan, String kou)
throws Exception {
PBEKeySpec spec = new PBEKeySpec(kou.toCharArray());
SecretKeyFactory factory = SecretKeyFactory.getInstance(suan);
SecretKey key = factory.generateSecret(spec);
Cipher cipher = Cipher.getInstance(suan);
PBEParameterSpec pps = new PBEParameterSpec(new byte[] { 0xF, 0x0, 0x1,
0x2, 0x3, 0x4, 0x5, 0x6 }, 1);
cipher.init(Cipher.ENCRYPT_MODE, key, pps);
byte[] debyte = cipher.doFinal(s);
return debyte;
}
public static byte[] decryptByKouling(byte[] s, String suan, String kou)
throws Exception {
PBEKeySpec spec = new PBEKeySpec(kou.toCharArray());
SecretKeyFactory factory = SecretKeyFactory.getInstance(suan);
SecretKey key = factory.generateSecret(spec);
Cipher cipher = Cipher.getInstance(suan);
PBEParameterSpec pps = new PBEParameterSpec(new byte[] { 0xF, 0x0, 0x1,
0x2, 0x3, 0x4, 0x5, 0x6 }, 1);
cipher.init(Cipher.DECRYPT_MODE, key, pps);
byte[] debyte = cipher.doFinal(s);
return debyte;
}
}
证书操作类
package com.hongan.lh.cert;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Date;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.security.auth.x500.X500Principal;
import org.bouncycastle.asn1.DEROutputStream;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.x509.X509V1CertificateGenerator;
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CertInfo;
public class HongAnCA {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// CreateRootCert("中国|广东省|深圳市|公司A|3650|1000000000000001|HARootPwd|c://ca//HARoot.pem|c://ca//HARoot.cer");
// BackupRootCert("c://ca//honganbackup.p12|p12pass|c://ca//HARoot.pem|HARootPwd|c://ca//HARoot.cer");
// RestoreRootCert("c://ca//honganbackup.p12|p12pass|c://ca//honganroot_restore.pem|privkeypass|c://ca//honganroot_restore.cer");
// CreateUserCert("中国|广东省|深圳市|一家|廖敏|111@163.com|3333333|365|1000000000000002|c://ca//廖敏.pem|user1pass|c://ca//廖敏.cer|c://ca//HARoot.pem|HARootPwd|c://ca//HARoot.cer");
// ExportCompanyKeyByUserCert("c://ca//companykey.dat|keypassword|c://ca//廖敏.cer|c://ca//廖敏.dat");
// CreateCompanyKey("ABCDEFGHIJKLMNOPQRSTUVWXYZ123456|c://ca//companykey.dat|keypassword");
// ExportCompanyKeyByUserCert("c://ca//companykey.dat|keypassword|c://ca//廖敏.cer|c://ca//廖敏.dat");
}
public static byte[] CreateRootCert(String certInfo) throws Exception {
Security.addProvider(new BouncyCastleProvider());
String[] splt = certInfo.split("//|");
String country = splt[0];
String provice = splt[1];
String shi = splt[2];
String company = splt[3];
String validateTime = splt[4];
String seriszeString = splt[5];
String rootMiyuePwd = splt[6];
String rootMiyuePath = splt[7];
String rootcertPath = splt[8];
String certSubject = "C=" + country + ",S=" + provice + ",L=" + shi
+ ",O=" + company + ",CN=" + company;
KeyPair pair = HongAnUtils.generateRSAKeyPair();
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
certGen.setSerialNumber(new BigInteger(seriszeString));
certGen.setNotBefore(new Date(System.currentTimeMillis()));
certGen.setNotAfter(new Date(System.currentTimeMillis()
+ Long.parseLong(validateTime) * 24 * 60 * 60 * 1000L));
certGen.setSubjectDN(new X500Principal(certSubject));
certGen.setPublicKey(pair.getPublic());
certGen.setIssuerDN(new X500Principal(certSubject));
certGen.setSignatureAlgorithm("SHA1WithRSA");
X509Certificate certificate = certGen.generate(pair.getPrivate());
HongAnUtils.saveX509Certificate(certificate, rootcertPath);
HongAnUtils.savePEM(pair.getPrivate(), rootMiyuePwd, rootMiyuePath);
return certificate.getEncoded();
}
public static void BackupRootCert(String certInfo) throws Exception {
Security.addProvider(new BouncyCastleProvider());
String[] splt = certInfo.split("//|");
String backPath = splt[0];
String backPwd = splt[1];
String rootMiyuePath = splt[2];
final String rootMiyuePwd = splt[3];
String rootCertPath = splt[4];
CertificateFactory factory = CertificateFactory.getInstance("X.509");
FileInputStream inputStream = new FileInputStream(rootCertPath);
X509Certificate certificate = (X509Certificate) factory
.generateCertificate(inputStream);
KeyPair key = HongAnUtils.getPrivateKey(rootMiyuePath, rootMiyuePwd);
Certificate[] chain = new Certificate[1];
chain[0] = certificate;
KeyStore store = KeyStore.getInstance("PKCS12", "BC");
store.load(null, null);
store.setCertificateEntry("root", certificate);
store.setKeyEntry("rootPriKey", key.getPrivate(), null, chain);
store.store(new FileOutputStream(backPath), backPwd.toCharArray());
}
public static void RestoreRootCert(String certInfo) throws Exception {
Security.addProvider(new BouncyCastleProvider());
String[] splt = certInfo.split("//|");
String bakpath = splt[0];
String bakpwd = splt[1];
String yuanpem = splt[2];
String pempwd = splt[3];
String certPath = splt[4];
InputStream stream = new FileInputStream(bakpath);
KeyStore store = KeyStore.getInstance("PKCS12", "BC");
store.load(stream, bakpwd.toCharArray());
X509Certificate certificate = (X509Certificate) store
.getCertificate("root");
PrivateKey key = (PrivateKey) store.getKey("rootPriKey", null);
HongAnUtils.saveX509Certificate(certificate, certPath);
HongAnUtils.savePEM(key, pempwd, yuanpem);
}
// ca.CreateUserCert("中国|广东省|深圳市|一家|廖敏|111@163.com|3333333|365|1000000000000002|c://ca//廖敏.pem|user1pass|c://ca//于泳涛.cer|c://ca//HARoot.pem|HARootPwd|c://ca//HARoot.cer");
public static CertInfo CreateUserCert(String certInfo) throws Exception {
Security.addProvider(new BouncyCastleProvider());
String[] splt = certInfo.split("//|");
String country = splt[0];
String provice = splt[1];
String shi = splt[2];
String company = splt[3];
String userName = splt[4];
String email = splt[5];
String cellphone = splt[6];
String validateTime = splt[7];
String seriszeString = splt[8];
String userpemPath = splt[9];
String userpemPwd = splt[10];
String usercertpath = splt[11];
String rootpempath = splt[12];
String rootpempwd = splt[13];
String rootcertpath = splt[14];
X509Certificate CA = HongAnUtils.getCertificate(rootcertpath);
String certSubject = "C=" + country + ",S=" + provice + ",L=" + shi
+ ",O=" + company + ",Email=" + email
+ ",0.9.2342.19200300.100.1.41 = " + cellphone + ",CN="
+ userName;
KeyPair pair = HongAnUtils.generateRSAKeyPair();
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
certGen.setSerialNumber(new BigInteger(seriszeString));
certGen.setNotBefore(new Date(System.currentTimeMillis()));
certGen.setNotAfter(new Date(System.currentTimeMillis()
+ Long.parseLong(validateTime) * 24 * 60 * 60 * 1000L));
certGen.setSubjectDN(new X500Principal(certSubject));
certGen.setPublicKey(pair.getPublic());
certGen.setIssuerDN(CA.getIssuerX500Principal());
certGen.setSignatureAlgorithm("SHA1WithRSA");
X509Certificate certificate = certGen.generate(pair.getPrivate());
byte[] src = certificate.getEncoded();
KeyPair key = HongAnUtils.getPrivateKey(rootpempath, rootpempwd);
// byte[] b = HongAnUtils.RSASign(key.getPrivate(), src);
X509CertImpl newcert = new X509CertImpl(src);
X509CertInfo info = (X509CertInfo) newcert.get(newcert.getName() + "."
+ newcert.INFO);
X509CertImpl export = new X509CertImpl(info);
export.sign(key.getPrivate(), "SHA1WithRSA");
HongAnUtils.savePEM(pair.getPrivate(), userpemPwd, userpemPath);
DEROutputStream stream = new DEROutputStream(new FileOutputStream(
usercertpath));
stream.write(export.getEncoded());
stream.close();
CertInfo cert = new CertInfo();
cert.setCert(certificate);
cert.setKey(pair);
return cert;
}
// 将字符串生成密钥文件利用密码保护
// ("ABCDEFGHIJKLMNOPQRSTUVWXYZ123456|c://ca//companykey.dat|keypassword");
public static void CreateCompanyKey(String strArgs) throws Exception {
Security.addProvider(new BouncyCastleProvider());
String pwd = strArgs.split("//|")[0];
String savePathString = strArgs.split("//|")[1];
String baoPwd = strArgs.split("//|")[2];
char[] kouling = baoPwd.toCharArray();
PBEKeySpec spec = new PBEKeySpec(kouling);
SecretKeyFactory factory = SecretKeyFactory
.getInstance("PBEWithMD5AndDES");
SecretKey seKey = factory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
PBEParameterSpec pps = new PBEParameterSpec(new byte[] { 0xF, 0x0, 0x1,
0x2, 0x3, 0x4, 0x5, 0x6 }, 1);
cipher.init(Cipher.ENCRYPT_MODE, seKey, pps);
byte[] enbyte = cipher.doFinal(pwd.getBytes("utf-8"));
FileOutputStream stream = new FileOutputStream(savePathString);
stream.write(enbyte);
stream.close();
}
// 利用用户证书的公钥 给公司密钥加密生成用户密钥
// c://ca//companykey.dat|keypassword|c://ca//廖敏.cer|c://ca//廖敏.dat");
public static void ExportCompanyKeyByUserCert(String strArgs)
throws Exception {
Security.addProvider(new BouncyCastleProvider());
String[] splt = strArgs.split("//|");
String mimaPath = splt[0];
String mima = splt[1];
String rootCertPath = splt[2];
String usermimapath = splt[3];
FileInputStream input = new FileInputStream(mimaPath);
byte[] enbyte = new byte[input.available()];
input.read(enbyte);
input.close();
PBEKeySpec spec = new PBEKeySpec(mima.toCharArray());
SecretKeyFactory factory = SecretKeyFactory
.getInstance("PBEWithMD5AndDES");
SecretKey key = factory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
PBEParameterSpec pps = new PBEParameterSpec(new byte[] { 0xF, 0x0, 0x1,
0x2, 0x3, 0x4, 0x5, 0x6 }, 1);
cipher.init(Cipher.DECRYPT_MODE, key, pps);
byte[] debyte = cipher.doFinal(enbyte);
PublicKey pk = (PublicKey) HongAnUtils.getCertificate(rootCertPath)
.getPublicKey();
byte[] enbyteagain = HongAnUtils.encrypt(pk, debyte, "RSA");
HongAnUtils.write(usermimapath, enbyteagain);
}
}
发表评论
-
DecimalFormat的用法介绍
2012-02-06 14:16 2499import java.text.*; import ... -
tomcat,jdk,maven环境变量配置(备忘)
2011-12-25 10:24 1434环境变量: CATALINA_HOME D:\P ... -
java获得指定时间几天前或几天后的日期
2010-01-14 09:28 8876/** * 得到几天前的时间 * * ... -
动态读取配置文件
2009-08-04 11:30 2513import java.io.File; import ja ... -
JAVA对数字证书的常用操作
2009-07-12 18:42 1455一需要包含的包 import java.security.* ... -
签字证书例子
2009-07-12 18:31 1641import java.io.FileInputStr ... -
list,set,map,数组间的相互转换
2009-07-01 16:10 29281.list转set Set set = new HashSe ... -
动态运行Java代码
2009-07-01 14:08 1440有些情况下,我们不得不动态运行Java代码,以便提供更加灵活的 ... -
防止java反编译的一些常用方法
2009-02-12 14:33 21191.隔离Java程序 最 ... -
操作数组
2009-01-20 12:48 1271List list1 = new ArrayList() ... -
java写入文件时庄和薛字的问题
2008-12-26 10:47 1509今天碰到一个很有意思的问题,当写入文件是庄或薛的时候,就会出现 ... -
Hessian的学习笔记
2008-12-01 12:20 12531- 整个jar很小,200多K,3.1版本的,当然,我下载的 ... -
编写一个Java程序,对指定文本进行数字签名(文本随意)
2008-10-08 14:12 2882一:需要包含的包 import java.security. ... -
java的property配置文件的用法
2008-07-29 16:26 1772package configuration; ... -
统一异常处理
2008-07-16 16:47 1984package com.bjsxt.oa.manager; ... -
Java反射机制之代理模式
2008-07-02 23:12 1578代理模式的作用是:为其他对象提供一种代理以控制对这个对象的访问 ... -
java.lang.reflect.Array的应用
2008-07-02 23:11 1902java.lang.Array 类提供了动态创建和访问数组元 ... -
Java语言的反射机制
2008-07-02 23:10 1692在JAVA运行环境中,对 ... -
@Documented Annotation的使用
2008-07-02 23:09 2487@Documented Annotation的使用: @Do ... -
有关Annotation的继承
2008-07-02 23:09 4073有关Annotation的继承说明: 1、JDK文档中的说明是 ...
相关推荐
在代码签名中,开发人员会用到这种证书,将自己的公钥与软件绑定,当用户下载并运行该软件时,系统会验证这个签名,以确认软件的来源和完整性。 P12证书文件,也称为PKCS#12,是一种包含私钥和公钥的文件格式,用于...
总结来说,创建PKCS#12格式的数字签名证书涉及密钥对生成、X.509证书创建、PKCS#12存储构建和文件输出等步骤。BouncyCastle库提供了一套完整的API,使得在Java中实现这一过程变得相对简单。了解并熟练掌握这些步骤和...
我们使用微软自带的 VS 控制台命令工具进行我们自己的自定义证书创建,可以设置有效期限。 步骤如下: 1. 打开 Microsoft .NET Framework 的 SDK 命令提示,按以下步骤操作: 2. 创建一个自我签署的 X.509 证书...
3. 执行签名操作:在打包CAB文件之后,使用合适的工具(如signtool.exe)对CAB文件进行签名,用到的参数包括数字证书的路径和私钥。 总的来说,免费数字签名在保护信息安全、防止欺诈和恶意软件方面发挥着关键作用...
设备上的固件更新、远程管理、设备身份验证等场景都会用到数字证书。通过证书,可以防止恶意软件攻击、中间人攻击,保护设备不被未经授权的访问和控制。 此外,设备上的硬件安全模块(HSM)可以用于安全地存储和...
在Java环境中实现PKCS#7数字签名,常常会用到Bouncy Castle库。Bouncy Castle是一个广泛使用的开源加密库,提供了对多种加密算法的支持,包括PKCS#7标准。提供的两个jar文件"bcprov-jdk15-146.jar"和"bcmail-jdk15-...
这段代码展示了如何从一个包含证书的文件中读取数字证书。 ```java CertificateFactory cf = CertificateFactory.getInstance("X.509"); FileInputStream in = new FileInputStream("out.csr"); Certificate c = cf...
在PKCS#7签名中,原始数据和签名一起打包,提供了一种方式来验证数据未被篡改,并确认发送者的身份。 BouncyCastle是一个开源的密码学库,它支持多种加密算法和标准,包括PKCS#7。在C#环境中,BouncyCastle提供了...
在C++中实现这些算法,开发者可能使用标准库(如 `<openssl>`)或者自定义实现。项目可能包括以下部分: 1. **散列函数的实现**:将原始数据转化为散列值,并检查散列值的匹配性。 2. **非对称加密的实现**:生成...
应用程序签名是指在软件发布前,使用特定的数字证书对其代码进行签名的过程。这个过程由可信的签名者执行,通常是一个证书颁发机构(CA)。签名的目的在于确保软件未经修改,并向用户表明开发者身份。当用户下载或...
- **数字签名的意义**:数字签名在软件开发中尤为重要,尤其是对于那些需要访问客户端本地资源的应用(如Applet或Java Web Start程序)。通过数字签名,开发者可以提高程序的安全性和可信度,使得程序能够合法地访问...
3. 执行签名:运行签名命令,将数字证书应用到EXE文件上,生成数字签名。 4. 验证签名:用户或系统可以通过检查数字签名来验证文件的完整性和发行者的真实性。 总的来说,这个“数字签名添加工具”简化了为EXE文件...
在"php生成PDF电子合同签名"这个主题中,我们主要关注的是如何利用PHP技术来创建PDF文档,并在其中实现电子签名功能,这在现代商业环境中具有很高的实用价值。以下是对这一主题的详细阐述: 首先,我们需要理解PDF...
2. **makecert.exe**: 这是一个证书创建工具,用于生成自签名的证书或者请求证书颁发机构(CA)的证书。这对于测试环境和开发过程中的签名非常有用。 3. **signcode.exe**: 这个工具与signtool类似,用于对代码进行...
虽然不是直接与证书和签名相关的工具,setreg.exe用于设置注册表项,这在配置签名策略或管理相关证书设置时可能会用到。 9. **chktrust.exe**: 这是一个检查信任状态的工具,用于验证数字签名的有效性,帮助...
在JRE8中,包含了一个名为`keytool`的命令行工具,它能够帮助我们创建、管理和查看数字证书。 **步骤一:打开命令提示符** 在Windows操作系统中,我们可以通过搜索“cmd”或“命令提示符”来启动它。这将是执行`...
这将涉及到创建 CA、生成私钥和自签名证书、颁发数字证书、吊销证书、生成证书吊销列表文件(CRL)等步骤。 标签解释 软件开发是指使用计算机语言和工具来设计、开发、测试和维护软件系统的过程。在这个过程中,...
1. **已创建应用程序的签名证书**:签名证书是用于对应用进行数字签名的关键文件,它包含了公钥等信息,用于验证应用的身份。因此,在生成签名证书指纹之前,必须首先拥有该证书。 2. **当前PC已经安装JDK**:生成...
Windows CA(证书颁发机构)是微软提供的一种服务,用于创建、管理和分发数字证书。本文将深入探讨Windows CA证书服务器的配置过程,并详细阐述OCX认证签名的步骤。 ### 一、Windows CA 证书服务器配置 #### 1. ...
每个Android应用在安装到设备上之前都必须有一个数字证书进行签名。这个证书可以用来确认应用的开发者,并且在应用更新时确保新版本是由同一开发者发布的。签名还有助于防止恶意代码篡改,因为一旦应用被修改,签名...