Functions for encrypting and decrypting in ASP

Source: Internet
Author: User
Tags exit crypt functions key modulus
Encryption | Decryption in the ASP encryption method has a corresponding decryption method does not seem much, now based on the predecessors of the data in the ASP encryption and decryption function

Rsa.asp
<%
REM Encryption and decryption in ASP, encryption method: according to 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 "Before encrypting:" &first
Last=encryptstr (a)
Response.Write "After Encryption" &last
Response.Write "Decrypted as" &decryptstr (last)
%>

Related Article

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.