20175122邱昕个人课程设计报告

北京电子科技学院 《信息安全工程技术应用》课程设计报告

 

基于OpenSSL的证书格式转换工具设计与实现    

 

 

题目 基于openssl的证书格式转换工具设计与实现

小组成员姓名:20175119郑楚琪   20175122邱昕 20175129陈尧

指导教师:娄嘉鹏

提交时间:2020年5月4日

 

 

 

一、个人任务:

 

  1. 学习各种证书格式,学习不同格式的特点差别,如何用openssl查看,如何用openssl转换为其他格式。
  2. windows环境下使用openssl生成一个pem格式的证书,并转化为各种其他格式。实现各个格式之间的互相转换
  3. 负责编程实现一个窗口来实现证书格式转换

 

 

 

二、学习成果:

 

1、常见数字证书格式:

 

1)、PEM Format

 

最常用的证书格式(密钥格式)

 

用于Apache平台

 

ASCII文件使用Base64编码

 

文件中包含 “—–BEGIN CERTIFICATE—–” 和 “—–END CERTIFICATE—–” 字符串

 

只包含公钥

 

可使用openssl x509 -in cert.pem -noout -text查看PEM证书内容

 

常用扩展名 .pem, .crt, .cer, .key

 

2)、ASCII PEM 格式证书的二进制版本

 

用于java平台

 

查看DER格式证书的信息openssl x509 -in cert.der -inform der -text -noout

 

扩展名 .der

 

3)、crt:有可能用der,也有可能用pem编码

 

4)、jksJava Key StorageJAVA的专属格式,利用keytool可以进行格式转换。一般用于 Tomcat 服务器。

 

5)、PFX P12公钥加密标准 #12 (PKCS#12) 可包含所有私钥、公钥和证书。其以二进制格式存储,也称为 PFX 文件。通常可以将Apache/OpenSSL使用的“KEY文件 + CRT文件格式合并转换为标准的PFX文件。转换时需要输入PFX文件的加密密码。

 

2、安装openssl:

 

下载openssl安装包并安装,在windows的环境变量中的path中加入opensslbin路径

 

 

 

3openssl转化格式命令:

 

PEM——>DERopenssl x509 -outform der -in certificate.pem -out certificate.der

 

PEM——>P7Bopenssl crl2pkcs7 -nocrl -certfile certificate.cer -out certificate.p7b -certfile CAcert.cer

 

PEM——>PFXopenssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CAcert.crt

 

PEM——>keystorekeytool -import -file ca.cer -keystore ca.keystore

 

DER——>PEMopenssl x509 -inform der -in certificate.cer -out certificate.pem

 

P7B——>PEMopenssl pkcs7 -print_certs -in certificate.p7b -out certificate.cer

 

PFX——>PEMopenssl pkcs12 -in certificate.pfx -out certificate.cer -nodes

 

 

 

4、代码:

 

import os
import sys
import tkinter  # 使用Tkinter前需要先导入
from datetime import datetime
from tkinter import filedialog
from tkinter import messagebox


def set_win_center(root, curWidth=200, curHight=200):
    '''
    设置窗口大小,并居中显示
    :param root:主窗体实例
    :param curWidth:窗口宽度,非必填,默认200
    :param curHight:窗口高度,非必填,默认200
    :return:
    '''
    if not curWidth:
        '''获取窗口宽度,默认200'''
        curWidth = root.winfo_width()
    if not curHight:
        '''获取窗口高度,默认200'''
        curHight = root.winfo_height()
    # print(curWidth, curHight)

    # 获取屏幕宽度和高度
    scn_w, scn_h = root.maxsize()
    # print(scn_w, scn_h)

    # 计算中心坐标
    cen_x = (scn_w - curWidth) / 2
    cen_y = (scn_h - curHight) / 2
    # print(cen_x, cen_y)

    # 设置窗口初始大小和位置
    size_xy = '%dx%d+%d+%d' % (curWidth, curHight, cen_x, cen_y)
    root.geometry(size_xy)


def pem_2_der():
    os.system("openssl x509 -outform der -in " + w1.get() + " -out " + w2.get())
    tkinter.messagebox.showinfo(title='提示', message='转换成功!')

def pem_2_p7b():
    os.system("openssl crl2pkcs7 -nocrl -certfile " + w1.get() + " -out " + w2.get())
    tkinter.messagebox.showinfo(title='提示', message='转换成功!')

def pem_2_pfx():
    os.system("echo 20175122|openssl pkcs12 -export -in " + w1.get() + "-inkey " +e1.get()+" -out " + w2.get())
    tkinter.messagebox.showinfo(title='提示', message='转换成功!')

def pem_2_keystore():
    os.system("keytool -import -file " + w1.get() + "-keystore " + w2.get() )
    tkinter.messagebox.showinfo(title='提示', message='转换成功!')

def der_2_pem():
    os.system("openssl x509 -inform der -in " + w1.get() + " -out " + w2.get())
    tkinter.messagebox.showinfo(title='提示', message='转换成功!')

def p7b_2_pem():
    os.system("openssl pkcs7 -print_certs -in " + w1.get() + " -out " + w2.get())
    tkinter.messagebox.showinfo(title='提示', message='转换成功!')

def pfx_2_pem():
    os.system("openssl pkcs12 -in " + w1.get() + " -out " + w2.get() + " -nodes ;" + e2.get() + ";")
    tkinter.messagebox.showinfo(title='提示', message='转换成功!')

def choose_file():
    """
    选择需要处理的文件
    :return:
    """
    """打开选择文件夹对话框"""
    filepath = filedialog.askopenfilename(title=u'选择被检测文件', initialdir=(os.path.abspath("..")))  # 获得选择好的文件
    w1.set(filepath)
    return filepath


def save_file():
    path = filedialog.asksaveasfilename(title=u'保存文件',
                                        initialfile="{}.".format(
                                            datetime.strftime(datetime.now(), "%Y-%m-%d %H-%M-%S")))  # 保存文件路径对话框
    w2.set(path)
    return path


window = tkinter.Tk()


# 2步,给窗口的可视化起名字
window.title('openssl证书格式转换工具')
# 3步,设定窗口的大小( * )
set_win_center(window, 720, 550)

w1 = tkinter.StringVar()
w1.set('输入文件路径')
w2 = tkinter.StringVar()
w2.set('输出文件路径')
tkinter.Entry(window, show=None, font=('楷体', 14), textvariable=w1).place(x=80, y=48, anchor='nw',
                                                                         width=500,
                                                                         height=35)
tkinter.Entry(window, show=None, font=('楷体', 14), textvariable=w2).place(x=80, y=98, anchor='nw',
                                                                         width=500,
                                                                         height=35)
tkinter.Button(window, text='选择文件', font=('宋体', 14), command=choose_file).place(x=600, y=50, anchor='nw')
tkinter.Button(window, text='保存位置', font=('宋体', 14), command=save_file).place(x=600, y=100, anchor='nw')

tkinter.Button(window, text='pemder', font=('宋体', 14), command=pem_2_der).place(x=80, y=200, anchor='nw')
tkinter.Button(window, text='pemp7b', font=('宋体', 14), command=pem_2_p7b).place(x=190, y=200, anchor='nw')
tkinter.Button(window, text='pempfx', font=('宋体', 14), command=pem_2_pfx).place(x=290, y=200, anchor='nw')
tkinter.Button(window, text='pemkeystore', font=('宋体', 14), command=pem_2_keystore).place(x=390, y=200, anchor='nw')
tkinter.Button(window, text='derpem', font=('宋体', 14), command=der_2_pem).place(x=80, y=250, anchor='nw')
tkinter.Button(window, text='p7bpem', font=('宋体', 14), command=p7b_2_pem).place(x=190, y=250, anchor='nw')
tkinter.Button(window, text='pfxpem', font=('宋体', 14), command=pfx_2_pem).place(x=290, y=250, anchor='nw')
e1 = tkinter.Entry(window, show='*', font=('Arial', 14))   # 显示成密文形式
e2 = tkinter.Entry(window, show=None, font=('Arial', 14))  # 显示成明文形式
e1.place(x=80,y=300)
e2.place(x=80,y=350)

l = tkinter.Label(window, text='保存位置请自己加上文件名后缀', font=('Arial', 12), width=30, height=2)
# 说明: bg为背景,font为字体,width为长,height为高,这里的长和高是字符的长和高,比如height=2,就是标签有2个字符这么高

# 5步,放置标签
l.place(x=80, y=150, anchor='nw') # Label内容content区域放置位置,自动调节尺寸
# 放置lable的方法有:1l.pack(); 2)l.place();

# 主窗口循环显示
window.mainloop()

 

 

 

5、参考资料

 

https://blog.csdn.net/freeking101/article/details/94625055

 

https://blog.csdn.net/weixin_33739541/article/details/93964322?utm_source=app

 

https://blog.csdn.net/bbwangj/article/details/82503675

 

https://blog.csdn.net/junbopengpeng/article/details/19161077

 

https://blog.csdn.net/MarsLee_U/article/details/86491759

 

 

 

 

 

三、遇到的困难及解决办法:

 

python的学习和编写代码,上网学习,菜鸟教程,csdn和博客园上的各种博客学习

 

Openssl的安装和环境配置,上网学,安装配置

 

Python的安装以及pycharm的安装,按网上教程学习安装

 

Python调用pem转pfx或pem转keystore或pfx转pem时需要输入密码确认密码,这个我不知道如何在python的tkinter上交互

 

 

 

四、体会与收获

 

我一开始对这个课设是比较陌生的,我不同于其他同学,pki课程我没有选上学习,这个课设对我来说就是全新的东西,我同班同学给了我一定支持,他们有学习pki,给了我一些有关openssl的资料,另外关于如何实现窗口也比较头疼,因为我没学过java。在大约一个月的学习中,我对openssl越发熟悉,也选定了使用python3语言利用tkinter来实现窗口,过程比较曲折吧,不过python是比较简单的,实现代码时还是相对来说比较顺利的。

 

编程时主要问题是没学过python,在学习网上教程,学习部分例子,参考博客后摸索着编程吧

 

感谢娄老师悉心的教导和教育,令我在这三周的实践中收获颇丰。通过查阅资料我的分析能力和代码编程能力又有了一定的进步。

 

 

 

 

 

posted @ 2020-05-04 22:31  20175122邱昕  阅读(221)  评论(0编辑  收藏  举报