如何加密以前解密的文件,使其恢复为原始格式?

时间:2019-06-21 02:43:16

标签: python encryption

我需要解密.3w文件,在中间添加一两行,然后再将其加密回.3w,以便可以识别。当我对文件不做任何更改,只是简单地解密然后加密以查看它是否起作用时,input.gcode和output.gcode是不同的

我尝试使用如下所示的.encrypt方法,虽然可以,但不能使用

from Crypto.Cipher.AES import AESCipher, MODE_ECB, MODE_CBC

#decrypting it
file = open("input.3w", 'rb')
string = file.read()
enc_gcode = string[0x2000:]

#Edit: I added the next line
g = string[0x0: 0x2000]

aes = AESCipher("@xyzprinting.com@xyzprinting.com", mode=MODE_ECB, IV=chr(0)*16)
gcode = aes.decrypt(enc_gcode)
f=open("text.gcode", "wb")
f.write(gcode)


#from Crypto.Cipher.AES import AESCipher, MODE_ECB, MODE_CBC

#Encrypting it
file = open("text.gcode", 'rb')
string = file.read()
enc_gcode = string[0x2000:]
aes = AESCipher("@xyzprinting.com@xyzprinting.com", mode=MODE_ECB, IV=chr(0)*16)
gcode = aes.encrypt(enc_gcode)
f=open("output.3w", "wb")

#EDIT: I added the next line
gcode = g + gcode

f.write(gcode)

我希望输出和输入完全相同,但是不会发生。我是python(和加密)的新手,所以我找不到问题。

谢谢 -讨厌

0 个答案:

没有答案