Python中RSA的PKCS#1、PKCS#8,MD5加密
一、Python-RSA
pip install rsa
python-rsa官方地址:https://stuvel.eu/python-rsa-doc/
rsa.encrypt(message, pub_key)
rsa.decrypt(crypto, priv_key)
import base64
import rsa class 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 = "13300000001"
crypt_phone = test.encrypt_str(phone)
decrypt_phone = test.decrypt_str(crypt_phone)
二、pyhton的Crypto
pip install pycryptodome
2、使用
import base64
from Crypto.PublicKey import RSA
from 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 = "123456" #加密
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 hashlib
no = "0001"
no = no.encode("utf8") #转为byte #md5_no = hashlib.md5(no) #编码为md5对象
#md5_no = md5_no.hexdigest() #转为16进制字符串 result = hashlib.md5(no).hexdigest()
print(result)
Python中RSA的PKCS#1、PKCS#8,MD5加密的更多相关文章
- iOS,一行代码进行RSA、DES 、AES、MD5加密、解密
本文为投稿文章,作者:Flying_Einstein(简书) 加密的Demo,欢迎下载 JAVA端的加密解密,读者可以看我同事的这篇文章:http://www.jianshu.com/p/98569e ...
- 使用RSA加密在Python中逆向shell
i春秋翻译小组-Neo(李皓伟) 使用RSA加密在Python中逆向shell 这是一个关于使用RSA加密编程逆向shell的python教程. 我想提一下,这篇文章更多的是关于理解shell中涉及的 ...
- RSA私钥和公钥文件格式 (pkcs#1, pkcs#8, pkcs#12, pem)
RSA私钥和公钥文件格式 (pkcs#1, pkcs#8, pkcs#12, pem) 2018年03月07日 11:57:22 阅读数:674 Format Name Description PKC ...
- OpenSSL和Python实现RSA Key公钥加密私钥解密
基于非对称算法的RSA Key主要有两个用途,数字签名和验证(私钥签名,公钥验证),以及非对称加解密(公钥加密,私钥解密).本文提供一个基于OpenSSL和Python进行非对称加解密的例子. 1. ...
- OPENSSL中RSA私钥文件(PEM格式)解析【一】
http://blog.sina.com.cn/s/blog_4fcd1ea30100yh4s.html 在PKCS#1 RSA算法标准中定义RSA私钥语法为: RSAPrivateKey ::= S ...
- OpenSSL 中 RSA 加密解密实现源代码分析
1.RSA 公钥和私钥的组成.以及加密和解密的公式: 2.模指数运算: 先做指数运算,再做模运算.如 5^3 mod 7 = 125 mod 7 = 6 3.RSA加密算法流程: 选择一对不同的.而且 ...
- Python的RSA加密和PBE加密
最近在写接口的时候,遇到了需要使用RSA加密和PBE加密的情况,对方公司提供的DEMO都是JAVA的,我需要用python来实现. 在网上搜了一下,python的RSA加密这块写的还是比较多的,但是P ...
- Python3 实现 JS 中 RSA 加密的 NoPadding 模式
前因后果之哗啦啦废话连篇: 这几天本人在 Python 做某网站登陆的时候,发现其登陆时用户名和密码被加密了 F12 仔细看了一下,发现是调用了一个 js 的 rsa 加密库,页面 dom 中有 rs ...
- python的rsa公钥解密方法
示例: # -*- coding: UTF- -*- import M2Crypto import base64 #私钥加密,公钥解密 def pri_encrypt(msg, file_name): ...
- linux命令(28):Linux下SCP无需输入密码传输文件,python 中scp文件
python 中scp文件:(如果下面的发送免密码已经完成的话,就直接能用下面这个) os.system('scp "%s" "%s:%s"' % (" ...
随机推荐
- 查看process状态
1.查看进程Uid.Gid bash-4.4# cat /proc/1/status | grep Uid Uid: 0 0 0 0 bash-4.4# cat /proc/1/status | gr ...
- php json_encode 斜杠 反斜杠 转义处理
$data = str_replace("\\\\n", "\\n", \jsonEncode($data)); // \\n转为\n $data = str_ ...
- 查看Windows操作系统编码方式
chcp 编码表: 代码页 国家(地区)或语言 437 美国 708 阿拉伯文(ASMO 708)720 阿拉伯文(DOS)850 多语言(拉丁文 I) 852 中欧(DOS) - 斯拉夫语(拉丁文 ...
- 编译内核出现错误cc1: error: code model kernel does not support PIC mode
删除该模块目录下的 .cache.mk 文件就好了,重新 make 即可
- [MicroPython ESP32] 内存分析
[MicroPython ESP32] 内存分析 [(1)芯片:ESP32-WROOM-DA] 手册: https://www.espressif.com.cn/zh-hans/support/doc ...
- Linux基本概念
目录 1. 内核.内核态和用户态 2. 用户和组 3. 文件和文件系统 4. I/O模型 5. 程序.进程.线程和协程 6. shell.终端和会话 1. 内核.内核态和用户态 内核是指管理和分配 ...
- 想通过anconda来创建一个虚拟环境,结果发现一直报错。An unexpected error has occurred. Conda has prepared the above report.
本来想要通过conda create -n drf-admin python==3.8 来创建一个虚拟环境,结果一直报错. An unexpected error has occurred. Cond ...
- npm run serve 报错问题 (npm ERR! code ELIFECYCLE)
运行 npm cache clean --force删除 node_modules删除 package-lock.json运行 npm install最后 npm run serve
- C#基础篇【类型转换原则】
C#可以提供兼容类型的转换,转换始终会根据一个存在的值创建另一个新的值,转换分为两种,显式转换 隐式转换 隐式转换:自动发生 显示转换:需要添加强制转换 在一下案列中 我们把Int 32隐式转换为Do ...
- VUE中如何使用MOCK
安装mockjs npm install mockjs 可以使用数据模板生成模拟数据. Mock.mock( rurl?, rtype?, template ) ) // 或者 Mock.mock( ...