我正在尝试编写两个程序,一个将字符串转换为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) 好吧我正在尝试将字符串的基本转换器写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) 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)