1:MD5

以前在asp时代常用的MD5算法好象是从动网流出来的,后来大家都用它,基本上有两种 ,区别在md5.asp的结尾部分

MD5 
= LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))
 
MD5
=LCase(WordToHex(b) & WordToHex(c))  

分别对应32位和16位加密方式

在C#中对应的实现为

/// <summary>
       
/// 16位MD5加密方法,以前的DVBBS所使用
       
/// </summary>
       
/// <param name="strSource">待加密字串</param>
        
/// <returns>加密后的字串</returns>

        public string MD5Encrypt(string strSource)
        
{
            
return MD5Encrypt(strSource, 16);
        }

        
/// <summary>
        
/// MD5加密,和动网上的16/32位MD5加密结果相同
        
/// </summary>
        
/// <param name="strSource">待加密字串</param>
        
/// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
        
/// <returns>加密后的字串</returns>

        public string MD5Encrypt(string strSource, int length)
        
{
            
byte[] bytes = Encoding.ASCII.GetBytes(strSource);
            
byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
            StringBuilder sb 
= new StringBuilder();
            
switch (length)
            
{
                
case 16:
                    
for (int i = 4; i < 12; i++)
                        sb.Append(hashValue[i].ToString(
"x2"));
                    
break;
                
case 32:
                    
for (int i = 0; i < 16; i++)
                    
{
                        sb.Append(hashValue[i].ToString(
"x2"));
                    }

                    
break;
                
default:
                    
for (int i = 0; i < hashValue.Length; i++)
                    
{
                        sb.Append(hashValue[i].ToString(
"x2"));
                    }

                    
break;
            }


同样的其它语言都实现了DES加密与.net framework的des基础实现也不一样,比较郁闷的是我刚开始使用.net framework时还真的改写过perl版的des,后面才发现其实有更简单的办法,因为网上流传的perl
/c/java版的des算法都是块加密的,设置CipherMode为ECB就好了,郁闷ing.

源代码如下

        
public static byte[] DESKey = new byte[] {0x820xBC0xA10x6A0xF50x870x3B0xE60x590x6A0x320x640x7F0x3A0x2A0xBB0x2B0x680xE20x5F0x060xFB0xB80x2D0x670xB30x550x190x4E0xB80xBF0xDD };
        
/// <summary>
        
/// DES加密
        
/// </summary>
        
/// <param name="strSource">待加密字串</param>
        
/// <param name="key">32位Key值</param>
        
/// <returns>加密后的字符串</returns>

        public string DESEncrypt(string strSource) {
            
return DESEncrypt(strSource, DESKey);
        }

        
public string DESEncrypt(string strSource,byte[] key)
        
{
            SymmetricAlgorithm sa 
= Rijndael.Create();
            sa.Key 
= key;
            sa.Mode
= CipherMode.ECB;
            sa.Padding 
= PaddingMode.Zeros;
            MemoryStream ms 
= new MemoryStream();
            CryptoStream cs 
= new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
            
byte[] byt = Encoding.Unicode.GetBytes(strSource);
            cs.Write(byt, 
0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            
return Convert.ToBase64String(ms.ToArray());
        }

        
/// <summary>
        
/// DES解密
        
/// </summary>
        
/// <param name="strSource">待解密的字串</param>
        
/// <param name="key">32位Key值</param>
        
/// <returns>解密后的字符串</returns>

        public string DESDecrypt(string strSource) {
            
return DESDecrypt(strSource, DESKey);
        }

        
public string DESDecrypt(string strSource,byte[] key)
        
{
            SymmetricAlgorithm sa 
= Rijndael.Create();
            sa.Key 
= key;
            sa.Mode 
= CipherMode.ECB;
            sa.Padding 
= PaddingMode.Zeros;
            ICryptoTransform ct 
= sa.CreateDecryptor();
            
byte[] byt = Convert.FromBase64String(strSource);
            MemoryStream ms 
= new MemoryStream(byt);
            CryptoStream cs 
= new CryptoStream(ms, ct, CryptoStreamMode.Read);
            StreamReader sr 
= new StreamReader(cs, Encoding.Unicode);
            
return sr.ReadToEnd();
        }



posted on 2007-08-24 17:37  ipusr  阅读(426)  评论(0编辑  收藏  举报