android - Python代碼轉(zhuǎn)為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代碼,但是返回的結(jié)果始終為空:
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
相關(guān)文章:
1. javascript - sublime快鍵鍵問題2. javascript - immutable配合react提升性能?3. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽數(shù)據(jù)變化4. 如何解決Centos下Docker服務(wù)啟動無響應(yīng),且輸入docker命令無響應(yīng)?5. 實(shí)現(xiàn)bing搜索工具urlAPI提交6. javascript - 移動端上不能實(shí)現(xiàn)拖拽布局嗎?7. angular.js - 單頁應(yīng)用(ng/vue)該如何監(jiān)聽用戶離開當(dāng)前頁面(或者路由)?8. javascript - ios返回不執(zhí)行js怎么解決?9. thinkPHP5中獲取數(shù)據(jù)庫數(shù)據(jù)后默認(rèn)選中下拉框的值,傳遞到后臺消失不見。有圖有代碼,希望有人幫忙10. index.php錯誤,求指點(diǎn)

網(wǎng)公網(wǎng)安備