求助:Python实现DES加解密问题

【JAVA源码】
image

其中;
image

【Python实现】

Python加解密逻辑走通了,但是为什么加密出来的结果,与预期不一致呢?万分感谢

上代码,别截图

目前【Python实现】

des模式 填充方式 ECB加密方式

from pyDes import des, PAD_PKCS5, ECB
import base64

def urlFriendlyEncoding(text):
“”"
urlFriendlyEncoding字符串
:param text: 传入的字符串文本
:return: urlFriendlyEncoding后的字符串
“”"
return text.replace("=", “”).lower()

def urlFriendlyDecoding(text):
“”"
urlFriendlyDecoding字符串
:param text: 传入的字符串文本
:return: urlFriendlyDecoding后的字符串
“”"
if (text.length() % 4 != 0):
text += “===”.substring(0, 4-str.length() % 4)
return text.upper()

秘钥

initKEY = “12345678”
DES_SECRET_KEY = “*fdY5L!@Up’:qpasdfml_zaeYugHfjnv”

def des_enscrypt(s):
s = str(s)
print(s)
s = s.encode(‘utf-8’) # 这里中文要转成字节,英文好像不用
print(s)
des_obj = des(initKEY, ECB, padmode=PAD_PKCS5) # 初始化一个des对象,参数是秘钥,加密方式,填充方式
print(des_obj.getKey())
des_obj.setKey(DES_SECRET_KEY) # 更改秘钥为实际长度的秘钥
print(des_obj.getKey())
secret_bytes = des_obj.encrypt(s) # 用对象的encrypt方法加密
base64_str = str(base64.b32encode(secret_bytes), ‘utf-8’)
friend_str = urlFriendlyEncoding(base64_str)
print(“加密后是:”, friend_str)

if name == “main”:
des_enscrypt(‘55591’) # 加密结果应该是hpchzkkaytno2

【JAVA源码】
package com.dajia.studio.common.util;

import org.apache.commons.codec.binary.Base32;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DESUtil {
static Cipher ecipher;

static Cipher dcipher;

final static String strkey = “*fdY5L!@Up’:qpasdfml_zaeYugHfjnv”;

static {
DESKeySpec dks;
try {
dks = new DESKeySpec(strkey.getBytes());
SecretKeyFactory skf = SecretKeyFactory.getInstance(“DES”);
SecretKey desKey = skf.generateSecret(dks);
ecipher = Cipher.getInstance(“DES”);
dcipher = Cipher.getInstance(“DES”);
ecipher.init(Cipher.ENCRYPT_MODE, desKey);
dcipher.init(Cipher.DECRYPT_MODE, desKey);
} catch (InvalidKeyException | NoSuchAlgorithmException
| InvalidKeySpecException | NoSuchPaddingException e) {
e.printStackTrace();
}
}

public static String encrypt(String str) throws Exception {
// Encode the string into bytes using utf-8
byte utf8 = str.getBytes(“UTF8”);

// Encrypt
byte[] enc = ecipher.doFinal(utf8);

// Encode bytes to base32 to get a string
return urlFriendlyEncoding(new Base32().encodeToString(enc));

}

public static String decrypt(String str) throws Exception {
str = urlFriendlyDecoding(str);
// Decode base32 to get bytes
byte dec = new Base32().decode(str);

byte[] utf8 = dcipher.doFinal(dec);

// Decode using utf-8
return new String(utf8, "UTF8");

}

private static String urlFriendlyEncoding(String base32str) {
return base32str.replace("=", “”).toLowerCase();
}

private static String urlFriendlyDecoding(String friendlstr) {
String str = friendlstr;
if (str.length() % 4 != 0) {
str+="===".substring(0, 4-str.length() % 4);
}
return str.toUpperCase();
}

}

关闭