在Python中编码base64并在C#中解码

时间:2018-03-01 12:36:08

标签: c# python

我正在尝试在python中对string进行base64编码,然后在C#中进行解码, 就目前我所看到的,问题是来自python的base64.b64encoding是用ASCII编码的。当我使用Convert.FromBase64String时,它使用unicode ..

Python:

msg = cipher.encrypt("hello")
msgb64 = base64.b64encode(msg)
  

编码消息:   b'UORSfV0 =',编码前的密文:b'P \ xe4R}]'

C#:

byte[] bytes = Convert.FromBase64String("UORSfV0=");
string s = Encoding.ASCII.GetString(bytes);
  

要显示字符串中的输出,请在aes解密之前显示:“PäR}]”

从此步骤开始,错误开始,因为它不是密文

有关解决方案的任何建议吗?

3 个答案:

答案 0 :(得分:2)

AES处理字节缓冲区,而不是字符串。在解密之前没有理由将缓冲区转换为字符串。此外,该字符串对于AES来说太短了。 字节缓冲区如何使用各种代码页看起来与字节值是否正确无关。

我怀疑解密代码存在问题,但未发布。 OP尝试将缓冲区转换为字符串以比较值。虽然Base64没有被打破。没有理由去寻找任何.NET开发人员15年来没有注意到的漏洞。

如果没有试图解密bytes缓冲区的代码,就不可能说出错了。尽管如此,该字符串看起来太短了。

其余的只是为了解释?的来源和原因。

原始答案

此代码存在多个问题。您在C#端使用的Base64字符串有一个额外的空间。

即使修复后,代码页肯定是错误的。 Encoding.ASCII是7位字符集,而Python字符串包含\xe4,即ä。这既不是ASCII也不是UTF8字符串。该角色肯定在有效范围之外。您需要知道用于解码的正确代码页。

在不知道实际代码页的情况下,人们只能猜测。可以解码此字符的一个代码页是1253,即Latin1。如果您使用:

byte[] bytes = Convert.FromBase64String("UORSfV0=");
string s = Encoding.GetEncoding(1252).GetString(bytes);

你会回来PäR}]

如果你使用了1253,虽然你得到PδR}]。 1251将返回PдR}]。您打算使用哪一个?

您唯一可以肯定的是,某些代码页会失败并返回?,或者定义良好的Unicode替换字符

为避免转换错误,您应该在Python和C#上使用UTF8。 C#和Windows通常使用Unicode(特定于UTF16),这就是我可以在答案中发布这些字符的原因。没有涉及特殊编码。大多数与文件相关的类默认使用UTF8并返回Unicode字符串。

答案 1 :(得分:0)

尝试Default编码

  

byte[] bytes = Convert.FromBase64String("UORSfV0 =");
  string s = Encoding.ASCII.GetString(bytes);

byte[] bytes = Convert.FromBase64String("UORSfV0 =");
string s = Encoding.Default.GetString(bytes);

答案 2 :(得分:0)

我花了一些时间来开发此Python3.7解决方案,以示例为例:

'''        Argument =   [110, 13, 46, 136, 95, 66, 92, 132, 109, 217, 58, 112, 43, 8, 145, 
                         42, 233, 98, 40, 139, 165, 228, 52, 9, 89, 175, 146, 103, 227, 238, 233, 190, 
                         78, 175, 242, 224, 202, 138, 248, 103, 114, 98, 199, 252, 80, 86, 61, 174]
            return =    'bg0uiF9CXIRt2TpwKwiRKuliKIul5DQJWa+SZ+Pu6b5Or/Lgyor4Z3Jix/xQVj2u' 
'''

赞:

import base64
import numpy as np


# Do the same as Convert.FromBase64String() from C#
def ConvertFromBase64String(encrypted):
    return np.frombuffer(base64.b64decode(encrypted.encode()), np.uint8)

# Do the oposite of Convert.FromBase64String() from C#
def ConvertToBase64String(encrypted):
    return (base64.b64encode(textoUnicodeToUtf8Literal(("".join([chr(item) for item in np.array(encrypted, dtype=np.uint8)]))).encode('ISO-8859-1'))).decode()



def textoUnicodeToUtf8Literal(encodando):
    return encodando.replace("\xc2\x80", r'\x80').replace("\xc2\x81", r'\x81').replace("\xc2\x82", r'\x82')\
        .replace("\xc2\x83", r'\x83').replace("\xc2\x84", r'\x84').replace("\xc2\x85", r'\x85')\
        .replace("\xc2\x86", r'\x86').replace("\xc2\x87", r'\x87').replace("\xc2\x88", r'\x88')\
        .replace("\xc2\x89", r'\x89').replace("\xc2\x8a", r'\x8A').replace("\xc2\x8b", r'\x8B')\
        .replace("\xc2\x8c", r'\x8C').replace("\xc2\x8d", r'\x8D').replace("\xc2\x8e", r'\x8E')\
        .replace("\xc2\x8f", r'\x8F').replace("\xc2\x90", r'\x90').replace("\xc2\x91", r'\x91')\
        .replace("\xc2\x92", r'\x92').replace("\xc2\x93", r'\x93').replace("\xc2\x94", r'\x94')\
        .replace("\xc2\x95", r'\x95').replace("\xc2\x96", r'\x96').replace("\xc2\x97", r'\x97')\
        .replace("\xc2\x98", r'\x98').replace("\xc2\x99", r'\x99').replace("\xc2\x9a", r'\x9A')\
        .replace("\xc2\x9b", r'\x9B').replace("\xc2\x9c", r'\x9C').replace("\xc2\x9d", r'\x9D')\
        .replace("\xc2\x9e", r'\x9E').replace("\xc2\x9f", r'\x9F').replace("\xc2\xa0", r'\xA0')\
        .replace("\xc2\xa1", r'\xA1').replace("\xc2\xa2", r'\xA2').replace("\xc2\xa3", r'\xA3')\
        .replace("\xc2\xa4", r'\xA4').replace("\xc2\xa5", r'\xA5').replace("\xc2\xa6", r'\xA6')\
        .replace("\xc2\xa7", r'\xA7').replace("\xc2\xa8", r'\xA8').replace("\xc2\xa9", r'\xA9')\
        .replace("\xc2\xaa", r'\xAA').replace("\xc2\xab", r'\xAB').replace("\xc2\xac", r'\xAC')\
        .replace("\xc2\xad", r'\xAD').replace("\xc2\xae", r'\xAE').replace("\xc2\xaf", r'\xAF')\
        .replace("\xc2\xb0", r'\xB0').replace("\xc2\xb1", r'\xB1').replace("\xc2\xb2", r'\xB2')\
        .replace("\xc2\xb3", r'\xB3').replace("\xc2\xb4", r'\xB4').replace("\xc2\xb5", r'\xB5')\
        .replace("\xc2\xb6", r'\xB6').replace("\xc2\xb7", r'\xB7').replace("\xc2\xb8", r'\xB8')\
        .replace("\xc2\xb9", r'\xB9').replace("\xc2\xba", r'\xBA').replace("\xc2\xbb", r'\xBB')\
        .replace("\xc2\xbc", r'\xBC').replace("\xc2\xbd", r'\xBD').replace("\xc2\xbe", r'\xBE')\
        .replace("\xc2\xbf", r'\xBF').replace("\xc3\x80", r'\xC0').replace("\xc3\x81", r'\xC1')\
        .replace("\xc3\x82", r'\xC2').replace("\xc3\x83", r'\xC3').replace("\xc3\x84", r'\xC4')\
        .replace("\xc3\x85", r'\xC5').replace("\xc3\x86", r'\xC6').replace("\xc3\x87", r'\xC7')\
        .replace("\xc3\x88", r'\xC8').replace("\xc3\x89", r'\xC9').replace("\xc3\x8a", r'\xCA')\
        .replace("\xc3\x8b", r'\xCB').replace("\xc3\x8c", r'\xCC').replace("\xc3\x8d", r'\xCD')\
        .replace("\xc3\x8e", r'\xCE').replace("\xc3\x8f", r'\xCF').replace("\xc3\x90", r'\xD0')\
        .replace("\xc3\x91", r'\xD1').replace("\xc3\x92", r'\xD2').replace("\xc3\x93", r'\xD3')\
        .replace("\xc3\x94", r'\xD4').replace("\xc3\x95", r'\xD5').replace("\xc3\x96", r'\xD6')\
        .replace("\xc3\x97", r'\xD7').replace("\xc3\x98", r'\xD8').replace("\xc3\x99", r'\xD9')\
        .replace("\xc3\x9a", r'\xDA').replace("\xc3\x9b", r'\xDB').replace("\xc3\x9c", r'\xDC')\
        .replace("\xc3\x9d", r'\xDD').replace("\xc3\x9e", r'\xDE').replace("\xc3\x9f", r'\xDF')\
        .replace("\xc3\xa0", r'\xE0').replace("\xc3\xa1", r'\xE1').replace("\xc3\xa2", r'\xE2')\
        .replace("\xc3\xa3", r'\xE3').replace("\xc3\xa4", r'\xE4').replace("\xc3\xa5", r'\xE5')\
        .replace("\xc3\xa6", r'\xE6').replace("\xc3\xa7", r'\xE7').replace("\xc3\xa8", r'\xE8')\
        .replace("\xc3\xa9", r'\xE9').replace("\xc3\xaa", r'\xEA').replace("\xc3\xab", r'\xEB')\
        .replace("\xc3\xac", r'\xEC').replace("\xc3\xad", r'\xED').replace("\xc3\xae", r'\xEE')\
        .replace("\xc3\xaf", r'\xEF').replace("\xc3\xb0", r'\xF0').replace("\xc3\xb1", r'\xF1')\
        .replace("\xc3\xb2", r'\xF2').replace("\xc3\xb3", r'\xF3').replace("\xc3\xb4", r'\xF4')\
        .replace("\xc3\xb5", r'\xF5').replace("\xc3\xb6", r'\xF6').replace("\xc3\xb7", r'\xF7')\
        .replace("\xc3\xb8", r'\xF8').replace("\xc3\xb9", r'\xF9').replace("\xc3\xba", r'\xFA')\
        .replace("\xc3\xbb", r'\xFB').replace("\xc3\xbc", r'\xFC').replace("\xc3\xbd", r'\xFD')\
        .replace("\xc3\xbe", r'\xFE').replace("\xc3\xbf", r'\xFF')