加密函数:http://community.csdn.net/Expert/topic/4174/4174058.xml?temp=.7768824解密函数:
Public Function DESDecryption(ByVal cipherText As String, ByVal key As String)
        Dim Vector() As Byte = {&H12, &H44, &H16, &HEE, &H88, &H15, &HDD, &H41}
        Dim des As New DESCryptoServiceProvider
        Dim iba(cipherText.Length / 2 - 1) As Byte
        Dim x, i As Integer
        For x = 0 To cipherText.Length / 2 - 1
            i = Convert.ToInt32(cipherText.Substring(x * 2, 2), 16)
            iba(x) = i
        Next
        des.Key = CreateKey(key)
        des.IV = Vector
        Dim ms As New MemoryStream
        Dim cs As New CryptoStream(ms, des.CreateEncryptor, CryptoStreamMode.Write)
        cs.Write(iba, 0, iba.Length)
        cs.FlushFinalBlock()
        Dim ret As New System.Text.StringBuilder
        Return System.Text.Encoding.Default.GetString(ms.ToArray())
    End Function

解决方案 »

  1.   

    Return System.Text.Encoding.UTF8.GetString(ms.ToArray())
      

  2.   

    你解密的 des.Key,des.IV
    和你加密的 des.Key,des.IV一样吗?不一样解密出来结果会不同的
      

  3.   

    /// <summary>
    /// DES加密字符串
    /// </summary>
    /// <param name="strInput"></param>
    /// <param name="strKey"></param>
    /// <param name="strIV"></param>
    /// <returns></returns>
    public string EncryString(string strInput,string strKey,string strIV)
    {
    if(strKey.Length > 8)
    {
    strKey = strKey.Substring(0,8);
    }
    else if(strKey.Length < 8)
    {
    for(int i = 0; i < 3; i ++)
    {
    strKey = strKey + strKey;
    }
    strKey = strKey.Substring(0,8);
    }
    else
    {
    byte[] btIV =  System.Text.Encoding.UTF8.GetBytes(strKey);
    byte[] key = System.Text.Encoding.UTF8.GetBytes(strIV);
    DESCryptoServiceProvider  mcsp = new  DESCryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(strInput);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms,mcsp.CreateEncryptor(key,btIV),CryptoStreamMode.Write);
    cs.Write(bytValue,0,bytValue.Length);
    cs.FlushFinalBlock();
    cs.Close();

    return Convert.ToBase64String(ms.ToArray());
    }
    } /// <summary>
    /// DES解密字符串
    /// </summary>
    /// <param name="strInput"></param>
    /// <param name="strKey"></param>
    /// <param name="strIV"></param>
    /// <returns></returns>
    public string DecryString(string strInput,string strKey,string strIV)
    {
    if(strKey.Length > 8)
    {
    strKey = strKey.Substring(0,8);
    }
    else if(strKey.Length < 8)
    {
    for(int i = 0; i < 3; i ++)
    {
    strKey = strKey + strKey;
    }
    strKey = strKey.Substring(0,8);
    }
    else
    {
    byte[] btIV =  System.Text.Encoding.UTF8.GetBytes(strIV);
    byte[] key = System.Text.Encoding.UTF8.GetBytes(strKey);
    DESCryptoServiceProvider  mcsp = new  DESCryptoServiceProvider(); byte[] bytValue = Convert.FromBase64String(strInput); MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms,mcsp.CreateDecryptor(key,btIV),CryptoStreamMode.Write);
    cs.Write(bytValue,0,bytValue.Length);
    cs.FlushFinalBlock();
    cs.Close();

    return Encoding.UTF8.GetString(ms.ToArray());
    }
    }这是C#的,只要strKey和strIV一样,加密和解密结果一样
      

  4.   

    sorry
    把这个
    else
    {
    byte[] btIV =  
    ....................................
    中的else去掉
      

  5.   

    static string strKey = "12345678";
    static string strIV = "12345678";
    private Byte[] Key = Encoding.UTF8.GetBytes(strKey.Substring(0,8));
    private Byte[] IV = Encoding.UTF8.GetBytes(strIV.Substring(0,8)); //'加密函数
    public String Encrypt(String strText)
    {
    try
    {
    Byte[] inputByteArray  = Encoding.UTF8.GetBytes(strText);
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    return Convert.ToBase64String(ms.ToArray());
    }
    catch(Exception ex)
    {
    return ex.Message;
    }
    } //'解密函数
    public String Decrypt(String strText)
    { byte[] inputByteArray = new Byte[strText.Length];
    try
    {
    inputByteArray = Convert.FromBase64String(strText);
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    MemoryStream  ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(Key, IV), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    Encoding encoding = Encoding.UTF8;
    return encoding.GetString(ms.ToArray());
    }
    catch(Exception ex)
    {
    return ex.Message;
    }
    }
      

  6.   

    本来想用TripleDES的,但是不知道密匙的字符是几位,哈哈,所以就用DES啦