CryptoHelper


namespace Microshaoft
{
    using System;
    using System.Security.Cryptography;
    using System.Text;
    using System.IO;
    class Class1
    {
        static void Main()
        {
            //RSA
            UTF8Encoding e = new UTF8Encoding();
            byte[] data = e.GetBytes("测试@microshaoft.com");
            byte[] encryptedData;
            //byte[] decryptedData;
            RSACryptoServiceProvider x = new RSACryptoServiceProvider();
            string privateKey = x.ToXmlString(true);
            Console.WriteLine("RSA Private Key: {0}", privateKey);
            string publicKey = x.ToXmlString(false);
            Console.WriteLine("RSA Public Key: {0}", publicKey);
            //公钥加密
            encryptedData = CryptoHelper.RSAEncrypt(data, publicKey, false);
            //私钥解密
            data = CryptoHelper.RSADecrypt(encryptedData, privateKey, false);
            Console.WriteLine("RSA Decrypted plaintext: {0}", e.GetString(data));
            //私钥签名
            byte[] signature = CryptoHelper.RSASignSHA1(data, privateKey);
            //公钥验签
            Console.WriteLine(CryptoHelper.RSAVerifySHA1(data, publicKey, signature));
            //TripleDES
            string key = "000111222333444555666777888999aaabbbcccdddeeefff";//48
            key = "012345678901234567890123456789012345678901234567";
            key = "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef";
            key = "0123456789abcdef01111111111111111111111111111110";
            string iv = "0123456789abcdef";//16
            iv = "0000000000000000";
            data = e.GetBytes("测试@microshaoft.com");
            data = CryptoHelper.TripleDESEncrypt
                                            (
                                                data,
                                                CryptoHelper.HexStringToBytesArray(key),
                                                CryptoHelper.HexStringToBytesArray(iv)
                                            );
            data = CryptoHelper.TripleDESDecrypt
                                            (
                                                data,
                                                CryptoHelper.HexStringToBytesArray(key),
                                                CryptoHelper.HexStringToBytesArray(iv)
                                            );
            Console.WriteLine("3DES Decrypted plaintext: {0}", e.GetString(data));
            Console.WriteLine(Environment.Version.ToString());
        }
    }
    public class CryptoHelper
    {
        public static byte[] RSASignSHA1
                                (
                                    byte[] data
                                    , string privateKey
                                )
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            return RSASignSHA1
                        (
                            provider
                            , data
                            , privateKey
                        );
        }
        public static byte[] RSASignSHA1
                (
                    RSACryptoServiceProvider provider
                    , byte[] data
                    , string privateKey
                )
        {
            provider.FromXmlString(privateKey);
            return provider.SignHash
                                (
                                    ComputeSHA1(data)
                                    , "SHA1"
                                );
        }
        public static bool RSAVerifySHA1
                                (
                                    byte[] data
                                    , string publicKey
                                    , byte[] signature
                                )
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            return RSAVerifySHA1
                            (
                                provider
                                , data
                                , publicKey
                                , signature
                            );
        }
        public static bool RSAVerifySHA1
                                (
                                    RSACryptoServiceProvider provider
                                    , byte[] data
                                    , string publicKey
                                    , byte[] signature
                                )
        {
            provider.FromXmlString(publicKey);
            return provider.VerifyHash
                                (
                                    ComputeSHA1(data)
                                    , "SHA1"
                                    , signature
                                );
        }
        public static byte[] RSASignMD5
                                (
                                    byte[] data
                                    , string privateKey
                                )
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            return RSASignMD5
                        (
                            provider
                            , data
                            , privateKey
                        );
        }
        public static byte[] RSASignMD5
                                (
                                    RSACryptoServiceProvider provider
                                    , byte[] data
                                    , string privateKey
                                )
        {
            provider.FromXmlString(privateKey);
            return provider.SignHash
                        (
                            ComputeMD5(data)
                            , "MD5"
                        );
        }
        public static bool RSAVerifyMD5
                                (
                                    byte[] data
                                    , string publicKey
                                    , byte[] signature
                                )
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            return RSAVerifyMD5
                                (
                                    provider
                                    , data
                                    , publicKey
                                    , signature
                                );
        }
        public static bool RSAVerifyMD5
                                (
                                    RSACryptoServiceProvider provider
                                    , byte[] data
                                    , string publicKey
                                    , byte[] signature
                                )
        {
            provider.FromXmlString(publicKey);
            return provider.VerifyHash
                                (
                                    ComputeMD5(data)
                                    , "MD5"
                                    , signature
                                );
        }
        public static byte[] RSAEncrypt
                                (
                                    byte[] data
                                    , string publicKey
                                    , bool DoOAEPPadding
                                )
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            return RSAEncrypt
                        (
                            provider,
                            data,
                            publicKey,
                            DoOAEPPadding
                        );
        }
        public static byte[] RSAEncrypt
                        (
                            RSACryptoServiceProvider provider
                            , byte[] data
                            , string publicKey
                            , bool DoOAEPPadding
                        )
        {
            provider.FromXmlString(publicKey);
            return provider.Encrypt(data, DoOAEPPadding);
        }
        public static byte[] RSADecrypt
                                (
                                    byte[] data
                                    , string privateKey
                                    , bool DoOAEPPadding
                                )
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            return RSADecrypt
                        (
                            provider,
                            data,
                            privateKey,
                            DoOAEPPadding
                        );
        }
        public static byte[] RSADecrypt
                        (
                            RSACryptoServiceProvider provider
                            , byte[] data
                            , string privateKey
                            , bool DoOAEPPadding
                        )
        {
            provider.FromXmlString(privateKey);
            return provider.Decrypt(data, DoOAEPPadding);
        }
        public static byte[] TripleDESDecrypt
                                        (
                                            byte[] data
                                            , byte[] Key
                                            , byte[] IV
                                        )
        {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            des.Key = Key;
            des.IV = IV;
            return des.CreateDecryptor().TransformFinalBlock(data, 0, data.Length);
        }
        public static byte[] TripleDESDecrypt
                                        (
                                            string text
                                            , string HexStringKey
                                            , string HexStringIV
                                        )
        {
            return TripleDESDecrypt
                            (
                                HexStringToBytesArray(text)
                                , HexStringToBytesArray(HexStringKey)
                                , HexStringToBytesArray(HexStringIV)
                            );
        }
        public static byte[] TripleDESDecrypt
                                    (
                                        string text
                                        , byte[] Key
                                        , byte[] IV
                                    )
        {
            return TripleDESDecrypt
                            (
                                HexStringToBytesArray(text)
                                , Key
                                , IV
                            );
        }
        public static string TripleDESDecrypt
                                    (
                                        string text
                                        , string HexStringKey
                                        , string HexStringIV
                                        , Encoding e //原文的encoding
                                    )
        {
            return e.GetString
                (
                    TripleDESDecrypt
                        (
                            text
                            , HexStringKey
                            , HexStringIV
                        )
                );
        }
        public static string TripleDESDecrypt
                                    (
                                        string text
                                        , byte[] Key
                                        , byte[] IV
                                        , Encoding e //原文的encoding
                                    )
        {
            return e.GetString
                        (
                            TripleDESDecrypt
                                (
                                    text
                                    , Key
                                    , IV
                                )
                        );
        }
        public static string GenerateTripleDESHexStringKey()
        {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            des.GenerateKey();
            return BytesArrayToHexString(des.Key);
        }
        public static string GenerateTripleDESHexStringIV()
        {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            des.GenerateIV();
            return BytesArrayToHexString(des.IV);
        }
        public static byte[] TripleDESEncrypt
                                        (
                                            byte[] data
                                            , byte[] Key
                                            , byte[] IV
                                        )
        {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            des.Key = Key;
            des.IV = IV;
            return des.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);
        }
        public static byte[] TripleDESEncrypt
                                        (
                                            string text
                                            , Encoding e
                                            , byte[] Key
                                            , byte[] IV
                                        )
        {
            return TripleDESEncrypt
                            (
                                e.GetBytes(text)
                                , Key
                                , IV
                            );
        }
        public static byte[] TripleDESEncrypt
                                        (
                                            string text
                                            , Encoding e
                                            , string HexStringKey
                                            , string HexStringIV
                                        )
        {
            return TripleDESEncrypt
                            (
                                text
                                , e
                                , HexStringToBytesArray(HexStringKey)
                                , HexStringToBytesArray(HexStringIV)
                            );
        }
        public static byte[] ComputeSHA1(byte[] data)
        {
            return new SHA1CryptoServiceProvider().ComputeHash(data);
        }
        public static byte[] ComputeSHA1(string text, Encoding e)
        {
            return ComputeSHA1(e.GetBytes(text));
        }
        public static byte[] ComputeSHA1(string text)
        {
            return ComputeSHA1(text, Encoding.UTF8);
        }
        public static byte[] ComputeSHA1(Stream stream)
        {
            return new SHA1CryptoServiceProvider().ComputeHash(stream);
        }
        public static byte[] ComputeMD5(byte[] data)
        {
            return new MD5CryptoServiceProvider().ComputeHash(data, 0, data.Length);
        }
        public static byte[] ComputeMD5(string text, Encoding e)
        {
            return ComputeMD5(e.GetBytes(text));
        }
        public static byte[] ComputeMD5(string text)
        {
            return ComputeMD5(text, Encoding.UTF8);
        }
        public static byte[] ComputeMD5(Stream stream)
        {
            return new MD5CryptoServiceProvider().ComputeHash(stream);
        }
        public static string BytesArrayToHexString(byte[] data)
        {
            return BitConverter.ToString(data).Replace("-", "");
        }
        public static byte[] HexStringToBytesArray(string text)
        {
            text = text.Replace(" ", "");
            int l = text.Length;
            byte[] buffer = new byte[l / 2];
            for (int i = 0; i < l; i += 2)
            {
                buffer[i / 2] = Convert.ToByte(text.Substring(i, 2), 16);
            }
            return buffer;
        }
    }
}

posted @ 2008-07-21 13:05  于斯人也  阅读(12845)  评论(0编辑  收藏  举报