将16进制字符串AA02523AA1315B4D做密钥,加密 DD3C2751C735933B,最后得出来结果应该是416C15D746515C99
具体怎么实现啊C#加密16进制DES

解决方案 »

  1.   

    如果你要把字符串转成16进制的:
       public static string StringTo16(string inputString)
            {
             
                byte[] encryptedBytes = Encoding.ASCII.GetBytes(inputString);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < encryptedBytes.Length; i++)
                {
                    sb.AppendFormat("{0:x2}", encryptedBytes[i]);
                }
                return sb.ToString();
            }
      
    这个是加密和解密的
    private static byte[] key = ASCIIEncoding.ASCII.GetBytes("caikelun");
            private static byte[] iv = ASCIIEncoding.ASCII.GetBytes("12345678");     
            public static string DESEncrypt(string inputString)
            {
                MemoryStream ms = null;
                CryptoStream cs = null;
                StreamWriter sw = null;            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                try
                {
                    ms = new MemoryStream();
                    cs = new CryptoStream(ms, des.CreateEncryptor(key, iv), CryptoStreamMode.Write);
                    sw = new StreamWriter(cs);
                    sw.Write(inputString);
                    sw.Flush();
                    cs.FlushFinalBlock();
                    return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
                }
                finally
                {
                    if (sw != null) sw.Close();
                    if (cs != null) cs.Close();
                    if (ms != null) ms.Close();
                }
            }       
            public static string DESDecrypt(string inputString)
            {
                MemoryStream ms = null;
                CryptoStream cs = null;
                StreamReader sr = null;            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                try
                {
                    ms = new MemoryStream(Convert.FromBase64String(inputString));
                    cs = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Read);
                    sr = new StreamReader(cs);
                    return sr.ReadToEnd();
                }
                finally
                {
                    if (sr != null) sr.Close();
                    if (cs != null) cs.Close();
                    if (ms != null) ms.Close();
                }
            }
      

  2.   

    你在csdn下载收这个加密吧 有很多代码
      

  3.   


    public class DESEncrypt
        {
            public DESEncrypt()
            {
            }
     
            #region ========加密========
     
            /// <summary>
            /// 加密
            /// </summary>
            /// <param name="Text"></param>
            /// <returns></returns>
            public static string Encrypt(string Text)
            {
                return Encrypt(Text, "steel");
            }
            /// <summary> 
            /// 加密数据 
            /// </summary> 
            /// <param name="Text"></param> 
            /// <param name="sKey"></param> 
            /// <returns></returns> 
            public static string Encrypt(string Text, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray;
                inputByteArray = Encoding.Default.GetBytes(Text);
                des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                System.IO.MemoryStream ms = new System.IO.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);
                }
                return ret.ToString();
            }
     
            #endregion
     
            #region ========解密========
     
     
            /// <summary>
            /// 解密
            /// </summary>
            /// <param name="Text"></param>
            /// <returns></returns>
            public static string Decrypt(string Text)
            {
                return Decrypt(Text, "steel");
            }
            /// <summary> 
            /// 解密数据 
            /// </summary> 
            /// <param name="Text"></param> 
            /// <param name="sKey"></param> 
            /// <returns></returns> 
            public static string Decrypt(string Text, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                int len;
                len = Text.Length / 2;
                byte[] inputByteArray = new byte[len];
                int x, i;
                for (x = 0; x < len; x++)
                {
                    i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                    inputByteArray[x] = (byte)i;
                }
                des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Encoding.Default.GetString(ms.ToArray());
            }
            #endregion
        }