A function for encryption and decryption in asp

Source: Internet
Author: User

There are not many decryption methods corresponding to the encryption method in ASP. Now, according to the previous documents, the encryption and decryption functions in asp are sorted out.

Rsa. asp
<%
Rem implements encryption and decryption in ASP. The encryption method is based on RSA
Rem contact: hnsoso@sina.com
Class clsRSA

Public PrivateKey
Public PublicKey
Public Modulus
  
  
  
Public Function Crypt (pLngMessage, pLngKey)
On Error Resume Next
Dim lLngMod
Dim lLngResult
Dim lLngIndex
If pLngKey Mod 2 = 0 Then
LLngResult = 1
For lLngIndex = 1 To pLngKey/2
LLngMod = (pLngMessage ^ 2) Mod Modulus
'Mod may error on key generation
LLngResult = (lLngMod * lLngResult) Mod Modulus
If Err Then Exit Function
Next
Else
LLngResult = pLngMessage
For lLngIndex = 1 To pLngKey/2
LLngMod = (pLngMessage ^ 2) Mod Modulus
On Error Resume Next
'Mod may error on key generation
LLngResult = (lLngMod * lLngResult) Mod Modulus
If Err Then Exit Function
Next
End If
Crypt = lLngResult
End Function

Public Function Encode (ByVal pStrMessage)
Dim lLngIndex
Dim lLngMaxIndex
Dim lBytAscii
Dim lLngEncrypted
LLngMaxIndex = Len (pStrMessage)
If lLngMaxIndex = 0 Then Exit Function
For lLngIndex = 1 To lLngMaxIndex
LBytAscii = Asc (Mid (pStrMessage, lLngIndex, 1 ))
LLngEncrypted = Crypt (lBytAscii, PublicKey)
Encode = Encode & NumberToHex (lLngEncrypted, 4)
Next
End Function
  
Public Function Decode (ByVal pStrMessage)
Dim lBytAscii
Dim lLngIndex
Dim lLngMaxIndex
Dim lLngEncryptedData
Decode = ""
LLngMaxIndex = Len (pStrMessage)
For lLngIndex = 1 To lLngMaxIndex Step 4
LLngEncryptedData = HexToNumber (Mid (pStrMessage, lLngIndex, 4 ))
LBytAscii = Crypt (lLngEncryptedData, PrivateKey)
Decode = Decode & Chr (lBytAscii)
Next
End Function
  
Private Function NumberToHex (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
%>

Test. asp
<! -- # Include file = "RSA. asp" -->
<%
Function Encryptstr (Message)
Dim LngKeyE
Dim LngKeyD
Dim LngKeyN
Dim StrMessage
Dim ObjRSA

LngKeyE = "32823"
LngKeyD = "20643"
LngKeyN = "29893"
StrMessage = Message
  
Set ObjRSA = New clsRSA
  
 
ObjRSA. PublicKey = LngKeyE
ObjRSA. Modulus = LngKeyN
Encryptstr = ObjRSA. Encode (StrMessage)
Set ObjRSA = Nothing
End function

Function decryptstr (Message)
Dim LngKeyE
Dim LngKeyD
Dim LngKeyN
Dim StrMessage
Dim ObjRSA

LngKeyE = "32823"
LngKeyD = "20643"
LngKeyN = "29893"
StrMessage = Message
  
Set ObjRSA = New clsRSA

ObjRSA. PrivateKey = LngKeyD
ObjRSA. Modulus = LngKeyN
Decryptstr = ObjRSA. Decode (StrMessage)
Set ObjRSA = Nothing
End function

Dim last, first
First = "sohu"
Response. Write ":" & first before encryption
Last = Encryptstr (first)
Response. Write "encrypted to" & last
Response. Write "decrypted as" & decryptstr (last)

%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.