C# 常见加密解密类

版权声明:本文为博主原创文章,转载请注明。 https://blog.csdn.net/hotmee/article/details/52583872

大部分的加密类的使用方式是差不多的,其中MD5和SHA类似,均为不可逆;RSA位可加可解;DES和AES类似,均需要加密向量;文中所有明文加密后都转为了净荷明文。没必要转Byte转16进制字符串(难为不懂的?)!!大笑

加密方式 加密向量 是否可逆
MD5、SHA 不需要 不可逆
RSA 不需要 可逆
AES、DES 需要 可逆
尴尬不可逆的不代表真的不可逆!

MD5

MD5为不可逆的加密方式一般用作密码验证,当密码加密结果一致时则认为登陆成功;

    public class MD5
    {
        public string MD5Encrypt(string normalTxt)
        {
           var bytes= Encoding.Default.GetBytes(normalTxt);//求Byte[]数组
            var Md5 = new MD5CryptoServiceProvider().ComputeHash(bytes);//求哈希值
            return Convert.ToBase64String(Md5);//将Byte[]数组转为净荷明文(其实就是字符串)
        }
    }

RSA

加密:

        public string RSAEncrypt(string normaltxt)
        {
            var bytes = Encoding.Default.GetBytes(normaltxt);
           var encryptBytes= new RSACryptoServiceProvider(new CspParameters()).Encrypt(bytes,false);
            return Convert.ToBase64String(encryptBytes);
        }

解密:

        public string RSADecrypt(string securityTxt)
        {
            try//必须使用Try catch,不然输入的字符串不是净荷明文程序就Gameover了
            {
               var bytes= Convert.FromBase64String(securityTxt);
               var DecryptBytes =new RSACryptoServiceProvider(new CspParameters()).Decrypt(bytes,false);
               return Encoding.Default.GetString(DecryptBytes);
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

DES

public class DES
    {
        public DES(byte[] keyvi)//密钥向量,8位就好了例如new byte[]{0x01,0x02,0x03,0x04,0x05,0x05,0x07}
        {
            this.keyvi = keyvi;
        }
        private byte[] keyvi;
        public string DesEncrypt(string normalTxt,string EncryptKey)
        {
           var bytes= Encoding.Default.GetBytes(normalTxt);
            var key = Encoding.UTF8.GetBytes(EncryptKey.PadLeft(8, '0').Substring(0, 8));
            using (MemoryStream ms=new MemoryStream())
            {
                var encry = new DESCryptoServiceProvider();
                CryptoStream cs = new CryptoStream(ms,encry.CreateEncryptor(key, keyvi),CryptoStreamMode.Write);
                cs.Write(bytes,0,bytes.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
        }
        public string DesDecrypt(string securityTxt, string EncryptKey)//解密
        {
            try
            {
                var bytes= Convert.FromBase64String(securityTxt);
                var key = Encoding.UTF8.GetBytes(EncryptKey.PadLeft(8, '0').Substring(0, 8));
                using (MemoryStream ms = new MemoryStream())
                {
                    var descrypt = new DESCryptoServiceProvider();
                    CryptoStream cs = new CryptoStream(ms, descrypt.CreateDecryptor (key, keyvi), CryptoStreamMode.Write);
                    cs.Write(bytes,0,bytes.Length);
                    cs.FlushFinalBlock();
                    return Encoding.UTF8.GetString(ms.ToArray());
                }

            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
    }

SHA

    public class SHA//SHA为不可逆加密方式
    {
        public string SHA1Encrypt(string normalTxt)
        {
            var bytes = Encoding.Default.GetBytes(normalTxt);
            var SHA = new SHA1CryptoServiceProvider();
            var encryptbytes = SHA.ComputeHash(bytes);
            return Convert.ToBase64String(encryptbytes);
        }
        public string SHA256Encrypt(string normalTxt)
        {
            var bytes = Encoding.Default.GetBytes(normalTxt);
            var SHA256 = new SHA256CryptoServiceProvider();
            var encryptbytes = SHA256.ComputeHash(bytes);
            return Convert.ToBase64String(encryptbytes);
        }
        public string SHA384Encrypt(string normalTxt)
        {
            var bytes = Encoding.Default.GetBytes(normalTxt);
            var SHA384 = new SHA384CryptoServiceProvider();
            var encryptbytes = SHA384.ComputeHash(bytes);
            return Convert.ToBase64String(encryptbytes);
        }
        public string SHA512Encrypt(string normalTxt)
        {
            var bytes = Encoding.Default.GetBytes(normalTxt);
            var SHA512 = new SHA512CryptoServiceProvider();
            var encryptbytes = SHA512.ComputeHash(bytes);
            return Convert.ToBase64String(encryptbytes);
        }
    }

AES

 public class AES
    {
        public AES(byte[] iv)
        {
            this.iv = iv;
        }
        byte[] iv;
        public string AESEncrypt(string normalTxt,string key)
        {
            var bytes = Encoding.Default.GetBytes(normalTxt);
            SymmetricAlgorithm des =Rijndael.Create();
            des.Key = Encoding.Default.GetBytes(key);
            des.IV = iv;
            using (MemoryStream ms=new MemoryStream())
            {
                CryptoStream cs = new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
                cs.Write(bytes, 0, bytes.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
        }
        public string AESDecrypt(string securityTxt, string key)
        {
            try
            {
                var bytes = Convert.FromBase64String(securityTxt);
                SymmetricAlgorithm des = Rijndael.Create();
                des.Key = Encoding.Default.GetBytes(key);
                des.IV = iv;
                using (MemoryStream ms = new MemoryStream())
                {
                    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                    cs.Write(bytes, 0, bytes.Length);
                    cs.FlushFinalBlock();
                    return Convert.ToBase64String(ms.ToArray());
                }
            }
            catch (Exception)
            {
                return string.Empty;
            }

        }
    }


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页