C# 加密解密

來源:互聯網
上載者:User

一種簡單的加密

Code
using System;
using System.Collections.Generic;

namespace md
{
class MainClass
{
   public interface IBindesh
    {
        string encode(string str);
        string decode(string str);
    }
   
    public class EncryptionDecryption : IBindesh
    {
        public string encode(string str)
        {
            string htext = "";
            for(int i=0;i<str.Length;i++)
            {
                htext = htext + (char)(str[i] + 10 - 1 * 2);
            }
            return htext;
        }
        public string decode(string str)
        {
            string dtext = "";
            for (int i = 0; i < str.Length; i++)
            {
                dtext = dtext + (char)(str[i] - 10 + 1 * 2);
            }
            return dtext;
        }

   public static void Main(string[] args)
   {
      EncryptionDecryption inter = new EncryptionDecryption();
             string str=((IBindesh)inter).encode("admin");
      Console.WriteLine(str);
                    Console.WriteLine(((IBindesh)inter).decode(str));
                    Console.Read();
   }
}
}
}

 

利用.net中提供的類進行資料DES加密

Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public partial class 加密解密 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string encryptdata = "ABCD";       //要加密的字串
        string encryptkey = "12345678";      //密鑰,必須為8位
        StringBuilder sb = new StringBuilder();
        sb.Append("要加密的資料是:"+encryptdata+"<br />");
        sb.Append("加密後的資料是:"+Encrypt(encryptdata,encryptkey)+"<br />");
        sb.Append("解密後的資料是:"+Decrypt(Encrypt(encryptdata,encryptkey),encryptkey)+"<br />");

        Response.Write(sb.ToString());
    }
    #region
    private string Encrypt(string strData, string strKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] bytedata = Encoding.UTF8.GetBytes(strData);
        //foreach (byte b in bytedata)
        //{
        //    Response.Write(b + "<br />");
        //}
        //Response.Write("<hr />");

        des.Key = ASCIIEncoding.ASCII.GetBytes(strKey); //ASCII碼值
        des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);
        //foreach (byte c in des.Key)
        //{
        //    Response.Write(c + "<br />");
        //}
        //Response.Write("<hr />");

        MemoryStream ms = new MemoryStream();
        using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
        {
            cs.Write(bytedata, 0, bytedata.Length);
            cs.FlushFinalBlock();
            cs.Close();
        }
        //foreach (byte d in ms.ToArray())
        //{
        //    Response.Write(d + "<br />");
        //}
        string str = Convert.ToBase64String(ms.ToArray());
        ms.Close();
        des.Clear();
        return str;
    }
    private string Decrypt(string strData, string strKey)
    {
        byte[] byteData = Convert.FromBase64String(strData);
        using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
        {
            des.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);
            MemoryStream ms = new MemoryStream();
            using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(byteData, 0, byteData.Length);
                cs.FlushFinalBlock();
                cs.Close();
            }
            string str = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return str;
        }
    }
    #endregion
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.