Use ASP encryption algorithm to encrypt your data

Source: Internet
Author: User
Tags crypt
Use ASP encryption algorithm to encrypt your data

Introduction

First, briefly introduce the background of encryption. Because the United States prohibits the number of outgoing encrypted digits of several cryptographic algorithms (such as the 40-bit SSL encryption limit), this article will introduce a simple character encryption algorithm that ASP can use, instead of those restricted encryption algorithms. As a matter of fact, the encryption algorithm introduced here is enough for common use to decrypt people for a while. Its encryption is based on the simplest vernum password method. I will introduce this password in the next article.

The basic principle is that an encrypted

Plaintext and a random decryption key file. Then combine the two files to generate the ciphertext.
(Plaintext) combination (key) = encrypted ciphertext

This article describes the code for generating a key. Assume that the generated key 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." & "<p>"
Response. Write err. Number & "<br>"
Response. Write err. Description & "<br>"
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 in IIS. You only need to do this once. He will write the key into the file C:/key.txt, which will be different. The following is an example of the key file:

Iy/; $> = 3 )? ^-+ 7m32 # q] voii. q = ofmc ': p7_ B; <r/8u) xfhc
<Sr_e $. dlg' = 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 [2 P, # '* 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 ^ * <]) (
S0 @ avd8c ^ q0r ^ t1d? (1 +, ye71x +. * + U $: 3xo ^ q]. kg & 0n0];
[LJ <oz6in? 7n4 <GTL? (M'4s8 + 3jmk5] HC % ^ 1 ^ + k ;/
$ Wbxpa? F & 5 ^ e/d $ 7% * o/U [1 /? 8 (5: 1ovwv * 1z-% ': K & V? X1,
1kurd @ 3w0 ^ d) <og40? (Vj4ewl5a5m <$ A); cq36r9i] * u # Q % 1
<Y/& SA % #1 <v

Next, we will analyze the above program carefully and find that the values of lowerbound and upperbound are actually the ASCII character ranges you want to use for encryption. The following article describes how to use this key to encrypt and decrypt a string.

In the first part, we discuss how to generate a key. The following describes how to use this key to encrypt and decrypt a string.

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

Crypt. asp file
<%
Dim g_key
Const g_cryptthis = "Now is the time
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 "<p> original string:" & g_cryptthis & "<p>"
Response. Write "<p> key value:" & g_key & "<p>"
Response. Write "<p> encrypted cyphertext:" & encrypt (g_cryptthis) & "<p>"
Response. Write "<p> decrypted cyphertext:" & decrypt (encrypt (g_cryptthis) & "<p>"

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 while not ts. atendofstream
Keyfile = Keyfile & TS. Readline
Loop

Readkeyfromfile = Keyfile
End Function

%>

In crypt. asp, we first obtain the key value from the key file, and then extract the key of the same length as the plaintext to be encrypted from this key. Then, a simple XOR operation is used to calculate the plaintext and key, and the encrypted ciphertext is obtained. The process is simple. Due to the use of an exclusive or operation, decryption will be very simple, as long as the same key is used to perform another exclusive or operation on the ciphertext can be decrypted. Based on the above introduction, You can encrypt a file in the same way without making any changes. The only thing to note is that for a binary file, you need to perform some integrity checks to ensure that the converted characters do not cross-border. What you need to do now is to save the key to a safe place on the server (it cannot be accessed externally)

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.