• 基础类库积累--Decrypt类


    前言:

    书承上文,上回说到如何做加密,那我们在讲讲如何解密如何?

             /// <summary>
            /// DES解密
            /// </summary>
            /// <param name="value">需要解密的字符串(密文)</param>
            /// <returns>解密之后的字符串(明文)</returns>
            public static string DesDecrypt(string value)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    
                byte[] inputByteArray = new byte[value.Length / 2];
                for (int x = 0; x < value.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(value.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }
    
                string key = "s1u&n*dy";
    
                des.Key = ASCIIEncoding.ASCII.GetBytes(key);
                des.IV = ASCIIEncoding.ASCII.GetBytes(key);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
    
                return Encoding.UTF8.GetString(ms.ToArray());
            }
    
            /// <summary>
            ///  MD5解密
            /// </summary>
            /// <param name="pToDecrypt"></param>
            /// <param name="sKey"></param>
            /// <returns></returns>
            public static string MD5Decrypt(string pToDecrypt, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    
                byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }
    
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
    
                StringBuilder ret = new StringBuilder();
    
                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
    
            /// <summary>
            /// Base64解密
            /// </summary>
            /// <param name="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param>
            /// <param name="result">待解密的密文</param>
            /// <returns>解密后的字符串</returns>
            public static string DecodeBase64(Encoding encode, string result)
            {
                string decode = "";
                byte[] bytes = Convert.FromBase64String(result);
                try
                {
                    decode = encode.GetString(bytes);
                }
                catch
                {
                    decode = result;
                }
                return decode;
            }
            /// <summary>
            /// AES解密(无向量)
            /// </summary>
            /// <param name="encryptedBytes">被加密的明文</param>
            /// <param name="key">密钥</param>
            /// <returns>明文</returns>
            public static string AESDecrypt(String Data, String Key)
            {
                Byte[] encryptedBytes = Convert.FromBase64String(Data);
                Byte[] bKey = new Byte[32];
                Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
    
                MemoryStream mStream = new MemoryStream(encryptedBytes);
                //mStream.Write( encryptedBytes, 0, encryptedBytes.Length );
                //mStream.Seek( 0, SeekOrigin.Begin );
                RijndaelManaged aes = new RijndaelManaged();
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;
                aes.KeySize = 128;
                aes.Key = bKey;
                //aes.IV = _iV;
                CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
                try
                {
                    byte[] tmp = new byte[encryptedBytes.Length + 32];
                    int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length + 32);
                    byte[] ret = new byte[len];
                    Array.Copy(tmp, 0, ret, 0, len);
                    return Encoding.UTF8.GetString(ret);
                }
                finally
                {
                    cryptoStream.Close();
                    mStream.Close();
                    aes.Clear();
                }
            }
    
            /// <summary>
            /// RSA解密
            /// </summary>
            /// <param name="privatekey"></param>
            /// <param name="content"></param>
            /// <returns></returns>
            public static string RSADecrypt(string privatekey, string content)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                byte[] cipherbytes;
                rsa.FromXmlString(privatekey);
                cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
    
                return Encoding.UTF8.GetString(cipherbytes);
            }
    

    简介:

    • 可能你回有疑问了,为啥没有SHA128,SHA256,SHA384,SHA512的解密,他们都是一种加密(单向)哈希函数,因此没有直接的解码方法。加密哈希函数的全部目的是您无法撤消它。

    总结:

    有时间就积累一点知识点,也许都用不到,多积累,多学习,没什么坏处。
    原文地址:https://www.cnblogs.com/2828sea/p/13491192.html

    如果你觉得本文对你有帮助,请点击“推荐”,谢谢。


    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    UDP和TCP是网络通讯
    HTTPS
    Kubernetes Ingress API Ingress资源通过允许API网关样式的流量路由
    30条黄金法则
    工作流
    开发注意H5移动端
    Wireshark TCP
    关于dotnet跨平台 和 移动开发&人工智能 微信公众号
    超燃| 2019 中国.NET 开发者峰会视频发布
    免费下载 80多种的微软推出入门级 .NET视频
  • 原文地址:https://www.cnblogs.com/2828sea/p/13495236.html
Copyright © 2020-2023  润新知