发布于2019-08-07 10:25 阅读(1226) 评论(0) 点赞(1) 收藏(5)
0x00 前言
学校网络安全实验,虽然我对密码学真的不感冒,但是我们学院就是这么特别,网络工程也要学密码学,而且是几个学期反复教相同的东西,没错,就是你(XD university)。原理部分就下次再写了。
0x01 实验要求
实验要求1
给定素数p和q,公钥e,计算d
p=11; q=13; e=11; d=??
p=17; q=11; e=7; d=??
p=5; q=11; e=3; d=??
实验要求2
根据自己计算出的公私钥对,以及p, q,设计程序,实现RSA的加密和解密
加密:输入明文、密钥,输出密文
解密:输入密文、密钥,输出明文
实验要求3
根据自己计算出的公私钥对,以及p, q,设计程序,实现RSA的签名和验证
签名:输入消息、密钥,输出签名消息
验证:输入签名消息、密钥,输出原文
0x02 代码与实现
1、计算d
- def find_d(e, s):
- for d in range(s):
- x = (e * d) % s
- if x == 1:
- return d
-
- if __name__ == '__main__':
- p = input("what's your p ?")
- q = input("what's your q ?")
- e = int(input("what's your e ?"))
- s = (int(p)-1) * (int(q)-1)
- print(s)
- print(find_d(e, s))
2、加密与解密
- import rsa
-
-
- def str2_ascii_int(string1):
- xx = ""
- for i in string1:
- xx = xx + hex(ord(i))[2:]
- return int(xx, 16)
-
-
- def ascii_int2str(num1):
- xx = hex(num1)[2:]
- i = 0
- sign_name = ""
- while i < len(xx) / 2:
- current_str = xx[2 * i:2 * i + 2]
- real = chr(int(current_str, 16))
- sign_name = sign_name + real
- i = i + 1
- return sign_name
-
-
- def encryption(plaintext, e, n):
- cryptotext = pow(plaintext, e) % n
- return cryptotext
-
-
- def decryption(cryptotext, d, n):
- plaintext = pow(cryptotext, d) % n
- return plaintext
-
-
- # this part is mainly for signature, in the part, the public key will be used for decryption and the private key will be
- # used for encryption, but why? Because the public key is open for everyone, and the private key is just for the owner.
- # if you want anyone else to recognize you as xxx, you should offer the other one a signature ,because the signature is
- # encrypted by private key, so no one else can get the same signature expect the loss of the private key.
- if __name__ == '__main__':
- key = rsa.newkeys(32)
- n = key[1].n
- e = key[1].e
- d = key[1].d
- plaintext = input("what's your plaintext ?")
- print("the plaintext is : ", plaintext)
- cryototext = encryption(str2_ascii_int(plaintext), e, n) # 用私钥加密,签名
- print("the cryptotext is : ", cryototext)
- recognize_name = ascii_int2str(decryption(cryototext, d, n)) # 用公钥解密,验证
- print(str(recognize_name))
3、签名与签名验证(我实在想吐槽这个,老师你就不能让我用hash吗,为什么要让我输出原文!!!!
注意:p*q 的值必须大于message的值,否则会出错,也就是说如果你用ascii来做的话,p=17, q = 11 那么message的值必须小于187,所以我这里就只选了一个字母来做验证)
- import rsa
-
-
- def str2_ascii_int(string1):
- xx = ""
- for i in string1:
- xx = xx + hex(ord(i))[2:]
- return int(xx, 16)
-
-
- def ascii_int2str(num1):
- xx = hex(num1)[2:]
- i = 0
- sign_name = ""
- while i < len(xx) / 2:
- current_str = xx[2 * i:2 * i + 2]
- real = chr(int(current_str, 16))
- sign_name = sign_name + real
- i = i + 1
- return sign_name
-
-
- def encryption(plaintext, e, n):
- cryptotext = pow(plaintext, e) % n
- return cryptotext
-
-
- def decryption(cryptotext, d, n):
- plaintext = pow(cryptotext, d) % n
- return plaintext
-
-
- # this part is mainly for signature, in the part, the public key will be used for decryption and the private key will be
- # used for encryption, but why? Because the public key is open for everyone, and the private key is just for the owner.
- # if you want anyone else to recognize you as xxx, you should offer the other one a signature ,because the signature is
- # encrypted by private key, so no one else can get the same signature expect the loss of the private key.
- if __name__ == '__main__':
- key = rsa.newkeys(16) # 这里的生成位数这么低是为了方便我自己计算,作为验证,实际使用请不要这么小
- n = key[1].n
- e = key[1].e
- d = key[1].d
- sig_name = input("what's your name ?")
- print("your name is : ", sig_name)
- signature = encryption(str2_ascii_int(sig_name), d, n) # 用私钥加密,签名
- print("the signature is : ", signature)
- recognize_name = ascii_int2str(decryption(signature, e, n)) # 用公钥解密,验证
- print(str(recognize_name))
0x03 注意事项
其实这个实验真的很简单,理论部分你不懂也能做,但是这个实验最恶心的就是python中各种16进制,10进制字符串,消息字符串之间的各种转变,我花时间最多的反而是这个,加解密和签名基本就十几分钟就写好了,当然也是因为这次实验本身不带有密钥生成部分的内容,否则还是要花一些时间的。
0x04 后记与总结
密码学毫无疑问是安全通信中基础中的基础,也是重中之重,然而如何生成安全密钥,管理密钥和防止密钥泄露才是一个工作人员应该注意的点之一。之后有时间会更新关于密钥生成以及密钥管理中的相关内容,有写得不对的地方还请指出。
作者:遥远的她
链接:https://www.pythonheidong.com/blog/article/9818/684f9616047c9a9da760/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!