I have existing java code that is decoding a base64 encoded string. It's subsequently using that string for functionality. I want to replicate that functionality in a python script and cannot seem to do so. I am confident this is happening when new String()
is called. If we use the byte array directly from the base64 decode to re-encode, the re-encoded string matches the initial value.
As mentioned, the java code cannot be changed, so I'd like to replicate the existing functionality in python if possible.
Java Code
String test = "MV6iqg2hXt5t4U53QCuBqmGuum11UCdT396SqOqBO0U=";
String base64Decoded = new String(Base64.getDecoder().decode(test));
String reEncoded = new String(Base64.getEncoder().encode(base64Decoded.getBytes()));
// I care about the string value here, but re-encoding to demonstrate that it has changed
System.out.println(reEncoded); // MV7vv73vv70N77+9Xu+/vW3vv71Od0Ar77+977+9Ye+/ve+/vW11UCdT77+93pLvv73vv707RQ==
Python Code
test = 'MV6iqg2hXt5t4U53QCuBqmGuum11UCdT396SqOqBO0U='
base64_decoded = base64.b64decode(test)
print(base64_decoded)
re_encoded = base64.b64encode(base64_decoded)
# again; I really care about the value here
print(re_encoded) # returns the initial value
question from:
https://stackoverflow.com/questions/65862487/how-can-i-replicate-encoding-decoding-base64-encoded-string-from-existing-java-c 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…