Encrypt your data using an ASP encryption Algorithm (II)

Source: Internet
Author: User
Tags chr crypt decrypt
Encryption | data | Algorithm in the first section, we discuss how to generate a key, and here's 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 "<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 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. On the basis of the above, you can add less changes, you can use the same method to encrypt a file. The only thing you need to be aware of is that for a binary file, you need to do some integrity checking to make sure the conversion comes back
Characters do not cross the line. All you need to do now is keep the key in a secure place on the server (not accessible externally)

Note:
The Vernam code was invented in 1918 by Gilbert Vernam, a at&t engineer. This is a method for encrypting and decrypting with a different or method.



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.