请问各位附件的DES加解密如何实现?
输入的值是十六进制的,2个字节。
密钥也是十六进制,2个字节。
要求加密或解密后的值也是十六进制的,2个字节。

解决方案 »

  1.   

    图片似乎传不上去
    图片地址是:
    http://www.5899222.com/DES.JPG
      

  2.   


      public static void Encrypt(string pathIn, string pathOut, string password)
            {
                try
                {
                    byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                    byte[] key = System.Text.Encoding.UTF8.GetBytes(password);
                    System.IO.FileStream fs = new FileStream(pathIn, FileMode.Open, FileAccess.Read);
                    System.IO.FileStream outfs = new FileStream(pathOut, FileMode.OpenOrCreate, FileAccess.Write);
                    outfs.SetLength(0);
                    byte[] myBytes = new byte[100];
                    long myInLength = 0;
                    long myLength = fs.Length;
                    System.Security.Cryptography.DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
                    System.Security.Cryptography.CryptoStream cs = new CryptoStream(outfs, dsp.CreateEncryptor(key, iv), CryptoStreamMode.Write);
                    while (myInLength < myLength)
                    {
                        int myLen = fs.Read(myBytes, 0, 100);
                        cs.Write(myBytes, 0, myLen);
                        myInLength += myLen;
                    }
                    cs.Close();
                    fs.Close();
                    outfs.Close();
                }
                catch (IOException ioex)
                {
                    Log.debug("Encrypt(pathIn, pathOut, password): " + ioex.Message + "\r\nSource:" + ioex.Source + "\r\nStackTrace:" + ioex.StackTrace);
                }
            }        public static void Decrypt(string pathIn, string pathOut, string password)
            {
                try
                {
                    System.IO.FileStream fs = new FileStream(pathIn, FileMode.Open, FileAccess.Read);
                    System.IO.FileStream outfs = new FileStream(pathOut, FileMode.OpenOrCreate, FileAccess.Write);
                    outfs.SetLength(0);
                    long myLength = fs.Length;
                    long myInLength = 0;
                    byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                    byte[] key = System.Text.Encoding.UTF8.GetBytes(password);
                    System.Security.Cryptography.DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
                    System.Security.Cryptography.CryptoStream cs = new CryptoStream(outfs, dsp.CreateDecryptor(key, iv), CryptoStreamMode.Write);
                    byte[] myBytes = new byte[100];
                    while (myInLength < myLength)
                    {
                        int myLen = fs.Read(myBytes, 0, 100);
                        cs.Write(myBytes, 0, myLen);
                        myInLength += myLen;
                    }
                    cs.Close();
                    fs.Close();
                    outfs.Close();
                }
                catch (IOException ioex)
                {
                    Log.debug("Dncrypt(pathIn, pathOut, password): " + ioex.Message + "\r\nSource:" + ioex.Source + "\r\nStackTrace:" + ioex.StackTrace);
                }
            }把这个改改,
      

  3.   

    参考。。
    /// <summary>
    /// Encrypt the string
    /// Attention:key must be 8 bits
    /// </summary>
    /// <param name="strText">string</param>
    /// <param name="strEncrKey">key</param>
    /// <returns></returns>
    public string DesEncrypt(string strText, string strEncrKey)
    {
    byte[] byKey = null;
    byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    try
    {
    byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    return Convert.ToBase64String(ms.ToArray());
    }
    catch (System.Exception error)
    {
    // MessageBox.Show(error.Message);
    return "error:" + error.Message + "\r";
    }
    }
    /// <summary>
    /// Decrypt string
    /// Attention:key must be 8 bits
    /// </summary>
    /// <param name="strText">Decrypt string</param>
    /// <param name="sDecrKey">key</param>
    /// <returns>output string</returns>
    public string DesDecrypt(string strText, string sDecrKey)
    {
    byte[] byKey = null;
    byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    byte[] inputByteArray = new Byte[strText.Length];
    try
    {
    byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    inputByteArray = Convert.FromBase64String(strText);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    System.Text.Encoding encoding = new System.Text.UTF8Encoding();
    return encoding.GetString(ms.ToArray());
    }
    catch (System.Exception error)
    {
    // MessageBox.Show(error.Message);
    return "error:" + error.Message + "\r";
    }
    }