java的des加密解密代码如下:

package com.api.call.demo; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import sun.misc.*; /** * RSA 加密算法是目前最有影响力的 公钥加密算法,并且被普遍认为是目前 最优秀的公钥方案 之一。RSA 是第一个能同时用于 加密 和 数字签名 的算法,它能够 抵抗 到目前为止已知的 所有密码攻击,已被 ISO 推荐为公钥数据加密标准。 */ public class DESPlus { private static String strDefaultKey = "PLFP"; //默认密钥 private static final byte[] iv = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xab, (byte) 0xcd, (byte) 0xef};//des 向量 private static BASE64Encoder enc = new BASE64Encoder();//将byte[]转换成String private static BASE64Decoder dec = new BASE64Decoder(); //将String转换成byte[] /** * 加密字节数组 * * @param arrB * 需加密的字节数组 * @param key * 密钥 * @return 加密后的字节数组 * @throws Exception */ public static byte[] encrypt(byte[] arrB, String key) throws Exception { DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("utf-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec ivp = new IvParameterSpec(DESPlus.iv); Cipher encryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivp); return encryptCipher.doFinal(arrB); } /** * 加密字符串 * * @param xml * 需加密的字符串 * @param key * 密钥 * @return 加密后的字符串 * @throws Exception */ public static String encrypt(String xml, String key) throws Exception { //return DESPlus.enc.encode(encrypt(xml.getBytes(), key)); return jdkBase64String(encrypt(xml.getBytes(), key)); } /** * 使用base64解决乱码 * * @param secretKey 加密后的字节码 */ public static String jdkBase64String(byte[] secretKey) { BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(secretKey); } /** * 使用默认公钥加密字符串 * @param xml 需加密的字符串 * @return 加密后的字符串 * @throws Exception */ public static String encrypt(String xml) throws Exception { return encrypt(xml, strDefaultKey); } /** * 解密字节数组 * * @param arrB * 需解密的字节数组 * @param key * 密钥 * @return 解密后的字节数组 * @throws Exception */ public static byte[] decrypt(byte[] arrB, String key) throws Exception { DESKeySpec desKeySpec = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec ivp = new IvParameterSpec(DESPlus.iv); Cipher decryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, ivp); return decryptCipher.doFinal(arrB); } /** * 解密字符串 * * @param xml * 需解密的字符串 * @param key * 密钥 * @return 解密后的字符串 * @throws Exception */ public static String decrypt(String xml, String key) throws Exception { return new String(decrypt(DESPlus.dec.decodeBuffer(xml), key)); } /** * 使用默认公钥解密字符串 * @param xml 需解密的字符串 * @return 解密后的字符串 * @throws Exception */ public static String decrypt(String xml) throws Exception { return decrypt(xml, strDefaultKey); } /** * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位 * * @param arrBTmp * 构成该字符串的字节数组 * @return 生成的密钥 * @throws java.lang.Exception */ private Key getKey(byte[] arrBTmp) throws Exception { // 创建一个空的8位字节数组(默认值为0) byte[] arrB = new byte[8]; // 将原始字节数组转换为8位 for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) { arrB[i] = arrBTmp[i]; } // 生成密钥 Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES"); return key; } /** * 获取默认密钥 * @return */ public static String getDesKey() { return DESPlus.strDefaultKey; } }
执行测试程序:
public static void main(String[] args) { try { //测试密匙 String key = "BOC_PLFP"; //004交易案例 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT><HEADER><TRANNO>004</TRANNO><BNKNO>17850</BNKNO><COMNO>COM01</COMNO><SN>1109141222222660161</SN></HEADER><BODY><TRANLOG><APPNO>0000000123</APPNO><NOTATION>客户信息不完整,请补充资料。</NOTATION></TRANLOG></BODY></ROOT>"; System.out.println("密钥:" + key); System.out.println("加密前的字符串:" + xml); System.out.println("加密前的字符串长度:" + xml.getBytes().length); //加密 xml = DESPlus.encrypt(xml, key); System.out.println("加密后的字符串:" + xml); System.out.println("加密后的字符串长度:" + xml.getBytes().length); //解密 xml = DESPlus.decrypt(xml, key); System.out.println("解密后的字符串:" + xml); System.out.println("解密后的字符串长度:" + xml.getBytes().length); } catch (Exception e) { e.printStackTrace(); } }
上面运行结果为:
密钥:BOC_PLFP
加密前的字符串:<?xml version="1.0" encoding="UTF-8"?><ROOT><HEADER><TRANNO>004</TRANNO><BNKNO>17850</BNKNO><COMNO>COM01</COMNO><SN>1109141222222660161</SN></HEADER><BODY><TRANLOG><APPNO>0000000123</APPNO><NOTATION>客户信息不完整,请补充资料。</NOTATION></TRANLOG></BODY></ROOT>
加密前的字符串长度:262
加密后的字符串:bE5N44gjyfO3SdUs6/OhVg4I4l725S2vWcKBRxYOAd/eAnyuADKXeNNgVXJMj3aJJzndntv364rh
YW2bF33lmEABMU43HfS8DcXX7+QrcIjp3mrk7uJdiNHu4T4oHMeqetFZqU5oh2XY1sbBPPdGEgMf
/OguRVaTblzl/ylkFc6C9BNNSD0IwL0Ks7Mi73+V76P+aFdPgXQc7u4Vkq8Cd6+HgHErbHbJI729
JPJKM5L2YAAW4Q06oi4yMoEASDjYf7Aa1X/FWqclsZImSDB0okGOiuj857l94BM1zYl2RtWdXa9o
0beiL4CbEvKSC3U3PydAI0+mZbtE0sVkyP0sXTke7ifrwiMG
加密后的字符串长度:360
解密后的字符串:<?xml version="1.0" encoding="UTF-8"?><ROOT><HEADER><TRANNO>004</TRANNO><BNKNO>17850</BNKNO><COMNO>COM01</COMNO><SN>1109141222222660161</SN></HEADER><BODY><TRANLOG><APPNO>0000000123</APPNO><NOTATION>客户信息不完整,请补充资料。</NOTATION></TRANLOG></BODY></ROOT>
解密后的字符串长度:262
参考了http://www.lijingquan.net/des-c-php-java.html一文,修改里面的des向量iv,字符编码UTF8为Default等
C#加解密代码:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; public class DES { private const string defaultKey = "BOC_PLFP"; //默认密钥 private static byte[] iv = { 0x12, 0x34, 0x56, 0x78, (byte)0x90, (byte)0xab, (byte)0xcd, (byte)0xef };//des 向量 /// <summary> /// des加密 /// </summary> /// <param name="pToEncrypt">要加密的字符串。</param> /// <param name="sKey">密钥,且必须为8位。默认公钥加密字符串defaultKey</param> /// <returns>以Base64格式返回的加密字符串。</returns> public static string Encrypt(string pToEncrypt, string sKey = defaultKey) { using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = iv; //ASCIIEncoding.ASCII.GetBytes(sKey); System.IO.MemoryStream ms = new System.IO.MemoryStream(); using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cs.Close(); } string str = Convert.ToBase64String(ms.ToArray()); ms.Close(); return str; } } /// <summary> /// des解密 /// </summary> /// <param name="pToDecrypt">要解密的以Base64</param> /// <param name="sKey">密钥,且必须为8位。默认公钥解密字符串defaultKey</param> /// <returns>已解密的字符串。</returns> public static string Decrypt(string pToDecrypt, string sKey = defaultKey) { byte[] inputByteArray = Convert.FromBase64String(pToDecrypt); using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = iv; // ASCIIEncoding.ASCII.GetBytes(sKey); System.IO.MemoryStream ms = new System.IO.MemoryStream(); using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cs.Close(); } string str = Encoding.Default.GetString(ms.ToArray()); ms.Close(); return str; } } }
经测试,加密和解密的结果和java的一样。
【转载】 https://blog.csdn.net/gdjlc/article/details/9704399
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决