C#中使用DES HMACSHA1 SHA1 RC2 MD5 进行字符串加密的例程

衣明志 发表于 , 阅读 (18108)

以下分别是使用了不同的加密方法加密/解密字符串:DES HMACSHA1 SHA1 RC2 MD5 等

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace Crypt
{
    class Crypt
    {

        [STAThread]
        public static void Main(string[] args)
        {
            string Line;
            string Key;
            string IV;
            string Text;
            Console.WriteLine("Input DES,SHA1 key:");
            Key = Console.ReadLine() + " ";
            Key = Key.Substring(0, 8);
            Console.WriteLine("Input DES IV:");
            IV = Console.ReadLine() + " ";
            IV = Key.Substring(0, 8);
            Console.WriteLine("Input text:");
            Text = Console.ReadLine();
            Line = Crypt.DESEncrypt(Text, Key, IV);
            //DES Crypt 

            Console.WriteLine("DES Crypt:{0}", Line);
            //MD5 Crypt 

            Console.WriteLine("MD5 Crypt:{0}", Crypt.MD5Encrypt(Text));
            //HMACSHA1 Crypt 

            Console.WriteLine("HMACSHA1 Crypt:{0}", Crypt.HMACSHA1Encrypt(Text, Key));
            //SHA1 Crypt 

            Console.WriteLine("SHA1 Crypt:{0}", Crypt.SHA1Encrypt(Text));
            //RC2 Crypt 

            RC2 myRC2 = new RC2(Text);
            Console.WriteLine("RC2 Crypt:{0}", myRC2.RC2Encrypt());
            /* 
            Start decrypt 
            */
            Console.WriteLine("Press any key to decrypt.");
            Console.ReadLine();
            //DES decrypt 

            Console.WriteLine("DES decrypt:{0}", Crypt.DESDecrypt(Line, Key, IV));
            //RC2 decrypt 

            Console.WriteLine("RC2 decrypt:{0}", myRC2.RC2Decrypt());
            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }

        class RC2
        {
            private byte[] Key;
            private byte[] IV;
            private byte[] orgText;
            private byte[] encryptText;
            private byte[] decryptText;
            public RC2(string rc2Text)
            {
                orgText = Encoding.Default.GetBytes(rc2Text);
                RC2CryptoServiceProvider myRC2 = new RC2CryptoServiceProvider();
                myRC2.GenerateIV();
                myRC2.GenerateKey();
                Key = myRC2.Key;
                IV = myRC2.IV;
            }
            public string RC2Encrypt()
            {
                RC2CryptoServiceProvider myRC2 = new RC2CryptoServiceProvider();
                ICryptoTransform myCryptoTrans = myRC2.CreateEncryptor(Key, IV);
                MemoryStream MStream = new MemoryStream();
                CryptoStream CStream = new CryptoStream(MStream, myCryptoTrans, CryptoStreamMode.Write);
                CStream.Write(orgText, 0, orgText.Length);
                CStream.FlushFinalBlock();
                StringBuilder EnText = new StringBuilder();
                encryptText = MStream.ToArray();
                foreach (byte Byte in encryptText)
                {
                    EnText.AppendFormat("{0:x2}", Byte);
                }
                CStream.Close();
                return EnText.ToString();
            }
            public string RC2Decrypt()
            {
                RC2CryptoServiceProvider myRC2 = new RC2CryptoServiceProvider();
                ICryptoTransform myCryptoTrans = myRC2.CreateDecryptor(Key, IV);
                MemoryStream MStream = new MemoryStream(encryptText);
                CryptoStream CStream = new CryptoStream(MStream, myCryptoTrans, CryptoStreamMode.Read);
                decryptText = new byte[encryptText.Length];
                CStream.Read(decryptText, 0, decryptText.Length);
                StringBuilder EnText = new StringBuilder();
                CStream.Close();
                ASCIIEncoding myText = new ASCIIEncoding();
                return myText.GetString(decryptText);
            }
        }
        public static string SHA1Encrypt(string EncryptText)
        {
            byte[] StrRes = Encoding.Default.GetBytes(EncryptText);
            HashAlgorithm mySHA = new SHA1CryptoServiceProvider();
            StrRes = mySHA.ComputeHash(StrRes);
            StringBuilder EnText = new StringBuilder();
            foreach (byte Byte in StrRes)
            {
                EnText.AppendFormat("{0:x2}", Byte);
            }
            return EnText.ToString();
        }
        public static string HMACSHA1Encrypt(string EncryptText, string EncryptKey)
        {
            byte[] StrRes = Encoding.Default.GetBytes(EncryptText);
            HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.Default.GetBytes(EncryptKey));
            CryptoStream CStream = new CryptoStream(Stream.Null, myHMACSHA1, CryptoStreamMode.Write);
            CStream.Write(StrRes, 0, StrRes.Length);
            StringBuilder EnText = new StringBuilder();
            foreach (byte Byte in StrRes)
            {
                EnText.AppendFormat("{0:x2}", Byte);
            }
            return EnText.ToString();
        }
        public static string MD5Encrypt(string CryptText)
        {
            MD5 myMD5 = new MD5CryptoServiceProvider();
            byte[] HashCode;
            HashCode = Encoding.Default.GetBytes(CryptText);
            HashCode = myMD5.ComputeHash(HashCode);
            StringBuilder EnText = new StringBuilder();
            foreach (byte Byte in HashCode)
            {
                EnText.AppendFormat("{0:x2}", Byte);
            }
            return EnText.ToString();
        }
        public static string DESEncrypt(string CryptText, string CryptKey, string CryptIV)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] textOut = Encoding.Default.GetBytes(CryptText);
            byte[] DESKey = ASCIIEncoding.ASCII.GetBytes(CryptKey);
            byte[] DESIV = ASCIIEncoding.ASCII.GetBytes(CryptKey);
            MemoryStream MStream = new MemoryStream();
            CryptoStream CStream = new CryptoStream(MStream, des.CreateEncryptor(DESKey, DESIV), CryptoStreamMode.Write);
            CStream.Write(textOut, 0, textOut.Length);
            CStream.FlushFinalBlock();
            StringBuilder StrRes = new StringBuilder();
            foreach (byte Byte in MStream.ToArray())
            {
                StrRes.AppendFormat("{0:x2}", Byte);
            }
            return StrRes.ToString();
        }

        public static string DESDecrypt(string CryptText, string CryptKey, string CryptIV)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] textOut = new byte[CryptText.Length / 2];
            for (int Count = 0; Count < CryptText.Length; Count += 2)
            {
                textOut[Count / 2] = (byte)(Convert.ToInt32(CryptText.Substring(Count, 2), 16));
            }
            byte[] DESKey = ASCIIEncoding.ASCII.GetBytes(CryptKey);
            byte[] DESIV = ASCIIEncoding.ASCII.GetBytes(CryptIV);
            MemoryStream MStream = new MemoryStream();
            CryptoStream CStream = new CryptoStream(MStream, des.CreateDecryptor(DESKey, DESIV), CryptoStreamMode.Write);
            CStream.Write(textOut, 0, textOut.Length);
            CStream.FlushFinalBlock();
            return System.Text.Encoding.Default.GetString(MStream.ToArray());
        }
    }
}

调试结果:

Input DES,SHA1 key: 
hello 
Input DES IV: 
world 
Input text: 
I am a boy my name is DexiangWu 
DES Crypt:9e701e4e8f55714bc68253d2ddf53fb6fd6a64df7f6b78f9491a64df18113642 
MD5 Crypt:f7d88e861602d2e1e0992dd70967314e 
HMACSHA1 Crypt:4920616d206120626f79206d79206e616d652069732044657869616e675775 
SHA1 Crypt:d5b25333f3e01ba96ac281e111af08701a7ccfb0 
RC2 Crypt:82cc5c822272a721db753e15d918c0d20e62322316fdd4a4d881b8eb03ff55e3 
Press any key to decrypt. 

DES decrypt:I am a boy my name is DexiangWu 
RC2 decrypt:I am a boy my name is DexiangWu