Î÷Î÷Èí¼þÔ°¶àÖØ°²È«¼ì²âÏÂÔØÍøÕ¾¡¢ÖµµÃÐÅÀµµÄÈí¼þÏÂÔØÕ¾£¡
Èí¼þ
Èí¼þ
ÎÄÕÂ
ËÑË÷

Ê×Ò³ ¡ú ±à³Ì¿ª·¢ ¡ú C#.NET ¡ú C#¿ª·¢Öг£ÓüÓÃܽâÃÜ·½·¨½âÎö

C#¿ª·¢Öг£ÓüÓÃܽâÃÜ·½·¨½âÎö

Ïà¹ØÈí¼þÏà¹ØÎÄÕ·¢±íÆÀÂÛ À´Ô´£ºÎ÷Î÷ÕûÀíʱ¼ä£º2012/10/26 15:27:12×ÖÌå´óС£ºA-A+

×÷ÕߣºÎ÷Î÷µã»÷£º4834´ÎÆÀÂÛ£º4´Î±êÇ©£º ¼ÓÃÜ

  • ÀàÐÍ£º±à³Ì¸¨Öú´óС£º3.8MÓïÑÔ£ºÓ¢ÎÄ ÆÀ·Ö£º5.0
  • ±êÇ©£º
Á¢¼´ÏÂÔØ

 ÒòΪ×î½üµÄ×öµÄ¶«Î÷ÒªÓõ½Ò»Ð©¹ØÓÚ¼ÓÃܵļ¼Êõ£¬ËùÒԾͰÑһЩ³£ÓõļÓÃÜ·½·¨¸ø¸ø×ܽáÁËһϡ£ºÃÁË·Ï»°²»¶à˵£¬»¹ÊÇ°´ÕÕÒÔÍùµÄ¹ßÀý£¬ÎÒ»áͨ¹ýÒ»¸ö¸öµÄʵÀý£¬ÖðÒ»µÄ°Ñ³£ÓÃËã·¨¸ø´ó¼Ò½²½âһϡ£

    Ò»¡¢MD5¼ÓÃÜËã·¨

    ÎÒÏëÕâÊÇ´ó¼Ò¶¼³£Ìý¹ýµÄËã·¨£¬¿ÉÄÜÒ²ÓõıȽ϶ࡣÄÇôʲôÊÇMD5Ëã·¨ÄØ£¿MD5È«³ÆÊÇmessage-digest algorithm 5£¬¼òµ¥µÄ˵¾ÍÊǵ¥ÏòµÄ¼ÓÃÜ£¬¼´ÊÇ˵ÎÞ·¨¸ù¾ÝÃÜÎÄÍƵ¼³öÃ÷ÎÄ¡£

      MD5Ö÷ÒªÓÃ;£º

       1¡¢¶ÔÒ»¶ÎÐÅÏ¢Éú³ÉÐÅÏ¢ÕªÒª£¬¸ÃÕªÒª¶Ô¸ÃÐÅÏ¢¾ßÓÐΨһÐÔ,¿ÉÒÔ×÷ΪÊý×ÖÇ©Ãû¡£

       2¡¢ÓÃÓÚÑéÖ¤ÎļþµÄÓÐЧÐÔ(ÊÇ·ñÓжªÊ§»òË𻵵ÄÊý¾Ý),

       3¡¢¶ÔÓû§ÃÜÂëµÄ¼ÓÃÜ£¬

       4¡¢ÔÚ¹þÏ£º¯ÊýÖмÆËãÉ¢ÁÐÖµ

   ´ÓÉϱߵÄÖ÷ÒªÓÃ;ÖÐÎÒÃÇ¿´µ½£¬ÓÉÓÚËã·¨µÄijЩ²»¿ÉÄæÌØÕ÷£¬ÔÚ¼ÓÃÜÓ¦ÓÃÉÏÓнϺõݲȫÐÔ¡£Í¨¹ýʹÓÃMD5¼ÓÃÜËã·¨£¬ÎÒÃÇÊäÈëÒ»¸öÈÎÒⳤ¶ÈµÄ×Ö½Ú´®£¬¶¼»áÉú³ÉÒ»¸ö128λµÄÕûÊý¡£ËùÒÔ¸ù¾ÝÕâÒ»µãMD5±»¹ã·ºµÄÓÃ×÷ÃÜÂë¼ÓÃÜ¡£ÏÂÃæÎÒ¾ÍÏñ´ó¼ÒÑÝʾһÏÂÔõÑù½øÐÐÃÜÂë¼ÓÃÜ¡£

   ÏÈ¿´ÏÂÑÝʾЧ¹û£º

¾ßÌå´úÂëÈçÏ£º

Ê×ÏÈÐèÒªÒýÈëÃüÃû¿Õ¼ä£º

using System.Security;
using System.Security.Cryptography;
private void btnmd5_Click(object sender, EventArgs e)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] palindata = Encoding.Default.GetBytes(txtyuan.Text);//½«Òª¼ÓÃܵÄ×Ö·û´®×ª»»Îª×Ö½ÚÊý×é
byte[] encryptdata=md5.ComputeHash(palindata);//½«×Ö·û´®¼ÓÃܺóҲת»»Îª×Ö·ûÊý×é
txtjiami.Text = Convert.ToBase64String(encryptdata);//½«¼ÓÃܺóµÄ×Ö½ÚÊý×éת»»Îª¼ÓÃÜ×Ö·û´®
}

    ÕâÀïÎÒÃÇÐèҪעÒâµÄÊÇ£¬²»ÂÛÊÇÔÚ¼ÓÃܵĹý³ÌÖУ¬¼ÓÃÜÇ°Òª½«¼ÓÃÜ×Ö·û´®×ªÎª×Ö½ÚÊý×飬¼ÓÃܺóÒ²ÒªÉú³ÉÃÜÎĵÄ×Ö½ÚÊý¾Ý£¬È»ºóÔÙת»¯ÎªÃÜÎÄ¡£

    ¶þ¡¢RSA¼ÓÃÜËã·¨

    ÔÚ̸RSA¼ÓÃÜË㷨֮ǰ£¬ÎÒÃÇÐèÒªÏÈÁ˽âÏÂÁ½¸öרҵÃû´Ê£¬¶Ô³Æ¼ÓÃܺͷǶԳƼÓÃÜ¡£

    ¶Ô³Æ¼ÓÃܼ´£ºº¬ÓÐÒ»¸ö³ÆΪÃÜÔ¿µÄ¶«Î÷£¬ÔÚÏûÏ¢·¢ËÍǰʹÓÃÃÜÔ¿¶ÔÏûÏ¢½øÐмÓÃÜ£¬ÔÚ¶Ô·½ÊÕµ½ÏûÏ¢Ö®ºó£¬Ê¹ÓÃÏàͬµÄÃÜÔ¿½øÐнâÃÜ

·Ç¶Ô³Æ¼ÓÃܼ´£º¼ÓÃܺͽâÃÜʹÓò»Í¬µÄÃÜÔ¿µÄÒ»Àà¼ÓÃÜËã·¨¡£ÕâÀà¼ÓÃÜË㷨ͨ³£ÓÐÁ½¸öÃÜÔ¿AºÍB£¬Ê¹ÓÃÃÜÔ¿A¼ÓÃÜÊý¾ÝµÃµ½µÄÃÜÎÄ£¬Ö»ÓÐÃÜÔ¿B¿ÉÒÔ½øÐнâÃܲÙ×÷£¨¼´Ê¹ÃÜÔ¿AÒ²ÎÞ·¨½âÃÜ£©£¬Ïà·´£¬Ê¹ÓÃÁËÃÜÔ¿B¼ÓÃÜÊý¾ÝµÃµ½µÄÃÜÎÄ£¬Ö»ÓÐÃÜÔ¿A¿ÉÒÔ½âÃÜ¡£ÕâÁ½¸öÃÜÔ¿·Ö±ð³ÆΪ˽ԿºÍ¹«Ô¿£¬¹ËÃû˼Ò壬˽Կ¾ÍÊÇÄã¸öÈ˱£Áô£¬²»Äܹ«¿ªµÄÃÜÔ¿£¬¶ø¹«Ô¿ÔòÊǹ«¿ª¸ø¼Ó½âÃܲÙ×÷µÄÁíÒ»·½µÄ¡£¸ù¾Ý²»Í¬ÓÃ;£¬¶ÔÊý¾Ý½øÐмÓÃÜËùʹÓõÄÃÜÔ¿Ò²²»Ïàͬ£¨ÓÐʱÓù«Ô¿¼ÓÃÜ£¬Ë½Ô¿½âÃÜ£»ÓÐʱÏà·´ÓÃ˽Կ¼ÓÃÜ£¬¹«Ô¿½âÃÜ£©¡£·Ç¶Ô³Æ¼ÓÃܵĴú±íËã·¨ÊÇRSAËã·¨¡£

     Á˽âÁËÕâÁ½¸öÃû´ÊÏÂÃæÀ´½²£¬RSA¼ÓÃÜËã·¨¡£RSAÈ¡ÃûÀ´×Ô¿ª·¢ËûÃÇÈýÕßµÄÃû×Ö¡£RSAÊÇÄ¿Ç°×îÓÐÓ°ÏìÁ¦µÄ¹«Ô¿¼ÓÃÜËã·¨£¬¶àÓÃÓÚÊý¾Ý¼ÓÃܺÍÊý×ÖÇ©Ãû¡£ËäÈ»ÓÐÕâô´óµÄÓ°ÏìÁ¦£¬µ«ÊÇͬʱËüÒ²ÓÐһЩ±×¶Ë£¬Ëü²úÉúÃÜÔ¿ºÜÂé·³£¬Êܵ½ËØÊý²úÉú¼¼ÊõµÄÏÞÖÆ£¬Òò¶øÄÑÒÔ×öµ½Ò»´ÎÒ»ÃÜ£¬·Ö×鳤¶ÈÌ«´óµÈ¡£

     ÏÂÃæͨ¹ýʾÀýÑÝʾʹÓÃRSA¼ÓÃÜ¡¢½âÃÜ£º

     ÏÈ´´½¨Ò»¸öÈ«¾ÖµÄCspParameters¶ÔÏóparam

     ¼ÓÃÜ£º

private void btnjm_Click(object sender, EventArgs e)
{
param = new CspParameters();
param.KeyContainerName = "Olive";//Ãܳ×ÈÝÆ÷µÄÃû³Æ£¬±£³Ö¼ÓÃܽâÃÜÒ»Ö²ÅÄܽâÃܳɹ¦
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
{
byte[] plaindata = Encoding.Default.GetBytes(txtyuan.Text);//½«Òª¼ÓÃܵÄ×Ö·û´®×ª»»Îª×Ö½ÚÊý×é
byte[] encryptdata = rsa.Encrypt(plaindata, false);//½«¼ÓÃܺóµÄ×Ö½ÚÊý¾Ýת»»ÎªÐµļÓÃÜ×Ö½ÚÊý×é
txtjiami.Text =Convert.ToBase64String(encryptdata);//½«¼ÓÃܺóµÄ×Ö½ÚÊý×éת»»Îª×Ö·û´®
}
}

    ½âÃÜ£º

private void btnjiemi_Click(object sender, EventArgs e)
{
param = new CspParameters();
param.KeyContainerName = "Olive";
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
{
byte[] encryptdata = Convert.FromBase64String(this.txtjiami.Text);
byte[] decryptdata = rsa.Decrypt(encryptdata, false);
txthjiemi.Text = Encoding.Default.GetString(decryptdata);
}
}

    Ч¹ûÈçͼ£º

     ÏÂÃæÎÒÔÙͨ¹ýÒ»¸öʾÀýÏò´ó¼ÒÑÝʾ£¬Í¨¹ýʹÓÃRSA¼ÓÃÜËã·¨²ú³ö¹«³×ºÍ˽³×

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
using (StreamWriter sw = new StreamWriter(@"D:\PublicKey.xml"))//²úÉú¹«³×
{
sw.WriteLine(rsa.ToXmlString(false));
}
using (StreamWriter sw = new StreamWriter(@"D:\PrivateKey.xml"))//²úÉú˽³×£¨Ò²°üº¬Ë½³×£©
{
sw.WriteLine(rsa.ToXmlString(false));
}
    Èý¡¢DES¼ÓÃÜ

    DES¼ÓÃÜ£ºÊ¹ÓÃÒ»¸ö 56 Î»µÄÃÜÔ¿ÒÔ¼°¸½¼ÓµÄ 8 Î»ÆæżУÑé룬²úÉú×î´ó 64 Î»µÄ·Ö×é´óС¡£ÕâÊÇÒ»¸öµü´úµÄ·Ö×éÃÜÂ룬ʹÓóÆΪ Feistel µÄ¼¼Êõ£¬ÆäÖн«¼ÓÃܵÄÎı¾¿é·Ö³ÉÁ½°ë¡£Ê¹ÓÃ×ÓÃÜÔ¿¶ÔÆäÖÐÒ»°ëÓ¦ÓÃÑ­»·¹¦ÄÜ£¬È»ºó½«Êä³öÓëÁíÒ»°ë½øÐС°Òì»ò¡±ÔËË㣻½Ó׎»»»ÕâÁ½°ë£¬ÕâÒ»¹ý³Ì»á¼ÌÐøÏÂÈ¥£¬µ«×îºóÒ»¸öÑ­»·²»½»»»¡£DES Ê¹Óà16 ¸öÑ­»·£¬Ê¹ÓÃÒì»ò£¬Öû»£¬´ú»»£¬ÒÆλ²Ù×÷ËÄÖÖ»ù±¾ÔËËã¡£¶îרҵÊõÓï¾Í¿´¿´µÃÁË£¬ÏÂÃæÖ±½Ó¸ø´ó¼ÒÑÝʾһ¸öСdemo£¬ÒÔ°ïÖú´ó¼ÒµÄÀí½â¡£

   Ïȶ¨ÒåÒ»¸öÈ«¾ÖµÄ×Ö½ÚÊý×éºÍʵÀý»¯Ò»¸öÈ«¾ÖµÄDESCryptoServiceProvider¶ÔÏó

        byte[] buffer;

        DESCryptoServiceProvider DesCSP = new DESCryptoServiceProvider();

   ¼ÓÃÜ£º

private void button2_Click(object sender, EventArgs e)
{
MemoryStream ms = new MemoryStream();//ÏÈ´´½¨ Ò»¸öÄÚ´æÁ÷
CryptoStream cryStream = new CryptoStream(ms, DesCSP.CreateEncryptor(), CryptoStreamMode.Write);//½«ÄÚ´æÁ÷Á¬½Óµ½¼ÓÃÜת»»Á÷
StreamWriter sw = new StreamWriter(cryStream);
sw.WriteLine(txtyuan.Text);//½«Òª¼ÓÃܵÄ×Ö·û´®Ð´Èë¼ÓÃÜת»»Á÷
sw.Close();
cryStream.Close();
buffer = ms.ToArray();//½«¼ÓÃܺóµÄÁ÷ת»»Îª×Ö½ÚÊý×é
txtjiami.Text =Convert.ToBase64String(buffer);//½«¼ÓÃܺóµÄ×Ö½ÚÊý×éת»»Îª×Ö·û´®
}

    ½âÃÜ£º

private void button1_Click(object sender, EventArgs e)
{
MemoryStream ms = new MemoryStream(buffer);//½«¼ÓÃܺóµÄ×Ö½ÚÊý¾Ý¼ÓÈëÄÚ´æÁ÷ÖÐ
CryptoStream cryStream = new CryptoStream(ms, DesCSP.CreateDecryptor(), CryptoStreamMode.Read);//ÄÚ´æÁ÷Á¬½Óµ½½âÃÜÁ÷ÖÐ
StreamReader sr = new StreamReader(cryStream);
txthjiemi.Text = sr.ReadLine();//½«½âÃÜÁ÷¶ÁȡΪ×Ö·û´®
sr.Close();
cryStream.Close();
ms.Close();
}

    ´ËÍ⻹ÓÐAES¼ÓÃÜËã·¨£¬µ«ÊÇAES¼ÓÃÜÊÇÒ»¸öеĿÉÒÔÓÃÓÚ±£»¤µç×ÓÊý¾ÝµÄ¼ÓÃÜËã·¨¡£Æä²úÉúµÄÃÜÂëÊǵü´ú¶Ô³ÆµÄ·Ö×éÃÜÂ룬´ú¼ÓÃÜʹÓÃÒ»¸öÑ­»·½á¹¹£¬ÔÚ¸ÃÑ­»·ÖÐÖظ´Öû»ºÍÌæ»»ÊäÈëÊý¾Ý¡£ÒòΪÓõIJ»ÊǺܶ࣬ÔÚÕâÀï¾Í²»ÔÙ×ö¾ßÌåµÄÑÝʾÁË¡£

    ºÃÁË£¬¹ØÓÚ¼ÓÃÜÕâÒ»½Ú¾Í½²µ½ÕâÀïÁË£¬ÕâÀïÖ»ÊǼòµ¥µÄ½éÉÜÁ˼¸ÖÖ³£ÓõļÓÃÜ·½·¨£¬Í¬Ê±ÅäÒÔ¼òµ¥µÄʾÀý£¬Ö÷ÒªµÄÄ¿µÄÔÚÓÚ¶ÔÕâЩ¼ÓÃÜËã·¨µÄÁ˽âºÍʹÓ㬵±È»£¬Èç¹ûÐèÒª¸üÉî²ã´ÎµÄ¼ÓÃܽö½öÖªµÀÕâЩ»¹ÊÇÔ¶Ô¶²»¹»µÄ£¬ÓÐÐËȤµÄÅóÓÑ¿ÉÒÔ×Ô¼ºÈ¥¶àÁ˽âЩ¡£Ï£ÍûÕâЩÄܸø´ó¼Ò´øÀ´°ïÖú¡£Ò²Ï£Íû´ó¼Ò¶à¶àÖ¸µã£¡

    Ïà¹ØÆÀÂÛ

    ÔĶÁ±¾ÎĺóÄúÓÐʲô¸ÐÏë? ÒÑÓÐÈ˸ø³öÆÀ¼Û!

    • 8 ϲ»¶Ï²»¶
    • 3 ¶¥¶¥
    • 1 ÄѹýÄѹý
    • 5 ‡å‡å
    • 3 Χ¹ÛΧ¹Û
    • 2 ÎÞÁÄÎÞÁÄ

    ÈÈÃÅÆÀÂÛ

    ×îÐÂÆÀÂÛ

    µÚ 1 Â¥ CZ88.NETÿ ÍøÓÑ ¿ÍÈË ·¢±íÓÚ: 2015/1/5 15:55:07
    good

    Ö§³Ö( 0 ) ¸ÇÂ¥(»Ø¸´)

    ·¢±íÆÀÂÛ ²é¿´ËùÓÐÆÀÂÛ(4)

    êdzÆ:
    ±íÇé: ¸ßÐË ¿É º¹ ÎÒ²»Òª º¦Ðß ºÃ ÏÂÏÂÏ ËÍ»¨ ʺ Ç×Ç×
    ×ÖÊý: 0/500 (ÄúµÄÆÀÂÛÐèÒª¾­¹ýÉóºË²ÅÄÜÏÔʾ)