小编spe*_*man的帖子

将字符串转换为Base 64和从Base 64转换

我正在尝试编写两个程序,一个将字符串转换为base64,然后另一个程序接受base64编码的字符串并将其转换回字符串.
到目前为止,我不能通过base64编码部分,因为我一直得到错误

TypeError: expected bytes, not str
Run Code Online (Sandbox Code Playgroud)

到目前为止我的代码看起来像这样

def convertToBase64(stringToBeEncoded):
import base64
EncodedString= base64.b64encode(stringToBeEncoded)
return(EncodedString)
Run Code Online (Sandbox Code Playgroud)

base64 python-3.x

20
推荐指数
1
解决办法
5万
查看次数

将String转换为MD5

好吧我正在尝试将字符串的基本转换器写md5入哈希代码,但是当我运行我的程序时,我不断收到错误信息:

Traceback (most recent call last):
  File "C:\Users\Shane\Documents\Amer CISC\lab4.py", line 30, in <module>
    assertEqual (computeMD5hash("The quick brown fox jumps over the lazy dog"),("9e107d9d372bb6826bd81d3542a419d6"))
  File "C:\Users\Shane\Documents\Amer CISC\lab4.py", line 27, in computeMD5hash
    m.update(string)
TypeError: Unicode-objects must be encoded before hashing
Run Code Online (Sandbox Code Playgroud)

我的代码看起来像这样:

def computeMD5hash(string):
    import hashlib
    from hashlib import md5
    m = hashlib.md5()
    m.update((string))
    md5string=m.digest()
    return md5string
Run Code Online (Sandbox Code Playgroud)

python string md5 python-3.x

11
推荐指数
2
解决办法
4万
查看次数

如何从Base64转换为字符串Python 3.2

可能重复:
将字符串转换为Base 64和从Base 64转换字符串

def convertFromBase64 (stringToBeDecoded):
    import base64
    decodedstring=str.decode('base64',"stringToBeDecoded")
    print(decodedstring)
    return

convertFromBase64(dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=)
Run Code Online (Sandbox Code Playgroud)

我想要使​​用base64编码的字符串并将其转换回原始字符串,但我无法弄清楚到底出了什么问题

我收到了这个错误

 Traceback (most recent call last):
 File "C:/Python32/junk", line 6, in <module>
convertFromBase64(("dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE="))
 File "C:/Python32/junk", line 3, in convertFromBase64
decodedstring=str.decode('base64',"stringToBeDecoded")
AttributeError: type object 'str' has no attribute 'decode'
Run Code Online (Sandbox Code Playgroud)

base64 python-3.x

9
推荐指数
1
解决办法
4万
查看次数

标签 统计

python-3.x ×3

base64 ×2

md5 ×1

python ×1

string ×1