Java和C#使用DES加密算法的代码不一样,比如,C#的是key和IV,而Java的是salt和key,真的搞不清楚是怎么回事。DES算法不是语言无关性的吗?怎么样统一两者,使得C#加密的字符串Java能解密,反之亦然。请高手们不吝赐教,最好能给能给一些例子源代码。谢谢!!!

解决方案 »

  1.   

    private static byte [ ] DESKey = new byte [ ] {70 , 0x35 , 50 , 0x42 , 0x31 , 0x38 , 0x36 , 70} ;
    private static byte [ ] DESIV = new byte [ ] {70 , 0x35 , 50 , 0x42 , 0x31 , 0x38 , 0x36 , 70} //加密方法  
    /// <summary>
    /// 
    /// </summary>
    /// <param name="pToEncrypt"></param>
    /// <returns></returns>
    public string Encrypt ( string pToEncrypt )
    {
    DESCryptoServiceProvider des = new DESCryptoServiceProvider ( ) ;

    byte [ ] inputByteArray = Encoding.Default.GetBytes ( pToEncrypt ) ;
    des.Key = DESKey ;
    des.IV = DESIV ;
    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 ( ) ;
    } ///解密方法
    /// <summary>
    /// 
    /// </summary>
    /// <param name="pToDecrypt"></param>
    /// <returns></returns>
    public string Decrypt ( string pToDecrypt)
    {
    DESCryptoServiceProvider des = new DESCryptoServiceProvider ( ) ;

    byte [ ] inputByteArray = new byte[pToDecrypt.Length / 2] ;
    for ( int x = 0 ; x < pToDecrypt.Length / 2 ; x++ )
    {
    int i = ( Convert.ToInt32 ( pToDecrypt.Substring ( x * 2 , 2 ) , 16 ) ) ;
    inputByteArray [ x ] = ( byte ) i ;
    }

    des.Key = DESKey ;
    des.IV = DESIV ;
    MemoryStream ms = new MemoryStream ( ) ;
    CryptoStream cs = new CryptoStream ( ms , des.CreateDecryptor ( ) , CryptoStreamMode.Write ) ;

    cs.Write ( inputByteArray , 0 , inputByteArray.Length ) ;
    cs.FlushFinalBlock ( ) ;
    return Encoding.Default.GetString ( ms.ToArray ( ) ) ;
    }
      

  2.   

    to DES算法不是语言无关性的吗?好像不是,虽说是DES算法,实现有可能不尽完全相同
      

  3.   


    我曾经想过使用C#的MD5算法来替代Java的MD5算法!失败,加密出来的字符串结构一样,但就是不相等!
    累啊!放弃了!
      

  4.   

    这是我写的加密解密,希望对你有帮助
    public string Enstring(string thisString) 

    string encrypted = ""; 
    byte[] code = ASCIIEncoding.Unicode.GetBytes(thisString); 
    encrypted = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(code, 0, code.Length)); 
    return encrypted; 
    }  public string Destring(string thisString) 

    string encrypted = ""; 
    byte[] code = Convert.FromBase64String(thisString);
    encrypted = ASCIIEncoding.Unicode.GetString(des.CreateDecryptor().TransformFinalBlock(code, 0, code.Length)); 
    return encrypted; 
    }
      

  5.   

    我曾经想过使用C#的MD5算法来替代Java的MD5算法!失败,加密出来的字符串结构一样,但就是不相等!
    累啊!放弃了!
    =================
    不可能,c#加密的md5和任何一种md5加密工具加密出来的都是一样的,我试过的。
      

  6.   

    你要解密的话必须要知道加密时候的密钥和偏移量 
    给你一个比较完整的C# DES 加密解密代码
    //pToEncrypt为需要加密字符串,sKey为密钥 
         public string Encrypt(string pToEncrypt, string sKey)  
             { 
                  DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
                  //把字符串放到byte数组中 
              
                  byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); 
      
                  //建立加密对象的密钥和偏移量 
                  //使得输入密码必须输入英文文本 
                  des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 
                  des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 
                  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(); 
             } 
      
    //pToDecrypt为需要解密字符串,sKey为密钥 
             public string Decrypt(string pToDecrypt, string sKey) 
             { 
                  DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
                  byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; 
                  for(int x = 0; x < pToDecrypt.Length / 2; x++) 
                  { 
                       int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); 
                       inputByteArray[x] = (byte)i; 
                  } 
      
                  //建立加密对象的密钥和偏移量,此值重要,不能修改 
                  des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 
                  des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 
                  MemoryStream ms = new MemoryStream(); 
                  CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write); 
                  cs.Write(inputByteArray, 0, inputByteArray.Length); 
                  cs.FlushFinalBlock(); 
      
                  //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象 
                  StringBuilder ret = new StringBuilder(); 
                  return System.Text.Encoding.Default.GetString(ms.ToArray()); 
             }
      

  7.   

    NickLee.Copyto控件可以三倍DESwww.cnblogs.com/mail-ricklee可以下载,给分
      

  8.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Security.Cryptography;
    using System.IO;namespace DesFile
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox textBox4;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.Button button4;
    private System.Windows.Forms.TextBox textBox6;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.TextBox textBox7;
    private System.Windows.Forms.Label label7;
    private System.Windows.Forms.TextBox textBox8;
    private System.Windows.Forms.Label label8;
    private System.Windows.Forms.OpenFileDialog openFileDialog1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
      

  9.   

    现在CSDN不能发长帖子 要的话就给我发邮件[email protected]