Python中RSA的PKCS#1、PKCS#8,MD5加密

Python中RSA的PKCS#1、PKCS#8,MD5加密

一、Python-RSA

RSA库只支持PKCS#1的密钥格式需要安装第三方库rsa
pip install rsa

python-rsa官方地址:https://stuvel.eu/python-rsa-doc/

RSA非对称加密:1、公钥进行加密(公开)
rsa.encrypt(message, pub_key)
2、私钥进行解密(保密)
rsa.decrypt(crypto, priv_key)
例:
import base64import rsaclass RsaDemo:    def __init__(self):        self.pubkey, self.privkey = rsa.newkeys(512)   #生成公钥、私钥对象,密钥位数512    def encrypt_str(self,test_str):        """        加密        :param test_str: 需要进行加密的字符串        :return: 返回加密后的str        """        new_str = test_str.encode("utf8")   #字符串转为utf8字节码        crypt_str = rsa.encrypt(message=new_str, pub_key=self.pubkey)   #加密,之后数据类型为byte        b64_str = base64.b64encode(crypt_str)  # base64编码,格式为byte        result = b64_str.decode()    # 转为字符串        print(type(result),result)        return result    def decrypt_str(self,crypt_str:str):        """        解密        :param crypt_str:        :return:        """        byte_str = crypt_str.encode()       #字符串编码为byte        test_str = base64.b64decode(byte_str)          #base64解码        byte_result = rsa.decrypt(crypto=test_str,priv_key=self.privkey)    #解密        str_result = byte_result.decode()              #解码为字符串        print(type(str_result), str_result)if __name__ == '__main__':    test = RsaDemo()    phone = "15200000001"    crypt_phone = test.encrypt_str(phone)    decrypt_phone = test.decrypt_str(crypt_phone)

  

二、pyhton的Crypto

支持PKCS#1、PKCS#8等密钥格式1、windows下的安装
pip install pycryptodome

2、使用

import base64from Crypto.PublicKey import RSAfrom Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher#公钥public_key = """-----BEGIN PUBLIC KEY-----********************************-----END PUBLIC KEY-----"""#私钥private_key ="""-----BEGIN RSA PRIVATE KEY-----********************************-----END RSA PRIVATE KEY-----"""message = "qq123456"#加密pub_key = RSA.importKey(public_key)  cipher = PKCS1_cipher.new(pub_key)rsa_text = base64.b64encode(cipher.encrypt(message.encode("utf-8)")))     #加密并转为b64编码text = rsa_text.decode("utf8")       #解码为字符串print("加密后的内容:",text)             # 解密pri_Key = RSA.importKey(private_key)cipher = PKCS1_cipher.new(pri_Key)back_text = cipher.decrypt(base64.b64decode(text.encode("utf8")), 0)print("解密后的内容:",back_text.decode("utf-8"))

  

三、MD5

import hashlibwork_no = "00865"work_no = work_no.encode("utf8")  #转为byte#md5_no = hashlib.md5(work_no)       #编码为md5对象#md5_no = md5_no.hexdigest()        #转为16进制字符串result = hashlib.md5(work_no).hexdigest()print(result)

  

备注:关于PKCS#8和PKCS#1证书格式之间,可进行转换 参考资料:https://www.jianshu.com/p/ada97fd7f8f6https://wenku.baidu.com/view/f3b082072c60ddccda38376baf1ffc4fff47e257.htmlhttps://blog.csdn.net/weixin_42856871/article/details/116268046https://blog.csdn.net/weixin_43824829/article/details/124045403https://blog.csdn.net/viviliving/article/details/113575418

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部