rsa加密算法python实现
时间: 2023-06-04 21:08:50 浏览: 182
RSA加密算法的Python实现可以使用PyCryptodome库。下面是一个示例代码:
```python
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
# 生成RSA密钥
key = RSA.generate(2048)
# 获取公钥和私钥
public_key = key.publickey().export_key()
private_key = key.export_key()
# 使用公钥加密数据
cipher = PKCS1_OAEP.new(RSA.import_key(public_key))
encrypted_data = cipher.encrypt(b'hello world')
# 使用私钥解密数据
cipher = PKCS1_OAEP.new(RSA.import_key(private_key))
decrypted_data = cipher.decrypt(encrypted_data)
print(decrypted_data.decode())
```
注意,使用RSA加密算法需要非常小心,因为错误的使用可能会导致数据泄漏等问题。在实际应用中请务必慎重考虑。
相关问题
RSA加密算法python实现
RSA是一种非对称加密算法,由Ron Rivest、Adi Shamir和Leonard Adleman于1977年发明。Python中可以使用内置库`cryptography`或第三方库`pycryptodome`来实现RSA。以下是基本的RSA加密算法在Python中的一种实现:
```python
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.backends import default_backend
# 生成密钥对
def generate_rsa_keys():
key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
private_key = key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
public_key = key.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
return private_key, public_key
# 加密函数
def encrypt_message(message, public_key):
encrypted = public_key.encrypt(
message.encode(),
padding=serialization.OAEP(
mgf=serialization.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return encrypted
# 解密函数
def decrypt_message(encrypted_message, private_key):
decrypted = private_key.decrypt(
encrypted_message,
padding=serialization OAEP(
mgf=serialization.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return decrypted.decode()
# 示例
private_key, public_key = generate_rsa_keys()
message = "Hello, RSA!"
encrypted = encrypt_message(message, public_key)
decrypted = decrypt_message(encrypted, private_key)
print(f"原始消息: {message}")
print(f"加密后: {encrypted.hex()}")
print(f"解密后: {decrypted}")
```
RSA加密算法python
RSA加密算法是一种非对称加密算法,常用于数据加密和数字签名。以下是一个简单的Python实现:
```python
import random
import math
# 生成大质数
def generate_prime_number(bit_length):
while True:
prime = random.getrandbits(bit_length)
if is_prime(prime):
return prime
# 判断素数
def is_prime(n):
if n == 2 or n == 3:
return True
if n < 2 or n % 2 == 0:
return False
for i in range(3, int(math.sqrt(n))+1, 2):
if n % i == 0:
return False
return True
# 求最大公约数
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
# 求模反元素
def mod_inverse(a, m):
for i in range(1, m):
if (a * i) % m == 1:
return i
return None
# 加密
def encrypt(plaintext, public_key):
e, n = public_key
ciphertext = [pow(ord(c), e, n) for c in plaintext]
return ciphertext
# 解密
def decrypt(ciphertext, private_key):
d, n = private_key
plaintext = [chr(pow(c, d, n)) for c in ciphertext]
return ''.join(plaintext)
# 生成公钥和私钥
def generate_keypair(bit_length):
p = generate_prime_number(bit_length)
q = generate_prime_number(bit_length)
n = p * q
phi = (p-1) * (q-1)
e = random.randrange(1, phi)
while gcd(e, phi) != 1:
e = random.randrange(1, phi)
d = mod_inverse(e, phi)
return ((e, n), (d, n))
# 测试
if __name__ == '__main__':
plaintext = 'Hello, world!'
bit_length = 1024
public_key, private_key = generate_keypair(bit_length)
ciphertext = encrypt(plaintext, public_key)
decrypted_plaintext = decrypt(ciphertext, private_key)
print('Plaintext:', plaintext)
print('Ciphertext:', ciphertext)
print('Decrypted plaintext:', decrypted_plaintext)
```
在这个例子中,我们生成了一个1024位的RSA密钥对,使用公钥加密了明文,然后使用私钥解密了密文,最后输出了原始的明文。需要注意的是,RSA加密算法的安全性取决于密钥的长度,一般要求密钥长度至少为2048位。
阅读全文