android - Python代码转为java代码?
问题描述
下面是一段Python的加密代码 :
import md5def encrypted_id(id): byte1 = bytearray(’3go8&$8*3*3h0k(2)2’) byte2 = bytearray(id) byte1_len = len(byte1) for i in xrange(len(byte2)):byte2[i] = byte2[i]^byte1[i%byte1_len] m = md5.new() m.update(byte2) result = m.digest().encode(’base64’)[:-1] result = result.replace(’/’, ’_’) result = result.replace(’+’, ’-’) return result
请问如何改写为java代码?下面是我改写的java代码,但是返回的结果始终为空:
public static String md5(String musicID) throws NoSuchAlgorithmException {System.out.print(musicID);String result;byte[] byte1, byte2;String word = '3go8&$8*3*3h0k(2)2';byte1 = word.getBytes();byte2 = musicID.getBytes();int byte2_len = byte2.length;int byte1_len=byte1.length;for (int i = 0; i < byte2_len; i++) { byte2[i] = (byte) (byte2[i] ^ byte1[i % byte1_len]);}MessageDigest md5 = MessageDigest.getInstance('MD5');md5.update(byte2);byte[] digest = md5.digest();result=new String(Base64.decodeBase64(digest));result = result.replace(’/’, ’_’);result = result.replace(’+’, ’-’);return result; }
问题解答
回答1:python md5之后是base64 encode
java md5之后是base64 decode
相关文章:
1. python - 两千万条结构化数据怎么进行数据分析2. Android-studio导入.so库问题?3. 微信小程序可以用gulp,webpack吗?4. javascript - 小米浏览器中,图片导致fixed定位的元素无法显示5. javascript - 为什么!function foo(){}返回false,!function foo(){}()返回true?6. java - yuicompressor-maven-plugin 合并可用却不压缩, 哪配置不对?7. 用tp5框架写sql语句8. thinkphp5.1学习时遇到session问题9. javascript - angularJS module之间可以通信吗?10. 为什么要使用javascript函数包装器(添加在coffeescript中)“。call(this)”
