单纯记录一下Python中DES加密解密的使用方式直接看代码:
Python代码段
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# encoding:utf-8
# @Author : Benjamin
# @Time : 2020/7/15 15:47
# des模式 填充方式 ECB加密方式
from pyDes import des, PAD_PKCS5, ECB
# 秘钥 (如果和Java对接,两边要有相同的秘钥)
DES_KEY = "test*key*"
s = '哈哈'.encode() # 这里中文要转成字节, 英文好像不用
des_obj = des(DES_KEY, ECB, DES_KEY, padmode=PAD_PKCS5) # 初始化一个des对象,参数是秘钥,加密方式,偏移, 填充方式
# secret_bytes = des_obj.encrypt(s) # 用对象的encrypt方法加密
# s = des_obj.decrypt(secret_bytes) # 用对象的decrypt方法解密
# print(secret_bytes,s)
def encrypt(s):
s = s.encode() # 这里中文要转成字节
secret_bytes = des_obj.encrypt(s) # 用对象的encrypt方法加密
return secret_bytes.hex()
def decrypt(secret_bytes):
secret_bytes = bytes.fromhex(secret_bytes) # 这里中文要转成字节
s = des_obj.decrypt(secret_bytes) # 用对象的decrypt方法解密
return s.decode()
test = encrypt("123")
print(test)
print(decrypt(test))
Java代码对比
public class RDes {
/** 加密算法,可用 DES,DESede,Blowfish. */
private final static String ALGORITHM = "DES";
/**
* DES解密算法
*
* @param cryptKey 密钥 要是偶数
* @throws Exception
*/
public static String decrypt(String data, String cryptKey) throws Exception {
return new String(decrypt(hex2byte(data.getBytes()), cryptKey.getBytes()));
}
/**
* DES加密算法
*
* @throws Exception
*/
public final static String encrypt(String data, String cryptKey) throws Exception {
return byte2hex(encrypt(data.getBytes(), cryptKey.getBytes()));
}
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(ALGORITHM);
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
// 现在,获取数据并加密
// 正式执行加密操作
return cipher.doFinal(data);
}
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建一个DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(ALGORITHM);
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
// 现在,获取数据并解密
// 正式执行解密操作
return cipher.doFinal(data);
}
private static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0) throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
private static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}
}