java des cbc加密 后.net去解密

咖啡加糖_ 2014-04-23 10:04:30
有这样个需求,我要把一个字符串经过descbc加密后传到,.net端去解密,

现在出现这样个问题,我在java这边加密后的和在.net加密后的结果不一样


在此问问有没有,同事会java和net的 帮忙看看,本人不懂net
java代码在二楼贴出 , net的在三楼贴出
谢谢
...全文
221 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
微笑着独行 2016-03-29
  • 打赏
  • 举报
回复
引用 6 楼 huanglin02 的回复:
引用 4 楼 jimmy609 的回复:
已经解决,是编码的问题 只要把.net的default格式做成utf-8就行
我曾经写的也是这样。就因为没有转成 utf-8.恭喜楼主成功解决呀
引用 4 楼 jimmy609 的回复:
已经解决,是编码的问题 只要把.net的default格式做成utf-8就行
二位 ,不能考虑 .net 服务端 去改动,因为对接的客户端不只是 android、ios、还有 Delphi客户端,要改了,都要动,,看看有别的办法吗
小律律 2014-04-24
  • 打赏
  • 举报
回复
引用 4 楼 jimmy609 的回复:
已经解决,是编码的问题 只要把.net的default格式做成utf-8就行
我曾经写的也是这样。就因为没有转成 utf-8.恭喜楼主成功解决呀
tony4geek 2014-04-23
  • 打赏
  • 举报
回复
估计底层实现不一样。。 自己写个加密方法吧。
咖啡加糖_ 2014-04-23
  • 打赏
  • 举报
回复
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Security.Cryptography; using System.IO; using System.Text; namespace MobileCommonPay.Tool { public class DES2 { // CBC模式 string iv = "NEMOBIlE"; string key = "NEMOBIlE"; /// <summary> /// DES加密偏移量,必须是>=8位长的字符串 /// </summary> public string IV { get { return iv; } set { iv = value; } } /// <summary> /// DES加密的私钥,必须是8位长的字符串 /// </summary> public string Key { get { return key; } set { key = value; } } /// <summary> /// 对字符串进行DES加密 /// </summary> /// <param name="sourceString">待加密的字符串</param> /// <returns>加密后的BASE64编码的字符串</returns> public string Encrypt(string sourceString) { byte[] btKey = Encoding.Default.GetBytes(key); byte[] btIV = Encoding.Default.GetBytes(iv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Encoding.Default.GetBytes(sourceString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Convert.ToBase64String(ms.ToArray()); } catch (Exception ex) { return ex.Message; } } } /// <summary> /// 对DES加密后的字符串进行解密 /// </summary> /// <param name="encryptedString">待解密的字符串</param> /// <returns>解密后的字符串</returns> public string Decrypt(string encryptedString) { byte[] btKey = Encoding.Default.GetBytes(key); byte[] btIV = Encoding.Default.GetBytes(iv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Convert.FromBase64String(encryptedString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Encoding.Default.GetString(ms.ToArray()); } catch (Exception ex) { return ex.Message; } } } /// <summary> /// 对文件内容进行DES加密 /// </summary> /// <param name="sourceFile">待加密的文件绝对路径</param> /// <param name="destFile">加密后的文件保存的绝对路径</param> public void EncryptFile(string sourceFile, string destFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile); byte[] btKey = Encoding.Default.GetBytes(key); byte[] btIV = Encoding.Default.GetBytes(iv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btFile = File.ReadAllBytes(sourceFile); using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write)) { try { using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(btFile, 0, btFile.Length); cs.FlushFinalBlock(); } } catch { throw; } finally { fs.Close(); } } } /// <summary> /// 对文件内容进行DES加密,加密后覆盖掉原来的文件 /// </summary> /// <param name="sourceFile">待加密的文件的绝对路径</param> public void EncryptFile(string sourceFile) { EncryptFile(sourceFile, sourceFile); } /// <summary> /// 对文件内容进行DES解密 /// </summary> /// <param name="sourceFile">待解密的文件绝对路径</param> /// <param name="destFile">解密后的文件保存的绝对路径</param> public void DecryptFile(string sourceFile, string destFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile); byte[] btKey = Encoding.Default.GetBytes(key); byte[] btIV = Encoding.Default.GetBytes(iv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btFile = File.ReadAllBytes(sourceFile); using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write)) { try { using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(btFile, 0, btFile.Length); cs.FlushFinalBlock(); } } catch { throw; } finally { fs.Close(); } } } /// <summary> /// 对文件内容进行DES解密,加密后覆盖掉原来的文件 /// </summary> /// <param name="sourceFile">待解密的文件的绝对路径</param> public void DecryptFile(string sourceFile) { DecryptFile(sourceFile, sourceFile); } } }
咖啡加糖_ 2014-04-23
  • 打赏
  • 举报
回复
package com.besttone.commons; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.IvParameterSpec; import sun.misc.BASE64Encoder; public class DESUtil2 { /** * * @return DES算法密钥 */ public static byte[] generateKey() { try { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 生成一个DES算法的KeyGenerator对象 KeyGenerator kg = KeyGenerator.getInstance("DES"); kg.init(sr); // 生成密钥 SecretKey secretKey = kg.generateKey(); // 获取密钥数据 byte[] key = secretKey.getEncoded(); return key; } catch (NoSuchAlgorithmException e) { System.err.println("DES算法,生成密钥出错!"); e.printStackTrace(); } return null; } /** * 加密函数 * * @param data * 加密数据 * @param key * 密钥 * @return 返回加密后的数据 */ public static byte[] encrypt(byte[] data, byte[] key) { try { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密匙工厂,然后用它把DESKeySpec转换成 // 一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(dks); // using DES in ECB mode Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // 用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr); // 执行加密操作 byte encryptedData[] = cipher.doFinal(data); return encryptedData; } catch (Exception e) { System.err.println("DES算法,加密数据出错!"); e.printStackTrace(); } return null; } /** * 解密函数 * * @param data * 解密数据 * @param key * 密钥 * @return 返回解密后的数据 */ public static byte[] decrypt(byte[] data, byte[] key) { try { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // byte rawKeyData[] = /* 用某种方法获取原始密匙数据 */; // 从原始密匙数据创建一个DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成 // 一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(dks); // using DES in ECB mode Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, secretKey, sr); // 正式执行解密操作 byte decryptedData[] = cipher.doFinal(data); return decryptedData; } catch (Exception e) { System.err.println("DES算法,解密出错。"); e.printStackTrace(); } return null; } /** * 加密函数 * * @param data * 加密数据 * @param key * 密钥 * @return 返回加密后的数据 */ public static byte[] CBCEncrypt(byte[] data, byte[] key, byte[] iv) { try { // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密匙工厂,然后用它把DESKeySpec转换成 // 一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(dks); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); // 若采用NoPadding模式,data长度必须是8的倍数 // Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding"); // 用密匙初始化Cipher对象 IvParameterSpec param = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, secretKey, param); // 执行加密操作 byte encryptedData[] = cipher.doFinal(data); return encryptedData; } catch (Exception e) { System.err.println("DES算法,加密数据出错!"); e.printStackTrace(); } return null; } /** * 解密函数 * * @param data * 解密数据 * @param key * 密钥 * @return 返回解密后的数据 */ public static byte[] CBCDecrypt(byte[] data, byte[] key, byte[] iv) { try { // 从原始密匙数据创建一个DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成 // 一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(dks); // using DES in CBC mode Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); // 若采用NoPadding模式,data长度必须是8的倍数 // Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding"); // 用密匙初始化Cipher对象 IvParameterSpec param = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, secretKey, param); // 正式执行解密操作 byte decryptedData[] = cipher.doFinal(data); return decryptedData; } catch (Exception e) { System.err.println("DES算法,解密出错。"); e.printStackTrace(); } return null; } public static void main(String[] args) { try { byte[] key = "NEMOBIlE".getBytes(); byte[] iv = "NEMOBIlE".getBytes(); byte[] data = DESUtil2.CBCEncrypt("测试商品".getBytes(), key, iv); System.out.println("加密前: 测试商品"); //加密转换 BASE64Encoder enc = new BASE64Encoder(); //加密运算之后 将byte[]转化为base64的String String encryptedtext = enc.encode(data); // System.out.println("加密后:" + data.toString()); System.out.println("加密后:" + encryptedtext); System.out.println("解密后:" + new String(DESUtil2.CBCDecrypt(data, key, iv))); } catch (Exception e) { e.printStackTrace(); } } }
咖啡加糖_ 2014-04-23
  • 打赏
  • 举报
回复
已经解决,是编码的问题 只要把.net的default格式做成utf-8就行

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧