private const string KEY = "VZCDFKBG";
/// <summary>
/// DES加密
/// </summary>
/// <param name="pToEncrypt"></param>
/// <returns></returns>
public static string DESEncrypt(string pToEncrypt)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(KEY);
des.IV = ASCIIEncoding.ASCII.GetBytes(KEY);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
ret.AppendFormat("{0:X2}", b);
ret.ToString();
return ret.ToString();
}
catch (Exception)
{
return "";
}
}
![](https://profile-avatar.csdnimg.cn/d4d3e06b9a94480f96a53987ea39087b_weixin_45157597.jpg!4)
能否将这个des加密C#版本改成Java版本
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- CSDN专家-赖老师(软件之家) 2021-12-17 17:38关注
DES工具类:
public class AesUtils { public static final String SECRETKEY = "secretKey"; public static String encryptStr(String srcStr, String password) { byte[] encryptResult = encryptData_AES(srcStr, password); String encryptResultStr = parseByte2HexStr(encryptResult); return encryptResultStr; } public static String decryptStr(String srcStr, String password) { String returnValue = ""; try { byte[] decryptFrom = parseHexStr2Byte(srcStr); byte[] decryptResult = decryptData_AES(decryptFrom, password); returnValue = new String(decryptResult, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return returnValue; } private static String parseByte2HexStr(byte buf[]) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < buf.length; i++) { String hex = Integer.toHexString(buf[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } private static byte[] encryptData_AES(String content, String password) { try { SecretKey secretKey = getKey(password); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); byte[] byteContent = content.getBytes("utf-8"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] result = cipher.doFinal(byteContent); return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } return null; } private static byte[] parseHexStr2Byte(String hexStr) { if (hexStr.length() < 1) return null; byte[] result = new byte[hexStr.length() / 2]; for (int i = 0; i < hexStr.length() / 2; i++) { int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; } private static SecretKey getKey(String secret) throws GeneralSecurityException { try { KeyGenerator _generator = KeyGenerator.getInstance("AES"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(secret.getBytes()); _generator.init(128,secureRandom); return _generator.generateKey(); } catch (Exception e) { throw new RuntimeException(""); } } private static byte[] decryptData_AES(byte[] content, String password) { try { SecretKey secretKey = getKey(password); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] result = cipher.doFinal(content); return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } return null; } }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 咨询一个PYTHON的问题
- ¥15 机器学习建模调参,roc评价指标
- ¥15 RCS plot 包内置数据集使用时报错,如何解决?
- ¥15 keil+mspm0g3507+二维总线舵机
- ¥15 如何用wireshark分析找出url接口和param参数
- ¥15 有谁知道这是阿里云那个应用的域名吗,怎么调用?
- ¥30 正则表达式的一些问题
- ¥15 C#如何使用不需要安装 Microsoft Excel 的机器上的方法或者库实现:将指定Excel区域导出为图片(例如A1:AO50)
- ¥15 虚拟机只能接收不能发送
- ¥15 为什么echarts极坐标柱形图的图形显示的特别小呢