Python代码使用私有指数和模数解密64位块的RSA加密文件 [英] Python code that decrypts RSA encrypted file in chunks of 64 bit using a private exponent and modulus

查看:772
本文介绍了Python代码使用私有指数和模数解密64位块的RSA加密文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个加密文件,我试图解码作为实验的一部分。经过努力和努力,我已经能够从公钥中提取私有指数,因为模数很小:



openssl rsa -pubin -inform PEM-text - noout< public_key.pem
公钥:(64位)
模数:16513720463601767803(0xe52c8544a915157b)
指数:65537



现在,我有:

 因素:3917781347 x 4215069449 
私人指数:9440767265896423601

现在,为了导出明文,我需要将每个64位的密文块提升到私有指数mod的模数。我正在尝试编写一个Python脚本,以十六进制数据形式为我做这个。



这是迄今为止的代码:

 #!/ usr / bin / python 

file = open('encrypted.hex','r')
c = file.readline()
d = 0x830457cf1ae460b1
N = 0xe52c8544a915157b

m = hex(pow(c,d,N))rstrip(L)

f = open('new.try','w')
f.write(m)
f.close()
pre>

我已经使用 xxd 从密文文件中提取十六进制数据:
xxd -p > encrypted.hex



这创建了一个名为'encrypted.hex'的文件的十六进制转储。这个文件的内容开始是这样的:

  7d4554292d7b9f980ed049cea0f968cf438b6fc312cf2028ce5ce2fe9f38 
387b72a01bf6564f25884a2cacd187c2eeccd0cf78c2a74785f18d5e72b5
270ac3e45b6f7505347b38ec7684b1af206d73ea4a84cd59b50be56d7abf
74a569868406ab2b17846c9e448fe1392b21dac0b10fbb733536c99e598b
683be7400a1ad55c42faa171becd803b8b8f4a1fa512a33222ec042486c5
672f6200d4f00e2994b6d247a44edb6ce90795bde7ccda4433cf6fca8362
f87c68f9df6418c4f0b8fb9da39a1d173fea2b1466e646f01e2dc7fb0499
311d35ec75c15c5910b2d3e0c662de0b3b1716bab44faa2a36538bb44f6a
3c3abd37692cf95fa075b58485ad983533782d7bf51e10c0e3b18ccec972

...等等。工具'xxd'创建了hexdump并插入了换行符。



所以'c'是密文,'d'是私有指数,'N'是模数,'m'应该包含明文十六进制字节。



但是,脚本给我这个错误:

 追溯(最近的最后一次调用):
文件final.py,第8行,< module>
m = hex(pow(c,d,N))。rstrip(L)
TypeError:不支持的pow()的操作数类型:'str','long','long '

有没有人可以更正我的Python脚本,以便能够根据需要解密密文? p>

解决方案

假设 c 是16个字符的十六进制字符串,您可以拆分每16个字符(64位),然后转换此 hex chunk into an int 来运行你的教科书RSA。由于您的十六进制文件具有换行符,因此您首先需要删除这些换行符。您可以像 file.read()而不是 file.readline()。 >

最终代码

  n = 16 
c = c.replace '\\\
','').replace('\r','')
m = [hex(pow(int(c [i:i + n],16),d,N) .rstrip(L)for i in range(0,len(c),n)]

f = open('new.try','w')
f.write (''.join(m))
f.close()


I have an encrypted file that I am trying to decode as part of an experiment. After working hard and long, I have been able to extract the private exponent from the public key since the modulus was small:

openssl rsa -pubin -inform PEM -text -noout < public_key.pem Public-Key: (64 bit) Modulus: 16513720463601767803 (0xe52c8544a915157b) Exponent: 65537

Now, I have:

 Factors: 3917781347 x 4215069449 
 Private exponent: 9440767265896423601

Now, to derive the plaintext, I need to raise each 64-bit block of ciphertext to the private exponent mod the modulus. I am trying to write a Python script that will do this for me in hex data form.

This is the code I have so far:

#!/usr/bin/python

 file = open('encrypted.hex', 'r')
 c = file.readline()
 d = 0x830457cf1ae460b1
 N = 0xe52c8544a915157b

 m = hex(pow(c, d, N)).rstrip("L")

 f = open('new.try', 'w')
 f.write(m)
 f.close()

I have used xxd to extract the hex data from the ciphertext file: xxd -p > encrypted.hex

This created a hex dump of the file called 'encrypted.hex'. The contents of this file begin like this:

7d4554292d7b9f980ed049cea0f968cf438b6fc312cf2028ce5ce2fe9f38
387b72a01bf6564f25884a2cacd187c2eeccd0cf78c2a74785f18d5e72b5
270ac3e45b6f7505347b38ec7684b1af206d73ea4a84cd59b50be56d7abf
74a569868406ab2b17846c9e448fe1392b21dac0b10fbb733536c99e598b
683be7400a1ad55c42faa171becd803b8b8f4a1fa512a33222ec042486c5
672f6200d4f00e2994b6d247a44edb6ce90795bde7ccda4433cf6fca8362
f87c68f9df6418c4f0b8fb9da39a1d173fea2b1466e646f01e2dc7fb0499
311d35ec75c15c5910b2d3e0c662de0b3b1716bab44faa2a36538bb44f6a
3c3abd37692cf95fa075b58485ad983533782d7bf51e10c0e3b18ccec972

...and so on. The tool 'xxd' created the hexdump and inserted line breaks I think.

So, 'c' is the ciphertext, 'd' is the private exponent, 'N' is the modulus and 'm' is supposed to contain the plaintext hex bytes.

However, the script gives me this error:

  Traceback (most recent call last):
  File "final.py", line 8, in <module>
   m = hex(pow(c, d, N)).rstrip("L")
  TypeError: unsupported operand type(s) for pow(): 'str', 'long', 'long'

Could anyone correct my Python script so that it is able to decipher the ciphertext as desired?

解决方案

Assuming that c is a hex string with a multiple of 16 characters, you can split it every 16 characters (64-bit) and then convert this hex chunk into an int to run your textbook RSA on it. Since your hex file has line breaks, you first need to remove those line breaks. You can read the whole file like with file.read() instead of file.readline().

Final code

n = 16
c = c.replace('\n', '').replace('\r', '')
m = [hex(pow(int(c[i:i+n], 16), d, N)).rstrip("L") for i in range(0, len(c), n)]

f = open('new.try', 'w')
f.write(''.join(m))
f.close()

这篇关于Python代码使用私有指数和模数解密64位块的RSA加密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆