要求是通过key加密,加密结果要位数定长

解决方案 »

  1.   

    试了一下这段代码,结果不是定长的public class MD5
        {
            private MD5()
            {
                //
                // TODO: 在此处添加构造函数逻辑
                //
            }        public static byte[] getbyte(string str)
            {
                return Encoding.Default.GetBytes(str);
            }        ///md5加密解密
            /// <param name="strkey"> 密钥不限定位数</param>
            public static string md5Encrypt(String plainText, string strkey)
            {
                if (plainText.Trim() == "") return "";
                string encrypted = null;
                byte[] key = getbyte(strkey);
                try
                {
                    byte[] inputBytes = ASCIIEncoding.ASCII.GetBytes(plainText);
                    byte[] pwdhash = null;
                    MD5CryptoServiceProvider hashmd5;
                    //generate an MD5 hash from the password. 
                    //a hash is a one way encryption meaning once you generate
                    //the hash, you cant derive the password back from it.
                    hashmd5 = new MD5CryptoServiceProvider();
                    pwdhash = hashmd5.ComputeHash(key);
                    hashmd5 = null;
                    // Create a new TripleDES service provider 
                    TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
                    tdesProvider.Key = pwdhash;
                    tdesProvider.Mode = CipherMode.ECB;                encrypted = Convert.ToBase64String(
                    tdesProvider.CreateEncryptor().TransformFinalBlock(inputBytes, 0, inputBytes.Length));
                }
                catch (Exception e)
                {
                    string str = e.Message;
                    throw;
                }
                return encrypted;
            }    }
      

  2.   

    md5是定长的,只是你后来又使用md5 key做了其他加密的密匙,返回的结果已经不是md5了