いくつかの一般的な暗号化方法の実現

6426 ワード

いくつかの一般的な暗号化方法の実現


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.MD 5暗号化
//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;
        }