RSA encryption and decryption through VBS class in ASP

xiaoxiao2021-03-05  19

RSA encryption and decryption through VBS class in ASP

This article has two files to compose TEST.ASP Test Demo CLSRSA.ASP implementation RSA encryption and decryption VBS class file below:

Test.asp

<% REM Title: RSA Encryption and Decryption REM Collection Claims in ASP: YANEKREM Contact: AspBoy@263.net

%> <% OPTION Explicit%> <%

Dim LngKeyedim LngKeyddim LngKeyndim Strimedim objrsaif Not request.form = "" THEN

LngKeye = Request.form ("Keye") LNGKEYD = Request.form ("Keyd") LNGKEYN = Request.form ("Keyn" StrMessage = Request.form ("Message")

Set Objrsa = New CLSRSA

Select Case Request.Form ( "Action") Case "Generate Keys" Call ObjRSA.GenKey () LngKeyE = ObjRSA.PublicKeyLngKeyD = ObjRSA.PrivateKeyLngKeyN = ObjRSA.ModulusCase "Encrypt" ObjRSA.PublicKey = LngKeyEObjRSA.Modulus = LngKeyNStrMessage = ObjRSA.Encode (StrMessage) Case "decrypt" objrsa.privatekey = LNGKEYDOBJRSA.MODULUS = LNGKEYNSTRMESSAGE = ObJRSA.DECode (StrMessage) End Select

Set objrsa = Nothing

END IF%> RSA Cipher Demonstration </ title> </ head> <body> <h1> RSA Cipher Demonstration </ h1> <p> you will first need to generate your public / privage Key-Pairbefore You can Encrypt / Decrypt Messages. </ P> <form method = "post"> <table> <tr> <td> public key </ td> <td> <input name = "key" value = " <% = Server.htmlencode (lngKeye)%>> </ td> <td rowspan = "3"> <input type = "submit" name = "action" value = "generate keys"> </ td> </ Tr> <Tr> <TD> Private Key </ TD> <TD> <input name = "keyd" value = "<% = server.htmlencode (lngKeyd)%>> </ td> </ tr> <TR > <TD> MODULUS </ TD> <TD> <input name = "key" value = "<% = server.htmlencode (lngKeyn)%>"> </ td> </ tr> <tr> <td colspan = "3"> Test Message: <br> <textarea name = "message" cols = "50" rows = "7"> <% = server.htmlencode (strmessage)%> </ textarea> </ td> </ tr > <Tr> <TD align = "Right" colSpan = "3"> <input type = "submit" name = "action" value = "encrypt"> <input type = "submit" name = "value =" Decrypt "> </ td> </ tr> </ table> </ form> </ body> </ html> CLSRSA.ASP</p> <p><% REM implementation RSA encryption and decryption VBS class file REM article Title: RSA encryption and decryption RSA encryption in ASP: YANEKREM Contact: AspBoy@263.net</p> <p>'RSA Encryption Class'' .PrivateKey 'Your personal private key. Keep this hidden.' '.PublicKey' Key for others to encrypt data with. '' .Modulus' Used with both public and private keys when encrypting 'and decrypting data. '' .GenKey () 'Creates Public / Private key set and Modulus' '.Crypt (pLngMessage, pLngKey)' encrypts / Decrypts message and returns 'as a string.' '.Encode (pStrMessage)' encrypts message and returns in double -HEX Format '' .decode (pstrmessage) 'Decrypts Message from Double-HEX Format and Returns A String'class Clsrsapublic PrivateKeyPublic Public Public Public Public Publishpublic Modulus</p> <p>Public Sub Genkey () DIM LLNGPHIDIM QDIM P</p> <p>Randomize</p> <p>DODO</p> <p>'2 Random Primary NumBers (0 to 1000) DOP = RND * 1000 / 1Loop While Not Isprime (P)</p> <p>DOQ = RND * 1000 / 1Loop while not isprime (q)</p> <p>'n = product of 2 primesmodulus = p * q / 1</p> <p>'Random Decryptor (2 to n) privatekey = rND * (MODULUS - 2) / 1 2</p> <p>LLNGPHI = (P - 1) * (Q - 1) / 1PublicKey = Euler (LLNGPHI, PRIVATEKEY)</p> <p>Loop while publickey = 0 or publickey = 1</p> <p>'Loop if we can't crypt / decrypt a byte loop while not testcrypt (255)</p> <p>End Sub</p> <p>Private function testcrypt (byref pbytdata) DIM LSTRCRYPTEDLSTRCRYPTED = CRYPT (PBYTDATA, PUBLICKEY) TESTCRYPT = Crypt (lstrcrypted, privatekey) = PBYTDATAEND FUNCTION</p> <p>Private function euler (byref plngphi, byref plngkey)</p> <p>DIM LLNGR (3) DIM LLNGP (3) DIM LLNGQ (3)</p> <p>DIM LLNGCOUNTERDIM LLNGRESULT</p> <p>Euler = 0</p> <p>LLNGR (1) = PLNGPHI: LLNGR (0) = PLNGKEYLLNGP (1) = 0: llngp (0) = 1LLngq (1) = 2: LLNGQ (0) = 0</p> <p>LLNGCOUNTER = -1</p> <p>Do Until LLNGR (0) = 0</p> <p>LLNGR (2) = LLNGR (1): llngr (1) = llngr (0) LLNGP (2) = llngp (1): llngp (1) = llngp (0) LLNGQ (2) = llngq (1): llngq 1) = llngq (0) LLNGCOUNTER = LLNGCOUNTER 1</p> <p>LLNGR (0) = LLNGR (2) MOD LLNGR (1) LLNGP (0) = ((LLNGR (2) / LLNGR (1)) * LLNGP (1)) LLNGP (2) LLNGQ (0) = ((LLNGR) (2) / llngr (1)) * LLNGQ (1)) LLNGQ (2)</p> <p>Loop</p> <p>LLNGRESULT = (PLNGKEY * LLNGP (1)) - (PLNGPHI * LLNGQ (1))</p> <p>IF llngresult> 0 theneuler = llngp (1) elseeuler = abs (llngp (1)) PLNGPHIEND IF</p> <p>END FUNCTION</p> <p>Public Function Crypt (pLngMessage, pLngKey) On Error Resume NextDim lLngModDim lLngResultDim lLngIndexIf pLngKey Mod 2 = 0 ThenlLngResult = 1For lLngIndex = 1 To pLngKey / 2lLngMod = (pLngMessage ^ 2) Mod Modulus' Mod may error on key generationlLngResult = (lLngMod * lLngResult ) Mod Modulus If Err Then Exit FunctionNextElselLngResult = pLngMessageFor lLngIndex = 1 To pLngKey / 2lLngMod = (pLngMessage ^ 2) Mod ModulusOn error Resume Next 'Mod may error on key generationlLngResult = (lLngMod * lLngResult) Mod ModulusIf Err Then Exit FunctionNextEnd IfCrypt = lLngResultEnd Function</p> <p>Private Function IsPrime (ByRef pLngNumber) Dim lLngSquareDim lLngIndexIsPrime = FalseIf pLngNumber <2 Then Exit FunctionIf pLngNumber Mod 2 = 0 Then Exit FunctionlLngSquare = Sqr (pLngNumber) For lLngIndex = 3 To lLngSquare Step 2If pLngNumber Mod lLngIndex = 0 Then Exit FunctionNextIsPrime = TrueEnd Function</p> <p>Public Function Encode (ByVal pStrMessage) Dim lLngIndexDim lLngMaxIndexDim lBytAsciiDim lLngEncryptedlLngMaxIndex = Len (pStrMessage) If lLngMaxIndex = 0 Then Exit FunctionFor lLngIndex = 1 To lLngMaxIndexlBytAscii = Asc (Mid (pStrMessage, lLngIndex, 1)) lLngEncrypted = Crypt (lBytAscii, PublicKey) Encode = Encode & NumberToHex (lLngEncrypted, 4) NextEnd FunctionPublic Function Decode (ByVal pStrMessage) Dim lBytAsciiDim lLngIndexDim lLngMaxIndexDim lLngEncryptedDataDecode = "" lLngMaxIndex = Len (pStrMessage) For lLngIndex = 1 To lLngMaxIndex Step 4lLngEncryptedData = HexToNumber (Mid (pStrMessage, lLngIndex, 4) ) lbytascii = crypt (llngencrypteddata, privatekey) decode = decode & chr (lbytascii) Nextend Function</p> <p>Private function number, byref plngnumber, byref plnglength) Numbertohex = right (String (Plnglength, "0") & hex (plngnumber), PLNGLENGTH) End Function</p> <p>Private function hextonumber (byref pstrhex) hextonumber = clng ("& H" & pstrhex) end function</p> <p>END CLASS%></p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-39020.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="39020" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.045</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '_2Fl0_2F5KBNO_2F2dDN7qEifoFdkNGEJUvH0Vtf_2By19lR9v2_2B6zMd1dsKS8Vp9NdaR55zJNo397phRvonvlNPr7ib4w_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>