java 加密后的结果为: D8B5E8E040EAFDB4E7D02A4D235A2462193D557C7DEE2D0E
package com.oa.base;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.w3c.dom.*;
import com.primeton.crimson.tree.ElementNode;
import com.primeton.eos.bizlets.util.BNXmlUtil;
import com.primeton.tp.core.api.BizContext;
public class Encrypt {
private static final String keys="8b57d0d3";
public static String myDecrypt(String message, String key) throws Exception {
byte[] bytesrc = convertHexString(message);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("US-ASCII"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("US-ASCII"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(bytesrc);
return new String(retByte);
}
public static String myEncrypt(String message, String key) throws Exception {
// message = java.net.URLEncoder.encode(message, "ascii");
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("US-ASCII"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("US-ASCII"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] encryptbyte = cipher.doFinal(message.getBytes());
return toHexString(encryptbyte).toUpperCase();
}
public static byte[] convertHexString(String ss) {
byte digest[] = new byte[ss.length() / 2];
for (int i = 0; i < digest.length; i++) {
String byteString = ss.substring(2 * i, 2 * i + 2);
int byteValue = Integer.parseInt(byteString, 16);
digest[i] = (byte) byteValue;
}
return digest;
}
public static String toHexString(byte b[]) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String plainText = Integer.toHexString(0xff & b[i]);
if (plainText.length() < 2)
plainText = "0" + plainText;
hexString.append(plainText);
}
return hexString.toString();
}
/**
*
* @param doc type: Document, DOM;
* @param param type: BizContext;
* @return: int ,运算逻辑返回值,如果失败返回0,成功返回1
* @throws Exception
* <p>
* ** bizlet 的显示名称 **
* @bizlet_displayName Encrypt
*/
public static int Encrypt(Document doc, BizContext param) throws Exception{
Node userID = (Node)param.getParaObjectAt(0);
String user = myDecrypt(BNXmlUtil.getNodeValue(userID), keys);
BNXmlUtil.setNodeValue(userID, user);
return 1;
}
}
这个是 Java 方面的加密方式及解密方式, 如何在 ruby 中使用 DES 解密 Java 中加密的内容? ruby 解密之后的结果应该为 2018h03e29h11eng