下面是一段Python的加密代码 :
import md5
def 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;
}
迷茫2017-04-18 10:29:22
python md5 is followed by base64 encode
java md5 is followed by base64 decode