Encrypt your data using an ASP encryption algorithm (2)

zhaozj2021-02-16  52

In the first part, discuss how to generate a key, the following will show how this key is used to encrypt and decrypt a string.

The following code is a function that can simultaneously implement this function.

CRYPT.ASP file

<%

DIM G_KEY

Const g_cryptthis = "Now is the time for all good men to come to the aid of their country." Const g_keylocation = "c: /key.txt"

g_key = mid (readkeyfromfile, 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 (streencrypted, 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 get key values ​​from the key file, then take the key from this key and we need to encrypt the same length. Then use a simple number or operation to calculate the plaintext and the key, then the result is the encrypted ciphertext. The process is simple. Since it is used, it is used, so decryption will be very simple, as long as the same key is used to perform different or operations again, it can be decrypted. On the basis of the above, you can use less and change, you can encrypt a file using the same method. The only thing you need to pay attention is that for a binary file, you need to do some integrity checks to ensure that the transition is not the cross. Now you need to do a safe place to save your key on the server (you can't be accessed outside)

Note: The Vernam Password is invented by Gilbert Vernam (he is an AT & T engineer) in 1918. This is a method of encrypting decryption using a different or method.

转载请注明原文地址:https://www.9cbs.com/read-25271.html

New Post(0)