td> tr> table> form> body> html> CLSRSA.ASP
<% 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
'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
Public Sub Genkey () DIM LLNGPHIDIM QDIM P
Randomize
DODO
'2 Random Primary NumBers (0 to 1000) DOP = RND * 1000 / 1Loop While Not Isprime (P)
DOQ = RND * 1000 / 1Loop while not isprime (q)
'n = product of 2 primesmodulus = p * q / 1
'Random Decryptor (2 to n) privatekey = rND * (MODULUS - 2) / 1 2
LLNGPHI = (P - 1) * (Q - 1) / 1PublicKey = Euler (LLNGPHI, PRIVATEKEY)
Loop while publickey = 0 or publickey = 1
'Loop if we can't crypt / decrypt a byte loop while not testcrypt (255)
End Sub
Private function testcrypt (byref pbytdata) DIM LSTRCRYPTEDLSTRCRYPTED = CRYPT (PBYTDATA, PUBLICKEY) TESTCRYPT = Crypt (lstrcrypted, privatekey) = PBYTDATAEND FUNCTION
Private function euler (byref plngphi, byref plngkey)
DIM LLNGR (3) DIM LLNGP (3) DIM LLNGQ (3)
DIM LLNGCOUNTERDIM LLNGRESULT
Euler = 0
LLNGR (1) = PLNGPHI: LLNGR (0) = PLNGKEYLLNGP (1) = 0: llngp (0) = 1LLngq (1) = 2: LLNGQ (0) = 0
LLNGCOUNTER = -1
Do Until LLNGR (0) = 0
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
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)
Loop
LLNGRESULT = (PLNGKEY * LLNGP (1)) - (PLNGPHI * LLNGQ (1))
IF llngresult> 0 theneuler = llngp (1) elseeuler = abs (llngp (1)) PLNGPHIEND IF
END FUNCTION
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
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
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
Private function number, byref plngnumber, byref plnglength) Numbertohex = right (String (Plnglength, "0") & hex (plngnumber), PLNGLENGTH) End Function
Private function hextonumber (byref pstrhex) hextonumber = clng ("& H" & pstrhex) end function
END CLASS%>
转载请注明原文地址:https://www.9cbs.com/read-39020.html
|