zoukankan      html  css  js  c++  java
  • 几种常见的加密方法的实现

    几种常见的加密方法的实现

    1.ACSII码加密

     //ACSII码加密
            private static string ACSIIPWd(string rpwd)
            {
                string Ret;
                byte[] array = System.Text.Encoding.ASCII.GetBytes(rpwd);
                byte[] byteArray = new byte[array.Length];
    
                for (int i = 0; i < array.Length; i++)
                {
                    int asciicode = (int)(array[i]);
                    asciicode = asciicode + 1;
                    byteArray[i] = (byte)asciicode;
                }
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                string strCharacter = asciiEncoding.GetString(byteArray);
                Ret = strCharacter;
                return Ret;
            }

    2.MD5加密

    //MD5加密
            private static string MD5PWd(string rpwd)
            {
                string Ret;
    
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] palindata = Encoding.Default.GetBytes(rpwd);//将要加密的字符串转换为字节数组
                byte[] encryptdata = md5.ComputeHash(palindata);//将字符串加密后也转换为字符数组
                Ret = Convert.ToBase64String(encryptdata);
                return Ret;
            }

    3.RSA加密

    //RSA加密
            private static string RSAPWD1(string myKeyContainerName)
            {
                string ret = "";         
                CspParameters cp = new CspParameters();
                cp.KeyContainerName = myKeyContainerName;
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
               
                ret = rsa.ToXmlString(true);
                Console.WriteLine("Key is : 
    " + rsa.ToXmlString(true));
                return ret;
    
            }

    4.DES加密

     //DES加密  
            private static string DESPWD(string ciphertext)
            {
                string desPWD = ""; 
                byte[] buffer;
                DESCryptoServiceProvider DesCSP = new DESCryptoServiceProvider();
    
                MemoryStream ms = new MemoryStream();//先创建 一个内存流
                CryptoStream cryStream = new CryptoStream(ms, DesCSP.CreateEncryptor(), CryptoStreamMode.Write);//将内存流连接到加密转换流
                StreamWriter sw = new StreamWriter(cryStream);
                sw.WriteLine(ciphertext);//将要加密的字符串写入加密转换流
                sw.Close();
                cryStream.Close();
                buffer = ms.ToArray();//将加密后的流转换为字节数组
                desPWD = Convert.ToBase64String(buffer);//将加密后的字节数组转换为字符串
    
                return desPWD;
            }
  • 相关阅读:
    ASP.NET Core 基于JWT的认证(二)
    ASP.NET Core 基于JWT的认证(一)
    C#数据Encrypt加密Encrypt解密的算法使用--非对称算法RSACryptoServiceProvider
    C#数据Encrypt加密Encrypt解密的算法使用
    C# 实现winform自动悬浮
    测试winform自动悬浮
    C#实现图像拖拽以及锚点缩放功能
    C# Microsoft.Office.Interop.Owc11 导出excel文件
    Docker修改daemon.json后无法启动的问题
    Kubernetes命名空间
  • 原文地址:https://www.cnblogs.com/wml-it/p/12978564.html
Copyright © 2011-2022 走看看