在特殊符号Python上进行加密和解密

时间:2018-08-24 02:29:00

标签: python-3.x encryption utf-8

test = '\x02m@\x0e\x00'
print(test)
print(test.replace('╗m@', "first"))
new_test = test.encode('raw_unicode_escape').decode('utf-8')
print(new_test)
print(new_test.replace('╗m@', "second"))
again_new = new_test.encode('raw_unicode_escape').decode('utf-8')
print(again_new)
print(again_new.replace('╗m@', "third"))

我一直在尝试解密一些最初使用python 3.6通过VB.Net加密的文件;在将文件编码为base 64之前,已使用公钥对该文件进行了加密。我尝试了多种方法来解码base 64并使用给定的私钥对其进行解密,但我总是会遇到一些特殊字符,例如“╗m@”,即使尝试手动删除它们也无法删除。

我也尝试通过'utf-8'和'ascii'进行解码,但无济于事。我尝试环顾四周,有人告诉我它看起来像是双重编码的字符串,但即使解码两次后,我似乎也无法替换该字符串。

据我所知,'\ x02m @ \ x0e \ x00'在运行时将返回'╗m@'。字符串无法直接解码,因此,在解码之前,我利用“ raw_unicode_escape”将字符串更改为字节对象。

如果有人可以指出我应该朝哪个方向前进,将不胜感激。谢谢。

import Crypto
from Crypto.PublicKey import RSA
import base64
import codecs
import re

# reading private key 
rsa_key = RSA.importKey(open('PrivateKey.pem', 'r').read())

# reading the file that is suppose to be decrypted
file = open('test.txt', 'r')
message = file.read()

# decode base64 first
decrypted_body = base64.b64decode(message)
print(decrypted_body)

# have to make it bytearray if not, it would not be able to concat with the decrypted chunk
final_decrypted = bytearray()

# size of the chunk to decrypt
chunk_size = 128
offset = 0

# maximum length of the body that has to be decrypted
limit = len(decrypted_body)

# there is a need to check the length that has to be decrypted so as to not overshot during the last iteration
while offset < len(decrypted_body):
    if (offset + chunk_size > limit):
        copyLength = limit
    else:
        copyLength = offset + chunk_size

    chunk = decrypted_body[offset: copyLength]

    final_decrypted += rsa_key.decrypt(chunk)

    offset += chunk_size

# decode the result to utf-8 to remove bytes that can't be read
xml_decrypted = codecs.decode(final_decrypted, encoding = 'utf-8', errors = 'ignore')

break_loop = False
remove_arr = []
# pattern to find the special character in what was decoded
pattern = re.compile('[^0-9a-zA-Z<>_=\"?. -/@:™{^\\r}{^\\n}]+')
for i in range(0, len(xml_decrypted)):
    if (pattern.match(xml_decrypted[i])):
        arr = []
        arr.append(xml_decrypted[i])
        # \x00 refer to " " thus, I do not want to remove it as it would make the xml harder to read
        # this is to append all the special symbol into an array 
        while (xml_decrypted[i] != '\x00' and i < len(xml_decrypted)):
            n = i
            i += 1
            if (i < len(xml_decrypted)):
                arr.append(xml_decrypted[i])
            else:
                break_loop = True
                break

        remove_str = "".join(arr)
        remove_arr.append(remove_str)
        i = n
        if (break_loop):
            break

# check if the array has any " " inside as there is no need to remove space from the xml result
# remove each of the result inside the array from the decoded result which will hopefully return me the xml without any special symbol
new_str = ""
remove_arr = [x for x in remove_arr if x != '\x00']
for each in remove_arr:
    print(str(each))
    new_str = xml_decrypted.replace(each, '')
    xml_decrypted = new_str

print(xml_decrypted)

根据要求,我已经在这里发布了python 3的代码,但是由于它不属于我,所以我无法发布VB.Net代码。干杯

1 个答案:

答案 0 :(得分:0)

您正在使用原始或教科书RSA。从decrypt文档中:

  

注意:此功能执行简单的原始RSA解密(教科书)。在实际的应用程序中,您始终需要使用适当的加密填充,并且您不应该使用此方法直接解密数据。否则可能会导致安全漏洞。建议改用模块private void rendowWindowText (Marker marker, View view) { String title = marker.getTitle(); TextView tvTitle = view.findViewById(R.id.txt_title); if (!title.equals("")) { tvTitle.setText(title); } String snippet = marker.getSnippet(); TextView tvSnippet = view.findViewById(R.id.txt_snippet); if (!title.equals("")) { tvSnippet.setText(snippet); } getPhotos(); } Crypto.Cipher.PKCS1_OAEP

因此,您可能会忘记从结果中删除(主要是随机的)填充。我会先尝试Crypto.Cipher.PKCS1_v1_5


强烈建议使用混合加密(例如AES / GCM与RSA OAEP结合使用),而不是使用同一RSA密钥多次加密。