コード例 #1
0
        /// <summary>
        /// RC2加密
        /// </summary>
        /// <param name="plainStr">明文字符串</param>
        /// <param name="key">加密密钥支持40~128长度,可以每8位递增(5到16个长度的字符串)</param>
        /// <param name="iv">加密向量64位以上(8个字节上去字符串)</param>
        /// <param name="isBase64Code">是否是Base64编码,否则是16进制编码</param>
        /// <param name="mode">加密模式</param>
        /// <param name="padding">填充模式</param>
        /// <returns>密文</returns>
        public static string RC2Encrypt(string plainStr, string key = DefaultRC2Key, string iv = DefaultRC2IV, bool isBase64Code = true, CipherMode mode = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7)
        {
            byte[] bKey      = Encoding.UTF8.GetBytes(key);
            byte[] bIV       = Encoding.UTF8.GetBytes(iv);
            byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);

            string encrypt = null;
            var    rc2     = new RC2CryptoServiceProvider();

            try
            {
                rc2.Padding = padding;
                rc2.Mode    = mode;
                using (var mStream = new MemoryStream())
                {
                    using (var cStream = new CryptoStream(mStream, rc2.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
                    {
                        cStream.Write(byteArray, 0, byteArray.Length);
                        cStream.FlushFinalBlock();
                        if (isBase64Code)
                        {
                            encrypt = Convert.ToBase64String(mStream.ToArray());
                        }
                        else
                        {
                            encrypt = ByteStrConvertHelper.ToString(mStream.ToArray());
                        }
                    }
                }
            }
            catch { }
            rc2.Clear();

            return(encrypt);
        }
コード例 #2
0
        /// <summary>
        /// 生成16位的MD5散列值
        /// </summary>
        /// <param name="data">要算MD5的字符串</param>
        /// <returns></returns>
        public static string HashMD516(string data)
        {
            var md5 = new MD5CryptoServiceProvider();

            byte[] bytes = Encoding.UTF8.GetBytes(data);
            return(ByteStrConvertHelper.ToString(md5.ComputeHash(bytes)).Substring(8, 16));
        }
コード例 #3
0
        /// <summary>
        /// 对传入的明文密码进行Hash加密,密码不能为中文
        /// </summary>
        /// <param name="oriPassword">需要加密的明文密码</param>
        /// <returns>经过Hash加密的密码</returns>
        public static string HashPassword(string oriPassword)
        {
            if (string.IsNullOrEmpty(oriPassword))
            {
                throw new ArgumentException("oriPassword is valid");
            }

            var acii        = new ASCIIEncoding();
            var hashedBytes = Hash(acii.GetBytes(oriPassword));

            return(ByteStrConvertHelper.ToString(hashedBytes));
        }
コード例 #4
0
        /// <summary>
        /// 对字符串进行SHA1散列
        /// </summary>
        /// <param name="data">需要哈稀的字符串</param>
        /// <param name="isBase64">是否采用Base64</param>
        /// <returns>密文</returns>
        public static string HashSHA1(string data, bool isBase64 = false)
        {
            var           StrRes = Encoding.UTF8.GetBytes(data);
            HashAlgorithm iSHA   = new SHA1CryptoServiceProvider();

            StrRes = iSHA.ComputeHash(StrRes);
            if (isBase64)
            {
                return(Convert.ToBase64String(StrRes));
            }
            else
            {
                return(ByteStrConvertHelper.ToString(StrRes));
            }
        }
コード例 #5
0
        /// <summary>
        /// RSA加密
        /// </summary>
        /// <param name="plaintData">明文</param>
        /// <param name="publicKey">RSA公钥</param>
        /// <param name="isBase64">输出数据是否用Base64编码</param>
        /// <returns></returns>
        public static string RSAEncrypt(string plaintData, RSAParameters publicKey, bool isBase64 = false)
        {
            var rsa = new RSACryptoServiceProvider();

            rsa.ImportParameters(publicKey);
            var cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(plaintData), false);

            if (isBase64)
            {
                return(Convert.ToBase64String(cipherbytes));
            }
            else
            {
                return(ByteStrConvertHelper.ToString(cipherbytes));
            }
        }
コード例 #6
0
        /// <summary>
        /// 对字符串进行HmacSHA512散列
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="data">需要哈稀的字符串</param>
        /// <param name="isBase64">是否采用Base64</param>
        /// <returns>密文</returns>
        public static string HmacSHA512(string key, string data, bool isBase64 = false)
        {
            var StrRes   = Encoding.UTF8.GetBytes(data);
            var bkey     = Encoding.UTF8.GetBytes(key);
            var hmacSha1 = new HMACSHA512(bkey);

            StrRes = hmacSha1.ComputeHash(StrRes);
            if (isBase64)
            {
                return(Convert.ToBase64String(StrRes));
            }
            else
            {
                return(ByteStrConvertHelper.ToString(StrRes));
            }
        }
コード例 #7
0
        /// <summary>
        /// SHA512加密,不可逆转
        /// </summary>
        /// <param name="data">string data:被加密的字符串</param>
        /// <param name="isBase64">是否采用Base64</param>
        /// <returns>返回加密后的字符串</returns>
        public static string HashSHA512(string data, bool isBase64 = false)
        {
            SHA512 s512 = new SHA512CryptoServiceProvider();

            byte[] byte1 = Encoding.Default.GetBytes(data);
            byte1 = s512.ComputeHash(byte1);
            s512.Clear();
            if (isBase64)
            {
                return(Convert.ToBase64String(byte1));
            }
            else
            {
                return(ByteStrConvertHelper.ToString(byte1));
            }
        }
コード例 #8
0
        /// <summary>
        /// SHA384加密,不可逆转
        /// </summary>
        /// <param name="data">string data:被加密的字符串</param>
        /// <param name="isBase64">是否采用Base64</param>
        /// <returns>返回加密后的字符串</returns>
        public static string HashSHA384(string data, bool isBase64 = false)
        {
            SHA384 s384 = new SHA384CryptoServiceProvider();

            byte[] byte1 = Encoding.UTF8.GetBytes(data);
            byte1 = s384.ComputeHash(byte1);
            s384.Clear();
            if (isBase64)
            {
                return(Convert.ToBase64String(byte1));
            }
            else
            {
                return(ByteStrConvertHelper.ToString(byte1));
            }
        }
コード例 #9
0
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="plainbytes">明文字节数组</param>
        /// <param name="key">加密密钥支持128(16字节)、192(24字节)、256位(32字节)的key</param>
        /// <param name="iv">加密向量(16到19字节)</param>
        /// <param name="isBase64Code">是否是Base64编码,否则是16进制编码</param>
        /// <param name="mode">加密模式</param>
        /// <param name="padding">填充模式</param>
        /// <returns>密文</returns>
        public static string AESEncrypt(byte[] plainbytes, string key = DefaultAESKey, string iv = DefaultAESIV, bool isBase64Code = true, CipherMode mode = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7)
        {
            byte[] bKey = Encoding.UTF8.GetBytes(key);
            byte[] bIV  = Encoding.UTF8.GetBytes(iv);

            var encrypts = AESEncrypt(plainbytes, key, iv, mode, padding);

            if (isBase64Code)
            {
                return(Convert.ToBase64String(encrypts));
            }
            else
            {
                return(ByteStrConvertHelper.ToString(encrypts));
            }
        }
コード例 #10
0
        /// <summary>
        /// RSA解密
        /// </summary>
        /// <param name="encryptData">密文</param>
        /// <param name="privateKey">RSA私钥</param>
        /// <param name="isBase64">密文数据是否用Base64编码</param>
        /// <returns></returns>
        public static string RSADecrypt(string encryptData, string privateKey = DefaultRSAPublicKey, bool isBase64 = false)
        {
            byte[] bData;
            if (isBase64)
            {
                bData = Convert.FromBase64String(encryptData);
            }
            else
            {
                bData = ByteStrConvertHelper.ToBytes(encryptData);
            }
            var rsa = new RSACryptoServiceProvider();

            rsa.FromXmlString(privateKey);
            var cipherbytes = rsa.Decrypt(bData, false);

            return(Encoding.UTF8.GetString(cipherbytes));
        }
コード例 #11
0
        /// <summary>
        /// 计算MD5
        /// </summary>
        /// <param name="data">要算MD5的字符串</param>
        /// <param name="isBase64Code">是否是Base64编码</param>
        /// <returns>MD5字符串</returns>
        public static string HashMD5(string data, bool isBase64Code = false)
        {
            if (string.IsNullOrEmpty(data))
            {
                return("");
            }
            var md5 = new MD5CryptoServiceProvider();

            byte[] bytes = Encoding.UTF8.GetBytes(data);
            bytes = md5.ComputeHash(bytes);
            md5.Clear();
            if (isBase64Code)
            {
                return(Convert.ToBase64String(bytes));
            }
            else
            {
                return(ByteStrConvertHelper.ToString(bytes));
            }
        }
コード例 #12
0
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="encryptStr">密文字符串</param>
        /// <param name="key">加密密钥支持128(16字节)、192(24字节)、256位(32字节)的key</param>
        /// <param name="iv">加密向量(16到19字节)</param>
        /// <param name="isBase64Code">是否是Base64编码,否则是16进制编码</param>
        /// <param name="mode">加密模式</param>
        /// <param name="padding">填充模式</param>
        /// <returns>明文字节数组</returns>
        public static byte[] AESDecryptToBytes(string encryptStr, string key = DefaultAESKey, string iv = DefaultAESIV, bool isBase64Code = true, CipherMode mode = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7)
        {
            byte[] bKey = Encoding.UTF8.GetBytes(key);
            byte[] bIV  = Encoding.UTF8.GetBytes(iv);
            byte[] byteArray;
            if (isBase64Code)
            {
                byteArray = Convert.FromBase64String(encryptStr);
            }
            else
            {
                byteArray = ByteStrConvertHelper.ToBytes(encryptStr);
            }

            byte[] decrypt = null;
            var    aes     = Rijndael.Create();

            try
            {
                aes.Mode    = mode;
                aes.Padding = padding;
                using (var mStream = new MemoryStream())
                {
                    using (var cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
                    {
                        cStream.Write(byteArray, 0, byteArray.Length);
                        cStream.FlushFinalBlock();
                    }
                    decrypt = mStream.ToArray();
                }
            }
            catch { }
            aes.Clear();

            return(decrypt);
        }
コード例 #13
0
        /// <summary>
        /// 使用指定的128字节的密钥对8字节数组进行3Des解密
        /// </summary>
        /// <param name="encryptStr">密文字符串</param>
        /// <param name="key">密钥长度,可以为128(16字节),或是192(24字节)</param>
        /// <param name="iv">加密向量长度64位以上(8个字节以上)</param>
        /// <param name="isBase64Code">是否是Base64编码,否则是16进制编码</param>
        /// <param name="mode">加密模式</param>
        /// <param name="padding">填充模式</param>
        /// <returns>已解密的字符串</returns>
        public static string TripleDESDecrypt(string encryptStr, string key = Default3DESKey, string iv = DefaultDESKey, bool isBase64Code = true, CipherMode mode = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7)
        {
            byte[] bKey = Encoding.UTF8.GetBytes(key);
            byte[] bIV  = Encoding.UTF8.GetBytes(iv);
            byte[] byteArray;
            if (isBase64Code)
            {
                byteArray = Convert.FromBase64String(encryptStr);
            }
            else
            {
                byteArray = ByteStrConvertHelper.ToBytes(encryptStr);
            }

            string decrypt = null;
            var    tdsc    = new TripleDESCryptoServiceProvider();

            try
            {
                tdsc.Mode    = mode;
                tdsc.Padding = padding;
                using (var mStream = new MemoryStream())
                {
                    using (var cStream = new CryptoStream(mStream, tdsc.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
                    {
                        cStream.Write(byteArray, 0, byteArray.Length);
                        cStream.FlushFinalBlock();
                        decrypt = Encoding.UTF8.GetString(mStream.ToArray());
                    }
                }
            }
            catch { }
            tdsc.Clear();

            return(decrypt);
        }