Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

How can I replicate encoding/decoding base64 encoded string from existing java code in python?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...