用netty造一个简易的fiddler ( 一 生成证书 )

2018-07-22 05:48:16来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

  本人曾在上一家公司,搞过半年的爬虫.因此认识了这个叫 fiddler 的工具.当然市面上还有 charles ,开源也有 anyproxy  whistle.到最后我还是喜欢用fiddler.似乎有点念旧.而现在这家公司是做erp的.项目用到了netty.

  因此才有这个念头.造一个简单的轮子-简易的fiddler

  Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据。摘自百度百科.

  用过上面的工具都知道,捕获https请求都要安装证书的.本篇内容:生成证书

   网上很多都是使用openssl生成证书的.其实用java完全可以生成的.

<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.59</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

 

Security.addProvider(new BouncyCastleProvider());
KeyPairGenerator keyPairGenerator
=KeyPairGenerator.getInstance("RSA", "BC"); keyPairGenerator.initialize(2048, new SecureRandom()); KeyPair keyPair = keyPairGenerator.genKeyPair();

X500NameBuilder builder = new X500NameBuilder(BCStyle.INSTANCE).addRDN(BCStyle.C, "C");
builder.addRDN(BCStyle.L, "L").addRDN(BCStyle.O, "O").addRDN(BCStyle.ST, "ST");
X500Name x500Name=builder.addRDN(BCStyle.OU, "OU").addRDN(BCStyle.CN, "CN").build();

Date notBefore = new Date(System.currentTimeMillis());
Date notAfter
= new Date(System.currentTimeMillis() + 730 * 48 * 3600000L);//two year
X509v3CertificateBuilder xcBuilder = new JcaX509v3CertificateBuilder(
x500Name, BigInteger.valueOf(1), notBefore, notAfter, x500Name, keyPair.getPublic());
xcBuilder.addExtension(Extension.basicConstraints, false, new BasicConstraints(true));
xcBuilder.addExtension(Extension.subjectKeyIdentifier, false, new JcaX509ExtensionUtils().createSubjectKeyIdentifier(keyPair.getPublic()));
xcBuilder.addExtension(Extension.authorityKeyIdentifier, false, new JcaX509ExtensionUtils().createAuthorityKeyIdentifier(keyPair.getPublic()));

ContentSigner signer = new JcaContentSignerBuilder("SHA256WithRSAEncryption").setProvider("BC").build(keyPair.getPrivate());
X509Certificate certificate = new JcaX509CertificateConverter().setProvider("BC").getCertificate(xcBuilder.build(signer));
String caPath="d:\\ca.crt";
String privateKeyPath="d:\\private.der";
saveX509Certificate(certificate.getEncoded(), caPath);
savePrivateKey(new File(privateKeyPath), keyPair.getPrivate(),null);
 
 

 

 public static void saveX509Certificate(byte[] content, String caPath) throws IOException, URISyntaxException {

        PemWriter pemWriter = null;
        try {
            pemWriter = new PemWriter(new OutputStreamWriter(new FileOutputStream(new File(caPath))));
            pemWriter.writeObject(new PemObject("CERTIFICATE", content));
        } finally {
            try {
                if (pemWriter != null) {
                    pemWriter.close();
                }
            } catch (IOException ignore) {

            }
        }

}
public static void savePrivateKey(File file, PrivateKey privateKey, OutputEncryptor encryptor) throws IOException {
    JcaPKCS8Generator jcaPKCS8Generator = new JcaPKCS8Generator(privateKey, encryptor);

StringWriter stringWriter = new StringWriter();
try (JcaPEMWriter pw = new JcaPEMWriter(stringWriter)) {
pw.writeObject(jcaPKCS8Generator.generate());
}

FileUtils.writeByteArrayToFile(file, stringWriter.toString().getBytes());

}
 

  运行上面代码都产生2个文件(ca.crt 是根证书 , private.der 是私钥).双击ca.crt,再点详细信息,就能看到下图右边那块了.下图是解释上面代码

   至此,我们已经生成好了ca 证书和私钥,下面是windows安装图解

 

  ps.2-3 步之间要点击下一步才行的.需要说明的是 openssl 和jdk 自带的 keytool 也是可以生成证书的.再次强调证书是要花钱买的.当然也有免费的

    本篇内容完结.下一篇用 netty 造 一个 中间人

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:java 三大框架 hibernate部分知识实现增删该查操作

下一篇:Spring MVC 注解基础(一)