Произход
W.DiffieandM.Hellmanpublishedanarticle"NewDirectioninCryptography"inIEEETrans.onInformationin1976,andproposedthat"asymmetriccryptographyispublicTheconceptof"keycryptosystem"hascreatedanewdirectionincryptographyresearch
Принцип на работа
1.AwantstosendinformationtoB,andAandBmusthaveapairoffunctions.Publicandprivatekeysforencryptionanddecryption.
2.A’sprivatekeyiskeptsecret, andA’s publicckeyistert toB;B’sprivatekeyiskeptsecret, andB’s publicckeyistert toA.
3. Когато някой иска да изпрати съобщение до B, използва публичния ключ на B, за да шифрова съобщението, защото познава публичния ключ на B.
4. Изпраща това съобщение до B (съобщението е шифровано с publickey на B).
5.AfterBreceivesthismessage,BuseshisprivatekeytodecryptA'smessage.Allotherpeoplewhoreceivethismessagecannotdecryptit,becauseonlyBhasB'sprivatekey.
Основно приложение
Asymmetricencryption(publickeyencryption):Referstoencryptionalgorithmsthatusedifferentkeysforencryptionanddecryption,alsoknownaspublicandprivatekeyencryption.Supposethattwouserswanttoencryptandexchangedata,andthetwopartiesexchangepublickeys.Whenusing,onepartyusestheotherparty'spublickeytoencrypt,andtheotherpartycanuseitsownprivatekeytodecrypt.Iftherearenusersinanenterprise,theenterpriseneedstogeneratenpairsofkeysanddistributenpublickeys.SupposeAusesB'spublickeytoencryptamessageandsignswithA'sprivatekey.Afterreceivingthemessage,BfirstverifiesthesignaturewithA'spublickey,andthendecryptsthemessagewithhisprivatekeyafterconfirmation.Sincethepublickeycanbedisclosed,usersonlyneedtokeeptheirprivatekeys,sothedistributionofencryptionkeyswillbecomeverysimple.Atthesametime,becauseeachuser'sprivatekeyisunique,otheruserscannotonlyusethesender'spublickeytoverifythesourceoftheinformation,butalsousedigitalsignaturestoensurethatthesendercannotdenythattheinformationhasbeensent.Thedisadvantageofasymmetricencryptionisthattheencryptionanddecryptionspeedismuchslowerthansymmetricencryption.Insomeextremecases,itcanevenbe1000timesslowerthansymmetricencryption.
DSACryptoServiceProviderRSACryptoServiceProvider
//Шифроване
UnicodeEncodingencoding=новоUnicodeEncoding();
byte[]PasswordBytes=encoding.GetBytes(password);//Конвертиране на паролата в abytearrayRSACryptoServiceProvidercrypt=newRSACryptoServiceProvider();//RSAencryptionalgorithm,asymmetricPasswordBytes=crypt.Encrypt(password,false);//Encryptthebytearray,whichisencryptedThepasswordvalueisplacedadinatablefield.intheda
stringkey=crypt.ToXmlString(true);//TheoutputkeyisastringinXMLformatandcontainstheprivatekey.Thisstringshouldbeplacedasafieldinthedatabasetablewiththeuser’spasswordTogether.
//Декриптиране
RSACryptoServiceProvidercrypt=newRSACryptoServiceProvider();//Akeypairhasbeenrandomlygenerated
crypt.Clear();//Унищожи текущата двойка ключове
crypt.FromXmlString(key)//Въведете двойката ключове,ключът енизът на XML формат, прочетен отполето на таблицата на базата данни,това е полето за ключPasswordBytes=crypt.Decrypt(password,false);//Декриптирайтемаиса от bytearray и върнетеоригиналната парола на потребителя
Onefeatureoftheabovemethodisthateachusercorrespondstoakeypair(includingpublicandprivatekeys),Theyareallrandomlygenerated,sotheyarealldifferent.Buttheshortcomingisalsoveryobvious,thatis,thekeyisstoredinthedatabase,ifthedatabaseiscompromised,thekeywillbeleaked.
Anothermethodistorandomlygenerateakeypair(includingpublickeyandprivatekey)accordingtotheabovemethod,exportitthroughtheToXmlString(true)method,andthenputthekeyinXMLstringformatGototheAppSettingnodeoftheWeb.configfileofyourWebprogram,andthenreadthekeythroughtheFromXmlString(key)method,whichmeansthatalluserpasswordsareencryptedanddecryptedwiththesamekeypair.
Основни функции
Theasymmetricencryptionsystemdoesnotrequirethecommunicationpartiestopassthekeyinadvanceorhaveanyagreementtocompleteconfidentialcommunication,andthekeymanagementisconvenient,whichcanpreventcounterfeitinganddenialTherefore,itismoresuitablefortheconfidentialcommunicationrequirementsinnetworkcommunication.
Основни алгоритми
RSA, Elgamal, knapsackalgorithm, Rabin, DH, ECC (криптиращ алгоритъм с елиптична крива).
ThemostwidelyusedistheRSAalgorithm.Elgamalisanothercommonlyusedasymmetricencryptionalgorithm.
ElgamalwasinventedbyTaherElgamalin1985.ItisbasedontheDiffieˉHellmankeyexchangealgorithm,whichenablesbothcommunicatingpartiestoderivethesecretkeyvaluethatonlytheyknowthroughpubliccommunication[DiffieˉHellman].DiffieˉHellmanwasinventedbyWhitfieldDiffieandMartinHellmanin1976.Itisregardedasthefirstasymmetricencryptionalgorithm.ThedifferencebetweenDiffieˉHellmanandRSAisthatDiffieˉHellmanisnotanencryptionalgorithm,itjustgeneratesasecretvaluethatcanbeusedasasymmetrickey..IntheDiffieˉHellmankeyexchangeprocess,thesenderandthereceiverrespectivelygenerateasecretrandomnumber,andderivethepublicvaluefromtherandomnumber,andthenthetwopartiesexchangethepublicvalue.ThebasisoftheDiffieˉHellmanalgorithmistheabilitytogenerateasharedkey.Aslongasthepublicvalueisexchanged,bothpartiescanusetheirownprivatenumberandtheother'spublicvaluetogenerateasymmetrickey,whichiscalledasharedkey.Forbothparties,thesymmetrickeyisthesameandcanbeusedtousesymmetricencryption.Algorithmtoencryptdata.
ComparedwithRSA,oneoftheadvantagesofDiffieˉHellmanisthatitusesanewsetofvalueseachtimethekeyisexchanged.WhenusingtheRSAalgorithm,iftheattackerobtainstheprivatekey,thenhecannotonlydecryptMessagesinterceptedbeforecanalsodecryptallmessagesafterwards.However,RSAcanpreventman-in-the-middleattacksthroughauthentication(suchasusingX.509digitalcertificates),butDiffieHellmanisveryvulnerabletoman-in-the-middleattacks.
Разлика в алгоритъма
NonSymmetricencryptionalgorithmDifferencefromsymmetricencryptionalgorithm
First,thekeyvalueusedformessagedecryptionisdifferentfromthekeyvalueusedformessageencryption;
Secondly,theasymmetricencryptionalgorithmisthousandsoftimesslowerthanthesymmetricencryptionalgorithm,butitprotectsIntermsofcommunicationsecurity,asymmetricencryptionalgorithmshavetheadvantagethatsymmetricencryptionisdifficulttoachieve.
Toillustratethisadvantage,anexampleofasymmetricencryptionalgorithmisusedtoemphasize:
AliceusesthekeyKtoencryptthemessageandsendsittoBob.AfterBobreceivestheencryptedmessage,UsethekeyKtodecryptittorestoretheoriginalmessage.Thereisaquestionhere,thatis,howdoesAlicesendthekeyvalueusedtoencryptthemessagetoBob?TheansweristhatwhenAlicesendsthekeyvaluetoBob,itmustpassthroughanindependentsecurecommunicationchannel(thatis,noonecanlistentothechannelCommunication).
Thisneedtouseanindependentsecurechanneltoexchangesymmetricencryptionalgorithmkeyswillcausemoreproblems:
First,thereisanindependentsecurechannel,butthebandwidthofthesecurechannelislimited.,Youcan’tdirectlyuseittosendtheoriginalmessage.
Secondly,AliceandBobcannotdeterminehowlongtheirkeyvaluecanbekeptwithoutrevealing(thatis,notknownbyothers)andwhentoexchangeanewkeyvalue
Ofcourse,TheseproblemsarenotonlyencounteredbyAlice,Bobandeveryoneelsewillencounterthem,theyallneedtoexchangekeysanddealwiththesekeymanagementproblems(infact,X9.17isaDESkeymanagementANSIstandard[ANSIX9.17]).IfAlicewantstosendmessagestohundredsofpeople,thenthingswillbeevenmoretroublesome.Shemustuseadifferentkeyvaluetoencrypteachmessage.Forexample,tosendanotificationto200people,Aliceneedstoencryptthemessage200timesandencryptthemessageonceforeachrecipient.Obviously,inthiscase,thecostofusingasymmetricencryptionalgorithmforsecurecommunicationisconsiderable.
Themainadvantageofasymmetricencryptionalgorithmsistousetwokeyvaluesinsteadofone:onekeyvalueisusedtoencryptthemessage,andtheotherkeyvalueisusedtodecryptthemessage.Thesetwokeyvaluesaregeneratedinthesameprocessandarecalledakeypair.Thekeyusedtoencryptthemessageiscalledthepublickey,andthekeyusedtodecryptthemessageiscalledtheprivatekey.Messagesencryptedwithapublickeycanonlybedecryptedwiththecorrespondingprivatekey.Theprivatekeyisknowntonooneexcepttheholder,whilethepublickeycanbesentthroughaninsecurechannelorpublishedinadirectory.
AliceneedstosendaconfidentialdocumenttoBobviaemail.First,BobusesemailtosendhispublickeytoAlice.ThenAliceencryptsthedocumentwithBob'spublickeyandsendstheencryptedmessagetoBobviaemail.SinceanymessageencryptedwithBob'spublickeycanonlybedecryptedwithBob'sprivatekey,evenifthesnoopknowsBob'spublickey,themessageisstillsafe.AfterBobreceivestheencryptedmessage,hedecryptsitwithhisprivatekeytorestoretheoriginaldocument.