Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write the following function: decodexorbase64(string, key) which takes the output of the previous function (xor'd string and base64 encoded key) and returns the original string,
Write the following function:
decodexorbase64(string, key) which takes the output of the previous
function (xor'd string and base64 encoded key) and returns the
original string, in b' format.
((
FYI, previous function: encodexorbase64(string, key) takes a string and key, each in byte (b') format, and does the following:
* converts each to strings of equal length (if the string is
shorter than the key, the key is truncated; if the key is shorter
than the string, it is replicated to be of equal length)
* converts those strings to base64 encoding (use binascii module)
* xor's the base64 strings (use strxor from Crypto)
* returns a tuple with the xor'd string and the base64 encoded key
FYI, Examples:
>>> str = b'string'
>>> key = b'kermit'
>>> code = encodexorbase64(str, key)
>>> code
(b'\x02\x01\x04\x00\x03\x00Y^\x00', b'a2VybWl0 ')
>>> decodexorbase64(code[0], code[1])
b'string'
>>> str2 = b'stringstringmore'
>>> code2 = encodexorbase64(str2, key)
>>> code2
(b'\x02\x01\x04\x00\x03\x00Y^\x02\x01\x04\x00\x03\x00Y^\x03eo\x008\x00\x00\x00\x00', b'a2VybWl0a2VybWl0a2VybQ== ')
>>> decodexorbase64(code2[0], code2[1])
b'stringstringmore'
FYI, here's what I have so far:
def encodexorbase64(string, key):
strlen = len(string)
keylen = len(key)
if strlen > keylen:
keynew = key[0:strlen]
count = keylen
while strlen < keylen:
keynew += keynew[count+1]
count+=1
keyb64 = binascii.b2a_base64(keynew)
strb64 = binascii.b2a_base64(string)
tple = strxor(strb64, keyb64)
return tple
def decodexorbase64(code, keyb64):
keyoriginal = base64.standard_b64decode(keyb64)
stroriginal = base64.standard_b64decode(code[0])
return(stroriginal, keyoriginal)
))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started