"Protect" your data with an ASP encryption algorithm

Source: Internet
Author: User
Tags chr crypt decrypt
Encryption | data | The algorithm first briefly introduces the background of encryption. Because the United States prohibits several cryptographic algorithms from exporting encrypted digits (such as SSL's 40-bit encryption limit), this article describes a simple character encryption algorithm that an ASP can use, rather than a restricted encryption algorithm. In fact, the encryption algorithm introduced here for the general application is enough to decrypt people for a while. Its encryption base is the simplest Vernum password method, which I will introduce in my next article.

Its rationale is that there is a need to encrypt the

PlainText and a randomly generated decryption key file. The two files are then combined to generate the ciphertext.

(plaintext) combination (key) = encrypted ciphertext

So this article is about the code that generates the key. We assume that the key we generated is a 512-bit long key, which is enough to encrypt a text character. The code is as follows:

keygen.asp file

<%

'******************************

' Keygen.asp

'******************************

Const g_keylocation = "C:\key.txt"

Const G_keylen = 512

On Error Resume Next

Call Writekeytofile (KeyGeN (G_keylen), g_keylocation)

If ERR <> 0 Then

Response.Write "ERROR generating KEY." & "

"

Response.Write Err.Number & "
"

Response.Write Err.Description & "
"

Else

Response.Write "KEY successfully generated."

End If

Sub Writekeytofile (Mykeystring,strfilename)

Dim KeyFile, FSO

Set fso = Server.CreateObject ("Scripting.") FileSystemObject ")

Set keyfile = FSO. CreateTextFile (strFileName, True)

Keyfile.writeline (mykeystring)

Keyfile.close

End Sub

Function KeyGeN (ikeylength)

Dim K, icount, Strmykey

Lowerbound = 35

Upperbound = 96

Randomize ' Initialize random-number generator.

For I = 1 to Ikeylength

s = 255

K = Int (((upperbound-lowerbound) + 1) * Rnd + lowerbound)

Strmykey = Strmykey & Chr (k) & ""

Next

KeyGeN = Strmykey

End Function

%>

Run the above keygen.asp page under IIS. You just have to do this once, and he will write the key in the file C:\key.txt (you can also put the file in a more secure place if you want to). You can then open the Key.txt file, which will contain 512 ASCII characters between 35 and 96. And because it is randomly generated, everyone's private key file Key.txt will be different, and here is an example key file:

iy/;$>=3)? ^-+7m32#q]voii. Q=OFMC ':P 7_b; LG ' =i+@5%*+op:f_= '; Nsy '-^s. ' Aa=bj3m0. WF#T5LGK (=/<:+c2k/^7ai$; PU ' Ome2+t8nd? W$c (j\,;631 ' m-ld5f%%1tf_&k2a-d-54[2p,# ' *ju%6 ' 0rf3cmf0 (#T07U ' fz=>#,+. aw_/+ ']dib;2dtia57tt&-' O '/*f ' m>h.xh5w^0y*=71+5*^ ' ^pkj (=e/x#7a:?,s>r&t;+b#<:-*\@) X9F ' _ '%QA3Z95 .? _T#1,$2#FWW5PBH^*<] A (s0@avd8c^q0r^t1d?) ( 1+,YE71X+.*+U$:3XO^Q]. kg&0n0]; [LJ
The following is a careful analysis of the above procedures, we found that the lowerbound and Upperbound values are actually the ASCII character range that you want to encrypt. A later article describes how to use this key to encrypt and decrypt a string.

In the first section, we discussed how to generate the key, which describes how to use this key to encrypt and decrypt a string.

The following code is the function that can implement this function at the same time

crypt.asp file

<%

Dim G_key

Const G_cryptthis = "Now is"

All good men to come to the aid of their country.

Const g_keylocation = "C:\key.txt"

G_key = Mid (Readkeyfromfile (g_keylocation), 1,len (G_cryptthis))

Response.Write "

ORIGINAL STRING:" & g_cryptthis & "

"

Response.Write "

KEY VALUE:" & G_key & "

"

Response.Write "

ENCRYPTED cyphertext:" & EnCrypt (g_cryptthis) & "

"

Response.Write "

Decrypted cyphertext:" & DeCrypt (EnCrypt (g_cryptthis)) & "

"

Function EnCrypt (Strcryptthis)

Dim Strchar, Ikeychar, Istringchar, I

For I = 1 to Len (strcryptthis)

Ikeychar = ASC (Mid (g_key,i,1))

Istringchar = ASC (Mid (strcryptthis,i,1))

' * * * * Uncomment below to encrypt with addition,

' Icryptchar = Istringchar + Ikeychar

Icryptchar = Ikeychar Xor Istringchar

strencrypted = strencrypted & Chr (Icryptchar)

Next

EnCrypt = strencrypted

End Function

Function DeCrypt (strencrypted)

Dim Strchar, Ikeychar, Istringchar, I

For I = 1 to Len (strencrypted)

Ikeychar = (ASC (mid g_key,i,1))

Istringchar = ASC (Mid (strencrypted,i,1))

' * * * * Uncomment below to decrypt with subtraction

' Idecryptchar = Istringchar-ikeychar

Idecryptchar = Ikeychar Xor Istringchar

strdecrypted = strdecrypted & Chr (Idecryptchar)

Next

DeCrypt = strdecrypted

End Function

Function Readkeyfromfile (strFileName)

Dim KeyFile, FSO, F

Set fso = Server.CreateObject ("Scripting.FileSystemObject")

Set F = fso. GetFile (strFileName)

Set ts = F.openastextstream (1,-2)

Do and not TS. AtEndOfStream

keyfile = keyfile & ts. ReadLine

Loop

Readkeyfromfile = KeyFile

End Function

%>

In crypt.asp we first get the key value from the key file, and then intercept the key with the same length as the plaintext we need to encrypt from the key. Then use a simple XOR or operation to operate the plaintext and the key, and the result is the encrypted ciphertext. The process is simple. Because the use of a different or operation, so decryption will be very simple, as long as the same key to the ciphertext again to be different or operation can be decrypted



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.