问题遇到的现象和发生背景
最近在做一个案例,全部都到位了,就在快要成功之时遇到了一个加密的问题解密不了。这是一种rsa加密,要解开的话我要获得他的私钥又或者叫密钥。密钥获得后,我利用在线的rsa解密网站,通过密钥将密文解了出来,经验证解出来的内容是正确的。但是问题来了,我会到网站上在线解密但是我不会在python里进行rsa解密,网上找了很多办法都不得而终,很多都是讲怎么创建公钥私钥然后利用这个进行加解密,但是都没有讲如何利用已获得的私钥(密钥)对密文进行解密。
# 这是已经获得的密钥
-----BEGIN PRIVATE KEY-----
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIXbcicDUMXbwTQa
O2z7mJ6kt+S71AO91jq0j3sN7rmtasfMo2ZRiGrZejkaNxAPSjVJ9U/8FJG7JZHE
HzOv2mtOqkSYt2Si8kofnU424jbZcxzm4zS8IeLO/FOXNYTNJ2SoJmMs1kqUZ91b
zZlZ+Mh6ZReK4OcM/lJtg21MfU31AgMBAAECgYABp4avqMDU0TSAGlrlq+5k95A2
SYQDT9NYAmdLlA6BSP1q3sKePx+6glmaXxKTJj06j2FyzaDLdE6wkIX6M5A1grxe
Jdgn4nNFVuphoW3NZNQlKeAXzHt8d6wCvlk2HuROPCDtAIPgE9k8JOZmOM8+BJ3L
mDBmgRQ1SxgowFtheQJBAMMG42KQQ3FjoJ3QKQzMixJQ6/6Fgat7KnRb+ru/E/ue
KTO0nO/JY3PZyy56t0ClPV/KVgcGFJ9j7qhJN1v+OiMCQQCvtMyZ7bHArbOPy7VX
xmYMLZI+kQChoLsmAtK9Itr6fN3EWqlWsG3Z+imFpVVkFVYNY7JZgXoZTw1ksHZA
GV0HAkB+Mu7g0MlTjkxY0mI9zf4QFB0hzfJirIiEUyCkbLjZkTbIJo+G+qJITR6V
LQN36XUVjzCrytt9aIT+kKIp/RBRAkB99mijhXLkBeLvmIN13Ka13km0jvFb7cWd
F3MK4p0H7FJz/ObfXfYyyZPQ2tlfuagKS0nFvvvQfwrD/3mWtFgrAkBN3xf+3JbG
2hauKJGx0yNbfCmweA3iqD7n3oml6fq7uL+pC4UXnvKtubUzVR1kJbRJAmR9YO4u
cv36qKveFFoS
-----END PRIVATE KEY-----
# 这是对应的密文
7bb70f65ec6a8e4e4f932df55f308f33a78636248dbacb76ebf88ea1cfd385e001fd36ede8ef61a1b4be44a69ad2c4d97adef8dd28c8ebfce8d0cd7a66fba772959dab9573592182c92e2bbc89991e6a5bbca75e92ac2bbb62f1a7c13d0fedf1b9d4bb679834f6d564152cebe592a893d71c9901116a41f9f654ecb5876e1e3f5bc696181543b86c8a2cafdb900a1c9d33841e803a0a008315aea8236cf8c9998658bb2f2699460bfbd3ee33ae94387817836213bb27f3aada76a2cb4e8fb30cb4c778206217ac3e6ae0082b7f05d2d092c6c01c20d0f46e897f67c6f0c37abf8669efb48590ef3689ace8df063d289b27432bc2b6216113f7a5f9c901e78a7f
# 这是解密后的正确结果
'''
足熔控线改荷一部磨,温尽即烧壁开锁配循控线模车。
'''
自行查找的相关资料链接
http://t.csdn.cn/9SI7N
http://t.csdn.cn/sPxLa
问题相关代码,及运行结果
# 这是看了第一个链接所写的代码
import base64
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pksc1_v1_5
from Crypto.PublicKey import RSA
def decrpt(password, private_key):
encrypt_text = password.encode('utf-8')
private_key = '-----BEGIN PRIVATE KEY-----\n' + private_key + '\n-----END PRIVATE KEY-----'
print(private_key)
rsakey = RSA.importKey(private_key)
cipher = Cipher_pksc1_v1_5.new(rsakey) # 创建用于执行pkcs1_v1_5加密或解密的密码
text = cipher.decrypt(base64.b64decode(encrypt_text), 1024)
print(text.decode('utf-8'))
return text.decode('utf-8')
if __name__ == '__main__':
wo = "7bb70f65ec6a8e4e4f932df55f308f33a78636248dbacb76ebf88ea1cfd385e001fd36ede8ef61a1b4be44a69ad2c4d97adef8dd28c8ebfce8d0cd7a66fba772959dab9573592182c92e2bbc89991e6a5bbca75e92ac2bbb62f1a7c13d0fedf1b9d4bb679834f6d564152cebe592a893d71c9901116a41f9f654ecb5876e1e3f5bc696181543b86c8a2cafdb900a1c9d33841e803a0a008315aea8236cf8c9998658bb2f2699460bfbd3ee33ae94387817836213bb27f3aada76a2cb4e8fb30cb4c778206217ac3e6ae0082b7f05d2d092c6c01c20d0f46e897f67c6f0c37abf8669efb48590ef3689ace8df063d289b27432bc2b6216113f7a5f9c901e78a7f"
private_key = '''MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIXbcicDUMXbwTQa
O2z7mJ6kt+S71AO91jq0j3sN7rmtasfMo2ZRiGrZejkaNxAPSjVJ9U/8FJG7JZHE
HzOv2mtOqkSYt2Si8kofnU424jbZcxzm4zS8IeLO/FOXNYTNJ2SoJmMs1kqUZ91b
zZlZ+Mh6ZReK4OcM/lJtg21MfU31AgMBAAECgYABp4avqMDU0TSAGlrlq+5k95A2
SYQDT9NYAmdLlA6BSP1q3sKePx+6glmaXxKTJj06j2FyzaDLdE6wkIX6M5A1grxe
Jdgn4nNFVuphoW3NZNQlKeAXzHt8d6wCvlk2HuROPCDtAIPgE9k8JOZmOM8+BJ3L
mDBmgRQ1SxgowFtheQJBAMMG42KQQ3FjoJ3QKQzMixJQ6/6Fgat7KnRb+ru/E/ue
KTO0nO/JY3PZyy56t0ClPV/KVgcGFJ9j7qhJN1v+OiMCQQCvtMyZ7bHArbOPy7VX
xmYMLZI+kQChoLsmAtK9Itr6fN3EWqlWsG3Z+imFpVVkFVYNY7JZgXoZTw1ksHZA
GV0HAkB+Mu7g0MlTjkxY0mI9zf4QFB0hzfJirIiEUyCkbLjZkTbIJo+G+qJITR6V
LQN36XUVjzCrytt9aIT+kKIp/RBRAkB99mijhXLkBeLvmIN13Ka13km0jvFb7cWd
F3MK4p0H7FJz/ObfXfYyyZPQ2tlfuagKS0nFvvvQfwrD/3mWtFgrAkBN3xf+3JbG
2hauKJGx0yNbfCmweA3iqD7n3oml6fq7uL+pC4UXnvKtubUzVR1kJbRJAmR9YO4u
cv36qKveFFoS'''
print(wo)
print('当前密文长度:', len(wo))
decrpt(wo, private_key)
# 运行结果
D:\项目\venv\Scripts\python.exe D:/项目/test.py
7bb70f65ec6a8e4e4f932df55f308f33a78636248dbacb76ebf88ea1cfd385e001fd36ede8ef61a1b4be44a69ad2c4d97adef8dd28c8ebfce8d0cd7a66fba772959dab9573592182c92e2bbc89991e6a5bbca75e92ac2bbb62f1a7c13d0fedf1b9d4bb679834f6d564152cebe592a893d71c9901116a41f9f654ecb5876e1e3f5bc696181543b86c8a2cafdb900a1c9d33841e803a0a008315aea8236cf8c9998658bb2f2699460bfbd3ee33ae94387817836213bb27f3aada76a2cb4e8fb30cb4c778206217ac3e6ae0082b7f05d2d092c6c01c20d0f46e897f67c6f0c37abf8669efb48590ef3689ace8df063d289b27432bc2b6216113f7a5f9c901e78a7f
当前密文长度: 512
-----BEGIN PRIVATE KEY-----
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIXbcicDUMXbwTQa
O2z7mJ6kt+S71AO91jq0j3sN7rmtasfMo2ZRiGrZejkaNxAPSjVJ9U/8FJG7JZHE
HzOv2mtOqkSYt2Si8kofnU424jbZcxzm4zS8IeLO/FOXNYTNJ2SoJmMs1kqUZ91b
zZlZ+Mh6ZReK4OcM/lJtg21MfU31AgMBAAECgYABp4avqMDU0TSAGlrlq+5k95A2
SYQDT9NYAmdLlA6BSP1q3sKePx+6glmaXxKTJj06j2FyzaDLdE6wkIX6M5A1grxe
Jdgn4nNFVuphoW3NZNQlKeAXzHt8d6wCvlk2HuROPCDtAIPgE9k8JOZmOM8+BJ3L
mDBmgRQ1SxgowFtheQJBAMMG42KQQ3FjoJ3QKQzMixJQ6/6Fgat7KnRb+ru/E/ue
KTO0nO/JY3PZyy56t0ClPV/KVgcGFJ9j7qhJN1v+OiMCQQCvtMyZ7bHArbOPy7VX
xmYMLZI+kQChoLsmAtK9Itr6fN3EWqlWsG3Z+imFpVVkFVYNY7JZgXoZTw1ksHZA
GV0HAkB+Mu7g0MlTjkxY0mI9zf4QFB0hzfJirIiEUyCkbLjZkTbIJo+G+qJITR6V
LQN36XUVjzCrytt9aIT+kKIp/RBRAkB99mijhXLkBeLvmIN13Ka13km0jvFb7cWd
F3MK4p0H7FJz/ObfXfYyyZPQ2tlfuagKS0nFvvvQfwrD/3mWtFgrAkBN3xf+3JbG
2hauKJGx0yNbfCmweA3iqD7n3oml6fq7uL+pC4UXnvKtubUzVR1kJbRJAmR9YO4u
cv36qKveFFoS
-----END PRIVATE KEY-----
Traceback (most recent call last):
File "D:/项目/test.py", line 35, in <module>
decrpt(wo, private_key)
File "D:/项目/test.py", line 12, in decrpt
text = cipher.decrypt(base64.b64decode(encrypt_text), 1024)
File "D:\Python\lib\site-packages\Crypto\Cipher\PKCS1_v1_5.py", line 174, in decrypt
raise ValueError("Ciphertext with incorrect length (not %d bytes)" % k)
ValueError: Ciphertext with incorrect length (not 128 bytes)
Process finished with exit code 1
# 这是看了第二个链接所写的代码
import rsa
# rsa解密
def rsaDecrypt(str, pk):
# 私钥解密
content = rsa.decrypt(str, pk)
con = content.decode("utf-8")
return con
if __name__ == "__main__":
# 密文
str = '7bb70f65ec6a8e4e4f932df55f308f33a78636248dbacb76ebf88ea1cfd385e001fd36ede8ef61a1b4be44a69ad2c4d97adef8dd28c8ebfce8d0cd7a66fba772959dab9573592182c92e2bbc89991e6a5bbca75e92ac2bbb62f1a7c13d0fedf1b9d4bb679834f6d564152cebe592a893d71c9901116a41f9f654ecb5876e1e3f5bc696181543b86c8a2cafdb900a1c9d33841e803a0a008315aea8236cf8c9998658bb2f2699460bfbd3ee33ae94387817836213bb27f3aada76a2cb4e8fb30cb4c778206217ac3e6ae0082b7f05d2d092c6c01c20d0f46e897f67c6f0c37abf8669efb48590ef3689ace8df063d289b27432bc2b6216113f7a5f9c901e78a7f'
# 密钥
pk = '''-----BEGIN PRIVATE KEY-----
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIXbcicDUMXbwTQa
O2z7mJ6kt+S71AO91jq0j3sN7rmtasfMo2ZRiGrZejkaNxAPSjVJ9U/8FJG7JZHE
HzOv2mtOqkSYt2Si8kofnU424jbZcxzm4zS8IeLO/FOXNYTNJ2SoJmMs1kqUZ91b
zZlZ+Mh6ZReK4OcM/lJtg21MfU31AgMBAAECgYABp4avqMDU0TSAGlrlq+5k95A2
SYQDT9NYAmdLlA6BSP1q3sKePx+6glmaXxKTJj06j2FyzaDLdE6wkIX6M5A1grxe
Jdgn4nNFVuphoW3NZNQlKeAXzHt8d6wCvlk2HuROPCDtAIPgE9k8JOZmOM8+BJ3L
mDBmgRQ1SxgowFtheQJBAMMG42KQQ3FjoJ3QKQzMixJQ6/6Fgat7KnRb+ru/E/ue
KTO0nO/JY3PZyy56t0ClPV/KVgcGFJ9j7qhJN1v+OiMCQQCvtMyZ7bHArbOPy7VX
xmYMLZI+kQChoLsmAtK9Itr6fN3EWqlWsG3Z+imFpVVkFVYNY7JZgXoZTw1ksHZA
GV0HAkB+Mu7g0MlTjkxY0mI9zf4QFB0hzfJirIiEUyCkbLjZkTbIJo+G+qJITR6V
LQN36XUVjzCrytt9aIT+kKIp/RBRAkB99mijhXLkBeLvmIN13Ka13km0jvFb7cWd
F3MK4p0H7FJz/ObfXfYyyZPQ2tlfuagKS0nFvvvQfwrD/3mWtFgrAkBN3xf+3JbG
2hauKJGx0yNbfCmweA3iqD7n3oml6fq7uL+pC4UXnvKtubUzVR1kJbRJAmR9YO4u
cv36qKveFFoS
-----END PRIVATE KEY-----'''
content = rsaDecrypt(str, pk)
print("解密后明文:\n%s" % content)
# 运行结果
D:\项目\venv\Scripts\python.exe D:/项目/test2.py
Traceback (most recent call last):
File "D:/项目/test2.py", line 44, in <module>
content = rsaDecrypt(str, pk)
File "D:/项目/test2.py", line 19, in rsaDecrypt
content = rsa.decrypt(str, pk)
File "D:\Python\lib\site-packages\rsa\pkcs1.py", line 255, in decrypt
blocksize = common.byte_size(priv_key.n)
AttributeError: 'str' object has no attribute 'n'
Process finished with exit code 1
我的解答思路和尝试过的方法
根据第一个链接的报错信息我估计是长度问题,我将这个案例分享个前端的一些朋友,他们也说是这个问题,用他们的语言将密文进行分段解密后再拼接得到了最终的正确结果,但是无奈的是我还是不知道在python里面要怎么写。
我想要达到的结果
另外说一声,我在在线网站里我直接把密文和密钥输进去是能直接得到结果的('https://the-x.cn/cryptography/Rsa.aspx