String strDefaultKey = "QabC-+50";
Key key = new SecretKeySpec(strDefaultKey.getBytes("UTF-8"), "DES");
encryptCipher = Cipher.getInstance(DES_ECB);
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
String seed = "2016-09-19 05:11";
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(seed.getBytes());
byte[] m = md5.digest();
encryptCipher.doFinal(m);
byte[] encodeUrl = Base64.encodeBase64(sEncription.encrypt(m));
String finalUrl = new String(encodeUrl);
finalResult = finalUrl.substring(2, 8) + finalUrl.substring(10, 13);
我的python代码是:
m = 'QabC-+50'
text = '2016-09-19 05:11'
md5 = MD5.new()
md5.update(text)
text = md5.hexdigest()
cipher = DES.new(m, DES.MODE_ECB)
text_temp = cipher.encrypt(text)
final_str = base64.b64encode(text_temp)
print final_str
print final_str[2:8] + final_str[10:13]
print type(text_temp)
两个版本代码无法获得相同的最终字符串。有人知道为什么吗?
答案 0 :(得分:2)
你加密逻辑没问题,两种方法的区别在于它们的MD5
结果。
没有MD5
步骤:
Java代码(我不知道你的sEncription
是什么,删除它):
import java.security.Key;
import java.security.MessageDigest;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class H {
public static void main(String args[]){
try{
String strDefaultKey = "QabC-+50";
Key key = new SecretKeySpec(strDefaultKey.getBytes("UTF-8"), "DES");
Cipher encryptCipher = Cipher.getInstance("DES/ECB/NoPadding");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
String seed = "2016-09-19 05:11";
byte[] a = encryptCipher.doFinal(seed.getBytes());
byte[] encodeUrl = Base64.getEncoder().encode(a);
// byte[] encodeUrl = Base64.encodeBase64(sEncription.encrypt(m));
String finalUrl = new String(encodeUrl);
String finalResult = finalUrl.substring(2, 8) + finalUrl.substring(10, 13);
System.out.println(finalUrl);
System.out.println(finalResult);
}catch(Exception e){
e.printStackTrace();
}
}
}
输出:
Wm+DLy8m9G2BJnH2wvtKvA==
+DLy8m2BJ
Python代码:
from Crypto.Hash import MD5
from Crypto.Cipher import DES
import base64
m = 'QabC-+50'
text = '2016-09-19 05:11'
md5 = MD5.new()
md5.update(text)
# text = md5.hexdigest()
cipher = DES.new(m, DES.MODE_ECB)
text_temp = cipher.encrypt(text)
print 'text_temp is ', text_temp
final_str = base64.b64encode(text_temp)
print final_str
print final_str[2:8] + final_str[10:13]
输出:
Wm+DLy8m9G2BJnH2wvtKvA==
+DLy8m2BJ
所以没有MD5
步骤,java代码和python代码都有一些输出。
java代码中的MD5方法有什么关系,它不是获取字符串的MD5值的正确方法。
下面的代码包含获取字符串MD5值的正确方法:
import java.security.Key;
import java.security.MessageDigest;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class H {
public static void main(String args[]){
try{
String strDefaultKey = "QabC-+50";
Key key = new SecretKeySpec(strDefaultKey.getBytes("UTF-8"), "DES");
Cipher encryptCipher = Cipher.getInstance("DES/ECB/NoPadding");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
String seed = "2016-09-19 05:11";
String seedMd5 = MD5(seed);
byte[] a = encryptCipher.doFinal(seedMd5.getBytes());
byte[] encodeUrl = Base64.getEncoder().encode(a);
String finalUrl = new String(encodeUrl);
String finalResult = finalUrl.substring(2, 8) + finalUrl.substring(10, 13);
System.out.println(finalUrl);
System.out.println(finalResult);
}catch(Exception e){
e.printStackTrace();
}
}
static String MD5(String src) {
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
StringBuffer deviceIDString = new StringBuffer(src);
src = convertToHex(md.digest(deviceIDString.toString().getBytes()));
} catch (Exception e) {
src = "00000000000000000000000000000000";
}
return src;
}
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while (two_halfs++ < 1);
}
return buf.toString();
}
}
输出:
c/C16RAE1fADZXNi2H0YlevNhuucGYYHGVQ7v0Eoo9w=
C16RAEADZ
Python代码:
from Crypto.Hash import MD5
from Crypto.Cipher import DES
import base64
m = 'QabC-+50'
text = '2016-09-19 05:11'
md5 = MD5.new()
md5.update(text)
text = md5.hexdigest()
cipher = DES.new(m, DES.MODE_ECB)
text_temp = cipher.encrypt(text)
final_str = base64.b64encode(text_temp)
print final_str
print final_str[2:8] + final_str[10:13]
输出:
c/C16RAE1fADZXNi2H0YlevNhuucGYYHGVQ7v0Eoo9w=
C16RAEADZ
现在一切都好! :)